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