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