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