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