make it subtree
[fanfix.git] / test / BasicSupportUtilitiesTest.java
CommitLineData
68e370a4
NR
1package be.nikiroo.fanfix.test;
2
22848428 3import java.io.File;
68e370a4 4import java.io.IOException;
68e370a4
NR
5import java.net.URL;
6import java.util.ArrayList;
7import java.util.List;
68e370a4 8
22848428
NR
9import be.nikiroo.fanfix.Instance;
10import be.nikiroo.fanfix.bundles.StringId;
68e370a4
NR
11import be.nikiroo.fanfix.data.Paragraph;
12import be.nikiroo.fanfix.data.Paragraph.ParagraphType;
22848428 13import be.nikiroo.fanfix.data.Story;
68e370a4 14import be.nikiroo.fanfix.supported.BasicSupport;
8d59ce07
NR
15import be.nikiroo.fanfix.supported.BasicSupportHelper;
16import be.nikiroo.fanfix.supported.BasicSupportImages;
17import be.nikiroo.fanfix.supported.BasicSupportPara;
0ffa4754 18import be.nikiroo.fanfix.supported.SupportType;
22848428 19import be.nikiroo.utils.IOUtils;
ed08c171 20import be.nikiroo.utils.Progress;
68e370a4
NR
21import be.nikiroo.utils.test.TestCase;
22import be.nikiroo.utils.test.TestLauncher;
23
8d59ce07 24class BasicSupportUtilitiesTest extends TestLauncher {
22848428 25 // quote chars
e8eeea0a 26 private char openQuote = Instance.getTrans().getCharacter(
22848428 27 StringId.OPEN_SINGLE_QUOTE);
e8eeea0a 28 private char closeQuote = Instance.getTrans().getCharacter(
22848428 29 StringId.CLOSE_SINGLE_QUOTE);
e8eeea0a 30 private char openDoubleQuote = Instance.getTrans().getCharacter(
22848428 31 StringId.OPEN_DOUBLE_QUOTE);
e8eeea0a 32 private char closeDoubleQuote = Instance.getTrans().getCharacter(
22848428 33 StringId.CLOSE_DOUBLE_QUOTE);
8d59ce07
NR
34
35 public BasicSupportUtilitiesTest(String[] args) {
36 super("BasicSupportUtilities", args);
37
68e370a4
NR
38 addSeries(new TestLauncher("General", args) {
39 {
40 addTest(new TestCase("BasicSupport.makeParagraphs()") {
41 @Override
42 public void test() throws Exception {
8d59ce07 43 BasicSupportParaPublic bsPara = new BasicSupportParaPublic() {
68e370a4
NR
44 @Override
45 public void fixBlanksBreaks(List<Paragraph> paras) {
46 }
47
48 @Override
8d59ce07 49 public List<Paragraph> requotify(Paragraph para, boolean html) {
68e370a4
NR
50 List<Paragraph> paras = new ArrayList<Paragraph>(
51 1);
52 paras.add(para);
53 return paras;
54 }
55 };
56
57 List<Paragraph> paras = null;
58
8d59ce07 59 paras = bsPara.makeParagraphs(null, null, "", true, null);
68e370a4
NR
60 assertEquals(
61 "An empty content should not generate paragraphs",
62 0, paras.size());
63
8d59ce07
NR
64 paras = bsPara.makeParagraphs(null, null,
65 "Line 1</p><p>Line 2</p><p>Line 3</p>", true, null);
68e370a4
NR
66 assertEquals(5, paras.size());
67 assertEquals("Line 1", paras.get(0).getContent());
68 assertEquals(ParagraphType.BLANK, paras.get(1)
69 .getType());
70 assertEquals("Line 2", paras.get(2).getContent());
71 assertEquals(ParagraphType.BLANK, paras.get(3)
72 .getType());
73 assertEquals("Line 3", paras.get(4).getContent());
74
8d59ce07
NR
75 paras = bsPara.makeParagraphs(null, null,
76 "<p>Line1</p><p>Line2</p><p>Line3</p>", true, null);
68e370a4
NR
77 assertEquals(6, paras.size());
78 }
79 });
80
81 addTest(new TestCase("BasicSupport.removeDoubleBlanks()") {
82 @Override
83 public void test() throws Exception {
8d59ce07 84 BasicSupportParaPublic support = new BasicSupportParaPublic();
68e370a4
NR
85
86 List<Paragraph> paras = null;
87
88 paras = support
ed08c171 89 .makeParagraphs(
8d59ce07 90 null,
ed08c171
NR
91 null,
92 "<p>Line1</p><p>Line2</p><p>Line3<br/><br><p></p>",
8d59ce07 93 true,
ed08c171 94 null);
68e370a4
NR
95 assertEquals(5, paras.size());
96
97 paras = support
ed08c171 98 .makeParagraphs(
8d59ce07 99 null,
ed08c171
NR
100 null,
101 "<p>Line1</p><p>Line2</p><p>Line3<br/><br><p></p>* * *",
8d59ce07 102 true,
ed08c171 103 null);
68e370a4
NR
104 assertEquals(5, paras.size());
105
8d59ce07
NR
106 paras = support.makeParagraphs(null, null, "1<p>* * *<p>2",
107 true, null);
68e370a4
NR
108 assertEquals(3, paras.size());
109 assertEquals(ParagraphType.BREAK, paras.get(1)
110 .getType());
111
8d59ce07
NR
112 paras = support.makeParagraphs(null, null,
113 "1<p><br/><p>* * *<p>2", true, null);
68e370a4
NR
114 assertEquals(3, paras.size());
115 assertEquals(ParagraphType.BREAK, paras.get(1)
116 .getType());
117
8d59ce07
NR
118 paras = support.makeParagraphs(null, null,
119 "1<p>* * *<br/><p><br><p>2", true, null);
68e370a4
NR
120 assertEquals(3, paras.size());
121 assertEquals(ParagraphType.BREAK, paras.get(1)
122 .getType());
123
8d59ce07
NR
124 paras = support.makeParagraphs(null, null,
125 "1<p><br/><br>* * *<br/><p><br><p>2", true, null);
68e370a4
NR
126 assertEquals(3, paras.size());
127 assertEquals(ParagraphType.BREAK, paras.get(1)
128 .getType());
129 }
130 });
22848428
NR
131
132 addTest(new TestCase("BasicSupport.processPara() quotes") {
133 @Override
134 public void test() throws Exception {
8d59ce07 135 BasicSupportParaPublic support = new BasicSupportParaPublic();
22848428
NR
136
137 Paragraph para;
138
139 // sanity check
8d59ce07 140 para = support.processPara("", true);
22848428
NR
141 assertEquals(ParagraphType.BLANK, para.getType());
142 //
143
8d59ce07 144 para = support.processPara("\"Yes, my Lord!\"", true);
22848428
NR
145 assertEquals(ParagraphType.QUOTE, para.getType());
146 assertEquals(openDoubleQuote + "Yes, my Lord!"
147 + closeDoubleQuote, para.getContent());
148
8d59ce07 149 para = support.processPara("«Yes, my Lord!»", true);
22848428
NR
150 assertEquals(ParagraphType.QUOTE, para.getType());
151 assertEquals(openDoubleQuote + "Yes, my Lord!"
152 + closeDoubleQuote, para.getContent());
153
8d59ce07 154 para = support.processPara("'Yes, my Lord!'", true);
22848428
NR
155 assertEquals(ParagraphType.QUOTE, para.getType());
156 assertEquals(openQuote + "Yes, my Lord!" + closeQuote,
157 para.getContent());
158
8d59ce07 159 para = support.processPara("‹Yes, my Lord!›", true);
22848428
NR
160 assertEquals(ParagraphType.QUOTE, para.getType());
161 assertEquals(openQuote + "Yes, my Lord!" + closeQuote,
162 para.getContent());
163 }
164 });
165
166 addTest(new TestCase(
167 "BasicSupport.processPara() double-simple quotes") {
168 @Override
169 public void setUp() throws Exception {
170 super.setUp();
171
211f7ddb 172 }
22848428
NR
173
174 @Override
175 public void tearDown() throws Exception {
176
177 super.tearDown();
178 }
179
180 @Override
181 public void test() throws Exception {
8d59ce07 182 BasicSupportParaPublic support = new BasicSupportParaPublic();
22848428
NR
183
184 Paragraph para;
185
8d59ce07 186 para = support.processPara("''Yes, my Lord!''", true);
22848428
NR
187 assertEquals(ParagraphType.QUOTE, para.getType());
188 assertEquals(openDoubleQuote + "Yes, my Lord!"
189 + closeDoubleQuote, para.getContent());
190
8d59ce07 191 para = support.processPara("‹‹Yes, my Lord!››", true);
22848428
NR
192 assertEquals(ParagraphType.QUOTE, para.getType());
193 assertEquals(openDoubleQuote + "Yes, my Lord!"
194 + closeDoubleQuote, para.getContent());
195 }
196 });
197
198 addTest(new TestCase("BasicSupport.processPara() apostrophe") {
199 @Override
200 public void test() throws Exception {
8d59ce07 201 BasicSupportParaPublic support = new BasicSupportParaPublic();
22848428
NR
202
203 Paragraph para;
204
205 String text = "Nous étions en été, mais cela aurait être l'hiver quand nous n'étions encore qu'à Aubeuge";
8d59ce07 206 para = support.processPara(text, true);
22848428
NR
207 assertEquals(ParagraphType.NORMAL, para.getType());
208 assertEquals(text, para.getContent());
209 }
210 });
793f1071
NR
211
212 addTest(new TestCase("BasicSupport.processPara() words count") {
213 @Override
214 public void test() throws Exception {
8d59ce07 215 BasicSupportParaPublic support = new BasicSupportParaPublic();
793f1071
NR
216
217 Paragraph para;
218
8d59ce07 219 para = support.processPara("«Yes, my Lord!»", true);
793f1071
NR
220 assertEquals(3, para.getWords());
221
8d59ce07 222 para = support.processPara("One, twee, trois.", true);
793f1071
NR
223 assertEquals(3, para.getWords());
224 }
225 });
226
227 addTest(new TestCase("BasicSupport.requotify() words count") {
228 @Override
229 public void test() throws Exception {
8d59ce07 230 BasicSupportParaPublic support = new BasicSupportParaPublic();
793f1071 231
e8eeea0a
NR
232 char openDoubleQuote = Instance.getTrans()
233 .getCharacter(StringId.OPEN_DOUBLE_QUOTE);
234 char closeDoubleQuote = Instance.getTrans()
235 .getCharacter(StringId.CLOSE_DOUBLE_QUOTE);
793f1071
NR
236
237 String content = null;
238 Paragraph para = null;
239 List<Paragraph> paras = null;
240 long words = 0;
241
242 content = "One, twee, trois.";
243 para = new Paragraph(ParagraphType.NORMAL, content,
244 content.split(" ").length);
8d59ce07 245 paras = support.requotify(para, false);
793f1071
NR
246 words = 0;
247 for (Paragraph p : paras) {
248 words += p.getWords();
249 }
250 assertEquals("Bad words count in a single paragraph",
251 para.getWords(), words);
252
253 content = "Such WoW! So Web2.0! With Colours!";
254 para = new Paragraph(ParagraphType.NORMAL, content,
255 content.split(" ").length);
8d59ce07 256 paras = support.requotify(para, false);
793f1071
NR
257 words = 0;
258 for (Paragraph p : paras) {
259 words += p.getWords();
260 }
261 assertEquals("Bad words count in a single paragraph",
262 para.getWords(), words);
263
264 content = openDoubleQuote + "Such a good idea!"
265 + closeDoubleQuote
266 + ", she said. This ought to be a new para.";
267 para = new Paragraph(ParagraphType.QUOTE, content,
268 content.split(" ").length);
8d59ce07 269 paras = support.requotify(para, false);
793f1071
NR
270 words = 0;
271 for (Paragraph p : paras) {
272 words += p.getWords();
273 }
274 assertEquals(
275 "Bad words count in a requotified paragraph",
276 para.getWords(), words);
277 }
278 });
22848428
NR
279 }
280 });
281
282 addSeries(new TestLauncher("Text", args) {
283 {
284 addTest(new TestCase("Chapter detection simple") {
285 private File tmp;
286
287 @Override
288 public void setUp() throws Exception {
289 tmp = File.createTempFile("fanfix-text-file_", ".test");
290 IOUtils.writeSmallFile(tmp.getParentFile(),
291 tmp.getName(), "TITLE"
292 + "\n"//
293 + "By nona"
294 + "\n" //
295 + "\n" //
296 + "Chapter 0: Resumé" + "\n" + "\n"
297 + "'sume." + "\n" + "\n"
298 + "Chapter 1: chap1" + "\n" + "\n"
299 + "Fanfan." + "\n" + "\n"
300 + "Chapter 2: Chap2" + "\n" + "\n" //
301 + "Tulipe." + "\n");
211f7ddb 302 }
22848428
NR
303
304 @Override
305 public void tearDown() throws Exception {
306 tmp.delete();
211f7ddb 307 }
22848428
NR
308
309 @Override
310 public void test() throws Exception {
0ffa4754
NR
311 BasicSupport support = BasicSupport.getSupport(
312 SupportType.TEXT, tmp.toURI().toURL());
22848428 313
0ffa4754 314 Story story = support.process(null);
22848428
NR
315
316 assertEquals(2, story.getChapters().size());
317 assertEquals(1, story.getChapters().get(1)
318 .getParagraphs().size());
319 assertEquals("Tulipe.", story.getChapters().get(1)
320 .getParagraphs().get(0).getContent());
321 }
322 });
323
324 addTest(new TestCase("Chapter detection with String 'Chapter'") {
325 private File tmp;
326
327 @Override
328 public void setUp() throws Exception {
329 tmp = File.createTempFile("fanfix-text-file_", ".test");
330 IOUtils.writeSmallFile(tmp.getParentFile(),
331 tmp.getName(), "TITLE"
332 + "\n"//
333 + "By nona"
334 + "\n" //
335 + "\n" //
336 + "Chapter 0: Resumé" + "\n" + "\n"
337 + "'sume." + "\n" + "\n"
338 + "Chapter 1: chap1" + "\n" + "\n"
339 + "Chapter fout-la-merde" + "\n"
340 + "\n"//
341 + "Fanfan." + "\n" + "\n"
342 + "Chapter 2: Chap2" + "\n" + "\n" //
343 + "Tulipe." + "\n");
211f7ddb 344 }
22848428
NR
345
346 @Override
347 public void tearDown() throws Exception {
348 tmp.delete();
211f7ddb 349 }
22848428
NR
350
351 @Override
352 public void test() throws Exception {
0ffa4754
NR
353 BasicSupport support = BasicSupport.getSupport(
354 SupportType.TEXT, tmp.toURI().toURL());
22848428 355
0ffa4754 356 Story story = support.process(null);
22848428
NR
357
358 assertEquals(2, story.getChapters().size());
359 assertEquals(1, story.getChapters().get(1)
360 .getParagraphs().size());
361 assertEquals("Tulipe.", story.getChapters().get(1)
362 .getParagraphs().get(0).getContent());
363 }
364 });
68e370a4
NR
365 }
366 });
367 }
8d59ce07
NR
368
369 class BasicSupportParaPublic extends BasicSupportPara {
370 public BasicSupportParaPublic() {
371 super(new BasicSupportHelper(), new BasicSupportImages());
68e370a4 372 }
8d59ce07 373
68e370a4
NR
374 @Override
375 // and make it public!
8d59ce07
NR
376 public Paragraph makeParagraph(BasicSupport support, URL source,
377 String line, boolean html) {
378 return super.makeParagraph(support, source, line, html);
68e370a4 379 }
8d59ce07 380
68e370a4
NR
381 @Override
382 // and make it public!
8d59ce07
NR
383 public List<Paragraph> makeParagraphs(BasicSupport support,
384 URL source, String content, boolean html, Progress pg)
385 throws IOException {
386 return super.makeParagraphs(support, source, content, html, pg);
68e370a4 387 }
8d59ce07 388
22848428
NR
389 @Override
390 // and make it public!
8d59ce07
NR
391 public Paragraph processPara(String line, boolean html) {
392 return super.processPara(line, html);
22848428 393 }
8d59ce07 394
793f1071
NR
395 @Override
396 // and make it public!
8d59ce07
NR
397 public List<Paragraph> requotify(Paragraph para, boolean html) {
398 return super.requotify(para, html);
793f1071 399 }
68e370a4
NR
400 }
401}