1 package be
.nikiroo
.fanfix
.test
;
4 import java
.io
.IOException
;
6 import java
.util
.ArrayList
;
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
;
24 class BasicSupportUtilitiesTest
extends TestLauncher
{
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
);
35 public BasicSupportUtilitiesTest(String
[] args
) {
36 super("BasicSupportUtilities", args
);
38 addSeries(new TestLauncher("General", args
) {
40 addTest(new TestCase("BasicSupport.makeParagraphs()") {
42 public void test() throws Exception
{
43 BasicSupportParaPublic bsPara
= new BasicSupportParaPublic() {
45 public void fixBlanksBreaks(List
<Paragraph
> paras
) {
49 public List
<Paragraph
> requotify(Paragraph para
, boolean html
) {
50 List
<Paragraph
> paras
= new ArrayList
<Paragraph
>(
57 List
<Paragraph
> paras
= null;
59 paras
= bsPara
.makeParagraphs(null, null, "", true, null);
61 "An empty content should not generate paragraphs",
64 paras
= bsPara
.makeParagraphs(null, null,
65 "Line 1</p><p>Line 2</p><p>Line 3</p>", true, null);
66 assertEquals(5, paras
.size());
67 assertEquals("Line 1", paras
.get(0).getContent());
68 assertEquals(ParagraphType
.BLANK
, paras
.get(1)
70 assertEquals("Line 2", paras
.get(2).getContent());
71 assertEquals(ParagraphType
.BLANK
, paras
.get(3)
73 assertEquals("Line 3", paras
.get(4).getContent());
75 paras
= bsPara
.makeParagraphs(null, null,
76 "<p>Line1</p><p>Line2</p><p>Line3</p>", true, null);
77 assertEquals(6, paras
.size());
81 addTest(new TestCase("BasicSupport.removeDoubleBlanks()") {
83 public void test() throws Exception
{
84 BasicSupportParaPublic support
= new BasicSupportParaPublic();
86 List
<Paragraph
> paras
= null;
92 "<p>Line1</p><p>Line2</p><p>Line3<br/><br><p></p>",
95 assertEquals(5, paras
.size());
101 "<p>Line1</p><p>Line2</p><p>Line3<br/><br><p></p>* * *",
104 assertEquals(5, paras
.size());
106 paras
= support
.makeParagraphs(null, null, "1<p>* * *<p>2",
108 assertEquals(3, paras
.size());
109 assertEquals(ParagraphType
.BREAK
, paras
.get(1)
112 paras
= support
.makeParagraphs(null, null,
113 "1<p><br/><p>* * *<p>2", true, null);
114 assertEquals(3, paras
.size());
115 assertEquals(ParagraphType
.BREAK
, paras
.get(1)
118 paras
= support
.makeParagraphs(null, null,
119 "1<p>* * *<br/><p><br><p>2", true, null);
120 assertEquals(3, paras
.size());
121 assertEquals(ParagraphType
.BREAK
, paras
.get(1)
124 paras
= support
.makeParagraphs(null, null,
125 "1<p><br/><br>* * *<br/><p><br><p>2", true, null);
126 assertEquals(3, paras
.size());
127 assertEquals(ParagraphType
.BREAK
, paras
.get(1)
132 addTest(new TestCase("BasicSupport.processPara() quotes") {
134 public void test() throws Exception
{
135 BasicSupportParaPublic support
= new BasicSupportParaPublic();
140 para
= support
.processPara("", true);
141 assertEquals(ParagraphType
.BLANK
, para
.getType());
144 para
= support
.processPara("\"Yes, my Lord!\"", true);
145 assertEquals(ParagraphType
.QUOTE
, para
.getType());
146 assertEquals(openDoubleQuote
+ "Yes, my Lord!"
147 + closeDoubleQuote
, para
.getContent());
149 para
= support
.processPara("«Yes, my Lord!»", true);
150 assertEquals(ParagraphType
.QUOTE
, para
.getType());
151 assertEquals(openDoubleQuote
+ "Yes, my Lord!"
152 + closeDoubleQuote
, para
.getContent());
154 para
= support
.processPara("'Yes, my Lord!'", true);
155 assertEquals(ParagraphType
.QUOTE
, para
.getType());
156 assertEquals(openQuote
+ "Yes, my Lord!" + closeQuote
,
159 para
= support
.processPara("‹Yes, my Lord!›", true);
160 assertEquals(ParagraphType
.QUOTE
, para
.getType());
161 assertEquals(openQuote
+ "Yes, my Lord!" + closeQuote
,
166 addTest(new TestCase(
167 "BasicSupport.processPara() double-simple quotes") {
169 public void setUp() throws Exception
{
175 public void tearDown() throws Exception
{
181 public void test() throws Exception
{
182 BasicSupportParaPublic support
= new BasicSupportParaPublic();
186 para
= support
.processPara("''Yes, my Lord!''", true);
187 assertEquals(ParagraphType
.QUOTE
, para
.getType());
188 assertEquals(openDoubleQuote
+ "Yes, my Lord!"
189 + closeDoubleQuote
, para
.getContent());
191 para
= support
.processPara("‹‹Yes, my Lord!››", true);
192 assertEquals(ParagraphType
.QUOTE
, para
.getType());
193 assertEquals(openDoubleQuote
+ "Yes, my Lord!"
194 + closeDoubleQuote
, para
.getContent());
198 addTest(new TestCase("BasicSupport.processPara() apostrophe") {
200 public void test() throws Exception
{
201 BasicSupportParaPublic support
= new BasicSupportParaPublic();
205 String text
= "Nous étions en été, mais cela aurait être l'hiver quand nous n'étions encore qu'à Aubeuge";
206 para
= support
.processPara(text
, true);
207 assertEquals(ParagraphType
.NORMAL
, para
.getType());
208 assertEquals(text
, para
.getContent());
212 addTest(new TestCase("BasicSupport.processPara() words count") {
214 public void test() throws Exception
{
215 BasicSupportParaPublic support
= new BasicSupportParaPublic();
219 para
= support
.processPara("«Yes, my Lord!»", true);
220 assertEquals(3, para
.getWords());
222 para
= support
.processPara("One, twee, trois.", true);
223 assertEquals(3, para
.getWords());
227 addTest(new TestCase("BasicSupport.requotify() words count") {
229 public void test() throws Exception
{
230 BasicSupportParaPublic support
= new BasicSupportParaPublic();
232 char openDoubleQuote
= Instance
.getTrans()
233 .getCharacter(StringId
.OPEN_DOUBLE_QUOTE
);
234 char closeDoubleQuote
= Instance
.getTrans()
235 .getCharacter(StringId
.CLOSE_DOUBLE_QUOTE
);
237 String content
= null;
238 Paragraph para
= null;
239 List
<Paragraph
> paras
= null;
242 content
= "One, twee, trois.";
243 para
= new Paragraph(ParagraphType
.NORMAL
, content
,
244 content
.split(" ").length
);
245 paras
= support
.requotify(para
, false);
247 for (Paragraph p
: paras
) {
248 words
+= p
.getWords();
250 assertEquals("Bad words count in a single paragraph",
251 para
.getWords(), words
);
253 content
= "Such WoW! So Web2.0! With Colours!";
254 para
= new Paragraph(ParagraphType
.NORMAL
, content
,
255 content
.split(" ").length
);
256 paras
= support
.requotify(para
, false);
258 for (Paragraph p
: paras
) {
259 words
+= p
.getWords();
261 assertEquals("Bad words count in a single paragraph",
262 para
.getWords(), words
);
264 content
= openDoubleQuote
+ "Such a good idea!"
266 + ", she said. This ought to be a new para.";
267 para
= new Paragraph(ParagraphType
.QUOTE
, content
,
268 content
.split(" ").length
);
269 paras
= support
.requotify(para
, false);
271 for (Paragraph p
: paras
) {
272 words
+= p
.getWords();
275 "Bad words count in a requotified paragraph",
276 para
.getWords(), words
);
282 addSeries(new TestLauncher("Text", args
) {
284 addTest(new TestCase("Chapter detection simple") {
288 public void setUp() throws Exception
{
289 tmp
= File
.createTempFile("fanfix-text-file_", ".test");
290 IOUtils
.writeSmallFile(tmp
.getParentFile(),
291 tmp
.getName(), "TITLE"
296 + "Chapter 0: Resumé" + "\n" + "\n"
297 + "'sume." + "\n" + "\n"
298 + "Chapter 1: chap1" + "\n" + "\n"
299 + "Fanfan." + "\n" + "\n"
300 + "Chapter 2: Chap2" + "\n" + "\n" //
305 public void tearDown() throws Exception
{
310 public void test() throws Exception
{
311 BasicSupport support
= BasicSupport
.getSupport(
312 SupportType
.TEXT
, tmp
.toURI().toURL());
314 Story story
= support
.process(null);
316 assertEquals(2, story
.getChapters().size());
317 assertEquals(1, story
.getChapters().get(1)
318 .getParagraphs().size());
319 assertEquals("Tulipe.", story
.getChapters().get(1)
320 .getParagraphs().get(0).getContent());
324 addTest(new TestCase("Chapter detection with String 'Chapter'") {
328 public void setUp() throws Exception
{
329 tmp
= File
.createTempFile("fanfix-text-file_", ".test");
330 IOUtils
.writeSmallFile(tmp
.getParentFile(),
331 tmp
.getName(), "TITLE"
336 + "Chapter 0: Resumé" + "\n" + "\n"
337 + "'sume." + "\n" + "\n"
338 + "Chapter 1: chap1" + "\n" + "\n"
339 + "Chapter fout-la-merde" + "\n"
341 + "Fanfan." + "\n" + "\n"
342 + "Chapter 2: Chap2" + "\n" + "\n" //
347 public void tearDown() throws Exception
{
352 public void test() throws Exception
{
353 BasicSupport support
= BasicSupport
.getSupport(
354 SupportType
.TEXT
, tmp
.toURI().toURL());
356 Story story
= support
.process(null);
358 assertEquals(2, story
.getChapters().size());
359 assertEquals(1, story
.getChapters().get(1)
360 .getParagraphs().size());
361 assertEquals("Tulipe.", story
.getChapters().get(1)
362 .getParagraphs().get(0).getContent());
369 class BasicSupportParaPublic
extends BasicSupportPara
{
370 public BasicSupportParaPublic() {
371 super(new BasicSupportHelper(), new BasicSupportImages());
375 // and make it public!
376 public Paragraph
makeParagraph(BasicSupport support
, URL source
,
377 String line
, boolean html
) {
378 return super.makeParagraph(support
, source
, line
, html
);
382 // and make it public!
383 public List
<Paragraph
> makeParagraphs(BasicSupport support
,
384 URL source
, String content
, boolean html
, Progress pg
)
386 return super.makeParagraphs(support
, source
, content
, html
, pg
);
390 // and make it public!
391 public Paragraph
processPara(String line
, boolean html
) {
392 return super.processPara(line
, html
);
396 // and make it public!
397 public List
<Paragraph
> requotify(Paragraph para
, boolean html
) {
398 return super.requotify(para
, html
);