Commit | Line | Data |
---|---|---|
9a553563 NR |
1 | package be.nikiroo.fanfix.test; |
2 | ||
3 | import java.io.File; | |
4 | import java.io.IOException; | |
5 | import java.net.URL; | |
6 | import java.util.ArrayList; | |
7 | import java.util.List; | |
8 | ||
9 | import be.nikiroo.fanfix.Instance; | |
10 | import be.nikiroo.fanfix.bundles.StringId; | |
11 | import be.nikiroo.fanfix.data.Paragraph; | |
12 | import be.nikiroo.fanfix.data.Paragraph.ParagraphType; | |
13 | import be.nikiroo.fanfix.data.Story; | |
14 | import be.nikiroo.fanfix.supported.BasicSupport; | |
15 | import be.nikiroo.fanfix.supported.BasicSupportHelper; | |
16 | import be.nikiroo.fanfix.supported.BasicSupportImages; | |
17 | import be.nikiroo.fanfix.supported.BasicSupportPara; | |
18 | import be.nikiroo.fanfix.supported.SupportType; | |
19 | import be.nikiroo.utils.IOUtils; | |
20 | import be.nikiroo.utils.Progress; | |
21 | import be.nikiroo.utils.test.TestCase; | |
22 | import be.nikiroo.utils.test.TestLauncher; | |
23 | ||
24 | class BasicSupportUtilitiesTest extends TestLauncher { | |
25 | // quote chars | |
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); | |
30 | ||
31 | public BasicSupportUtilitiesTest(String[] args) { | |
32 | super("BasicSupportUtilities", args); | |
33 | ||
34 | addSeries(new TestLauncher("General", args) { | |
35 | { | |
36 | addTest(new TestCase("BasicSupport.makeParagraphs()") { | |
37 | @Override | |
38 | public void test() throws Exception { | |
39 | BasicSupportParaPublic bsPara = new BasicSupportParaPublic() { | |
40 | @Override | |
41 | public void fixBlanksBreaks(List<Paragraph> paras) { | |
42 | } | |
43 | ||
44 | @Override | |
45 | public List<Paragraph> requotify(Paragraph para, boolean html) { | |
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 | ||
55 | paras = bsPara.makeParagraphs(null, null, "", true, null); | |
56 | assertEquals( | |
57 | "An empty content should not generate paragraphs", | |
58 | 0, paras.size()); | |
59 | ||
60 | paras = bsPara.makeParagraphs(null, null, | |
61 | "Line 1</p><p>Line 2</p><p>Line 3</p>", true, null); | |
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 | ||
71 | paras = bsPara.makeParagraphs(null, null, | |
72 | "<p>Line1</p><p>Line2</p><p>Line3</p>", true, null); | |
73 | assertEquals(6, paras.size()); | |
74 | } | |
75 | }); | |
76 | ||
77 | addTest(new TestCase("BasicSupport.removeDoubleBlanks()") { | |
78 | @Override | |
79 | public void test() throws Exception { | |
80 | BasicSupportParaPublic support = new BasicSupportParaPublic(); | |
81 | ||
82 | List<Paragraph> paras = null; | |
83 | ||
84 | paras = support | |
85 | .makeParagraphs( | |
86 | null, | |
87 | null, | |
88 | "<p>Line1</p><p>Line2</p><p>Line3<br/><br><p></p>", | |
89 | true, | |
90 | null); | |
91 | assertEquals(5, paras.size()); | |
92 | ||
93 | paras = support | |
94 | .makeParagraphs( | |
95 | null, | |
96 | null, | |
97 | "<p>Line1</p><p>Line2</p><p>Line3<br/><br><p></p>* * *", | |
98 | true, | |
99 | null); | |
100 | assertEquals(5, paras.size()); | |
101 | ||
102 | paras = support.makeParagraphs(null, null, "1<p>* * *<p>2", | |
103 | true, null); | |
104 | assertEquals(3, paras.size()); | |
105 | assertEquals(ParagraphType.BREAK, paras.get(1) | |
106 | .getType()); | |
107 | ||
108 | paras = support.makeParagraphs(null, null, | |
109 | "1<p><br/><p>* * *<p>2", true, null); | |
110 | assertEquals(3, paras.size()); | |
111 | assertEquals(ParagraphType.BREAK, paras.get(1) | |
112 | .getType()); | |
113 | ||
114 | paras = support.makeParagraphs(null, null, | |
115 | "1<p>* * *<br/><p><br><p>2", true, null); | |
116 | assertEquals(3, paras.size()); | |
117 | assertEquals(ParagraphType.BREAK, paras.get(1) | |
118 | .getType()); | |
119 | ||
120 | paras = support.makeParagraphs(null, null, | |
121 | "1<p><br/><br>* * *<br/><p><br><p>2", true, null); | |
122 | assertEquals(3, paras.size()); | |
123 | assertEquals(ParagraphType.BREAK, paras.get(1) | |
124 | .getType()); | |
125 | } | |
126 | }); | |
127 | ||
128 | addTest(new TestCase("BasicSupport.processPara() quotes") { | |
129 | @Override | |
130 | public void test() throws Exception { | |
131 | BasicSupportParaPublic support = new BasicSupportParaPublic(); | |
132 | ||
133 | Paragraph para; | |
134 | ||
135 | // sanity check | |
136 | para = support.processPara("", true); | |
137 | assertEquals(ParagraphType.BLANK, para.getType()); | |
138 | // | |
139 | ||
140 | para = support.processPara("\"Yes, my Lord!\"", true); | |
141 | assertEquals(ParagraphType.QUOTE, para.getType()); | |
142 | assertEquals(openDoubleQuote + "Yes, my Lord!" | |
143 | + closeDoubleQuote, para.getContent()); | |
144 | ||
145 | para = support.processPara("«Yes, my Lord!»", true); | |
146 | assertEquals(ParagraphType.QUOTE, para.getType()); | |
147 | assertEquals(openDoubleQuote + "Yes, my Lord!" | |
148 | + closeDoubleQuote, para.getContent()); | |
149 | ||
150 | para = support.processPara("'Yes, my Lord!'", true); | |
151 | assertEquals(ParagraphType.QUOTE, para.getType()); | |
152 | assertEquals(openQuote + "Yes, my Lord!" + closeQuote, | |
153 | para.getContent()); | |
154 | ||
155 | para = support.processPara("‹Yes, my Lord!›", true); | |
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 | ||
168 | } | |
169 | ||
170 | @Override | |
171 | public void tearDown() throws Exception { | |
172 | ||
173 | super.tearDown(); | |
174 | } | |
175 | ||
176 | @Override | |
177 | public void test() throws Exception { | |
178 | BasicSupportParaPublic support = new BasicSupportParaPublic(); | |
179 | ||
180 | Paragraph para; | |
181 | ||
182 | para = support.processPara("''Yes, my Lord!''", true); | |
183 | assertEquals(ParagraphType.QUOTE, para.getType()); | |
184 | assertEquals(openDoubleQuote + "Yes, my Lord!" | |
185 | + closeDoubleQuote, para.getContent()); | |
186 | ||
187 | para = support.processPara("‹‹Yes, my Lord!››", true); | |
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 { | |
197 | BasicSupportParaPublic support = new BasicSupportParaPublic(); | |
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"; | |
202 | para = support.processPara(text, true); | |
203 | assertEquals(ParagraphType.NORMAL, para.getType()); | |
204 | assertEquals(text, para.getContent()); | |
205 | } | |
206 | }); | |
207 | ||
208 | addTest(new TestCase("BasicSupport.processPara() words count") { | |
209 | @Override | |
210 | public void test() throws Exception { | |
211 | BasicSupportParaPublic support = new BasicSupportParaPublic(); | |
212 | ||
213 | Paragraph para; | |
214 | ||
215 | para = support.processPara("«Yes, my Lord!»", true); | |
216 | assertEquals(3, para.getWords()); | |
217 | ||
218 | para = support.processPara("One, twee, trois.", true); | |
219 | assertEquals(3, para.getWords()); | |
220 | } | |
221 | }); | |
222 | ||
223 | addTest(new TestCase("BasicSupport.requotify() words count") { | |
224 | @Override | |
225 | public void test() throws Exception { | |
226 | BasicSupportParaPublic support = new BasicSupportParaPublic(); | |
227 | ||
228 | char openDoubleQuote = Instance.getInstance().getTrans() | |
229 | .getCharacter(StringId.OPEN_DOUBLE_QUOTE); | |
230 | char closeDoubleQuote = Instance.getInstance().getTrans() | |
231 | .getCharacter(StringId.CLOSE_DOUBLE_QUOTE); | |
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); | |
241 | paras = support.requotify(para, false); | |
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); | |
252 | paras = support.requotify(para, false); | |
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); | |
265 | paras = support.requotify(para, false); | |
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 | }); | |
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"); | |
298 | } | |
299 | ||
300 | @Override | |
301 | public void tearDown() throws Exception { | |
302 | tmp.delete(); | |
303 | } | |
304 | ||
305 | @Override | |
306 | public void test() throws Exception { | |
307 | BasicSupport support = BasicSupport.getSupport( | |
308 | SupportType.TEXT, tmp.toURI().toURL()); | |
309 | ||
310 | Story story = support.process(null); | |
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"); | |
340 | } | |
341 | ||
342 | @Override | |
343 | public void tearDown() throws Exception { | |
344 | tmp.delete(); | |
345 | } | |
346 | ||
347 | @Override | |
348 | public void test() throws Exception { | |
349 | BasicSupport support = BasicSupport.getSupport( | |
350 | SupportType.TEXT, tmp.toURI().toURL()); | |
351 | ||
352 | Story story = support.process(null); | |
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 | }); | |
361 | } | |
362 | }); | |
363 | } | |
364 | ||
365 | class BasicSupportParaPublic extends BasicSupportPara { | |
366 | public BasicSupportParaPublic() { | |
367 | super(new BasicSupportHelper(), new BasicSupportImages()); | |
368 | } | |
369 | ||
370 | @Override | |
371 | // and make it public! | |
372 | public Paragraph makeParagraph(BasicSupport support, URL source, | |
373 | String line, boolean html) { | |
374 | return super.makeParagraph(support, source, line, html); | |
375 | } | |
376 | ||
377 | @Override | |
378 | // and make it public! | |
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); | |
383 | } | |
384 | ||
385 | @Override | |
386 | // and make it public! | |
387 | public Paragraph processPara(String line, boolean html) { | |
388 | return super.processPara(line, html); | |
389 | } | |
390 | ||
391 | @Override | |
392 | // and make it public! | |
393 | public List<Paragraph> requotify(Paragraph para, boolean html) { | |
394 | return super.requotify(para, html); | |
395 | } | |
396 | } | |
397 | } |