| 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_Deprecated; |
| 19 | import be.nikiroo.fanfix.supported.SupportType; |
| 20 | import be.nikiroo.utils.IOUtils; |
| 21 | import be.nikiroo.utils.Progress; |
| 22 | import be.nikiroo.utils.test.TestCase; |
| 23 | import be.nikiroo.utils.test.TestLauncher; |
| 24 | |
| 25 | class BasicSupportTest extends TestLauncher { |
| 26 | // quote chars |
| 27 | private char openQuote = Instance.getTrans().getCharacter( |
| 28 | StringId.OPEN_SINGLE_QUOTE); |
| 29 | private char closeQuote = Instance.getTrans().getCharacter( |
| 30 | StringId.CLOSE_SINGLE_QUOTE); |
| 31 | private char openDoubleQuote = Instance.getTrans().getCharacter( |
| 32 | StringId.OPEN_DOUBLE_QUOTE); |
| 33 | private char closeDoubleQuote = Instance.getTrans().getCharacter( |
| 34 | StringId.CLOSE_DOUBLE_QUOTE); |
| 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 |
| 55 | public List<Paragraph> requotify(Paragraph para) { |
| 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 | |
| 65 | paras = support.makeParagraphs(null, "", null); |
| 66 | assertEquals( |
| 67 | "An empty content should not generate paragraphs", |
| 68 | 0, paras.size()); |
| 69 | |
| 70 | paras = support.makeParagraphs(null, |
| 71 | "Line 1</p><p>Line 2</p><p>Line 3</p>", null); |
| 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, |
| 82 | "<p>Line1</p><p>Line2</p><p>Line3</p>", null); |
| 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 |
| 100 | .makeParagraphs( |
| 101 | null, |
| 102 | "<p>Line1</p><p>Line2</p><p>Line3<br/><br><p></p>", |
| 103 | null); |
| 104 | assertEquals(5, paras.size()); |
| 105 | |
| 106 | paras = support |
| 107 | .makeParagraphs( |
| 108 | null, |
| 109 | "<p>Line1</p><p>Line2</p><p>Line3<br/><br><p></p>* * *", |
| 110 | null); |
| 111 | assertEquals(5, paras.size()); |
| 112 | |
| 113 | paras = support.makeParagraphs(null, "1<p>* * *<p>2", |
| 114 | null); |
| 115 | assertEquals(3, paras.size()); |
| 116 | assertEquals(ParagraphType.BREAK, paras.get(1) |
| 117 | .getType()); |
| 118 | |
| 119 | paras = support.makeParagraphs(null, |
| 120 | "1<p><br/><p>* * *<p>2", null); |
| 121 | assertEquals(3, paras.size()); |
| 122 | assertEquals(ParagraphType.BREAK, paras.get(1) |
| 123 | .getType()); |
| 124 | |
| 125 | paras = support.makeParagraphs(null, |
| 126 | "1<p>* * *<br/><p><br><p>2", null); |
| 127 | assertEquals(3, paras.size()); |
| 128 | assertEquals(ParagraphType.BREAK, paras.get(1) |
| 129 | .getType()); |
| 130 | |
| 131 | paras = support.makeParagraphs(null, |
| 132 | "1<p><br/><br>* * *<br/><p><br><p>2", null); |
| 133 | assertEquals(3, paras.size()); |
| 134 | assertEquals(ParagraphType.BREAK, paras.get(1) |
| 135 | .getType()); |
| 136 | } |
| 137 | }); |
| 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 | |
| 184 | } |
| 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 | }); |
| 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 | |
| 259 | char openDoubleQuote = Instance.getTrans() |
| 260 | .getCharacter(StringId.OPEN_DOUBLE_QUOTE); |
| 261 | char closeDoubleQuote = Instance.getTrans() |
| 262 | .getCharacter(StringId.CLOSE_DOUBLE_QUOTE); |
| 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 | }); |
| 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"); |
| 329 | } |
| 330 | |
| 331 | @Override |
| 332 | public void tearDown() throws Exception { |
| 333 | tmp.delete(); |
| 334 | } |
| 335 | |
| 336 | @Override |
| 337 | public void test() throws Exception { |
| 338 | BasicSupport support = BasicSupport.getSupport( |
| 339 | SupportType.TEXT, tmp.toURI().toURL()); |
| 340 | |
| 341 | Story story = support.process(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.getSupport( |
| 381 | SupportType.TEXT, tmp.toURI().toURL()); |
| 382 | |
| 383 | Story story = support.process(null); |
| 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 | }); |
| 392 | } |
| 393 | }); |
| 394 | } |
| 395 | |
| 396 | private class BasicSupportEmpty extends BasicSupport_Deprecated { |
| 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, |
| 425 | InputStream in, Progress pg) throws IOException { |
| 426 | return null; |
| 427 | } |
| 428 | |
| 429 | @Override |
| 430 | protected String getChapterContent(URL source, InputStream in, |
| 431 | int number, Progress pg) throws IOException { |
| 432 | return null; |
| 433 | } |
| 434 | |
| 435 | @Override |
| 436 | // and make it public! |
| 437 | public List<Paragraph> makeParagraphs(URL source, String content, |
| 438 | Progress pg) throws IOException { |
| 439 | return super.makeParagraphs(source, content, pg); |
| 440 | } |
| 441 | |
| 442 | @Override |
| 443 | // and make it public! |
| 444 | public void fixBlanksBreaks(List<Paragraph> paras) { |
| 445 | super.fixBlanksBreaks(paras); |
| 446 | } |
| 447 | |
| 448 | @Override |
| 449 | // and make it public! |
| 450 | public Paragraph processPara(String line) { |
| 451 | return super.processPara(line); |
| 452 | } |
| 453 | |
| 454 | @Override |
| 455 | // and make it public! |
| 456 | public List<Paragraph> requotify(Paragraph para) { |
| 457 | return super.requotify(para); |
| 458 | } |
| 459 | } |
| 460 | } |