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
.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
);
31 public BasicSupportUtilitiesTest(String
[] args
) {
32 super("BasicSupportUtilities", args
);
34 addSeries(new TestLauncher("General", args
) {
36 addTest(new TestCase("BasicSupport.makeParagraphs()") {
38 public void test() throws Exception
{
39 BasicSupportParaPublic bsPara
= new BasicSupportParaPublic() {
41 public void fixBlanksBreaks(List
<Paragraph
> paras
) {
45 public List
<Paragraph
> requotify(Paragraph para
, boolean html
) {
46 List
<Paragraph
> paras
= new ArrayList
<Paragraph
>(
53 List
<Paragraph
> paras
= null;
55 paras
= bsPara
.makeParagraphs(null, null, "", true, null);
57 "An empty content should not generate paragraphs",
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)
66 assertEquals("Line 2", paras
.get(2).getContent());
67 assertEquals(ParagraphType
.BLANK
, paras
.get(3)
69 assertEquals("Line 3", paras
.get(4).getContent());
71 paras
= bsPara
.makeParagraphs(null, null,
72 "<p>Line1</p><p>Line2</p><p>Line3</p>", true, null);
73 assertEquals(6, paras
.size());
77 addTest(new TestCase("BasicSupport.removeDoubleBlanks()") {
79 public void test() throws Exception
{
80 BasicSupportParaPublic support
= new BasicSupportParaPublic();
82 List
<Paragraph
> paras
= null;
88 "<p>Line1</p><p>Line2</p><p>Line3<br/><br><p></p>",
91 assertEquals(5, paras
.size());
97 "<p>Line1</p><p>Line2</p><p>Line3<br/><br><p></p>* * *",
100 assertEquals(5, paras
.size());
102 paras
= support
.makeParagraphs(null, null, "1<p>* * *<p>2",
104 assertEquals(3, paras
.size());
105 assertEquals(ParagraphType
.BREAK
, paras
.get(1)
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)
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)
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)
128 addTest(new TestCase("BasicSupport.processPara() quotes") {
130 public void test() throws Exception
{
131 BasicSupportParaPublic support
= new BasicSupportParaPublic();
136 para
= support
.processPara("", true);
137 assertEquals(ParagraphType
.BLANK
, para
.getType());
140 para
= support
.processPara("\"Yes, my Lord!\"", true);
141 assertEquals(ParagraphType
.QUOTE
, para
.getType());
142 assertEquals(openDoubleQuote
+ "Yes, my Lord!"
143 + closeDoubleQuote
, para
.getContent());
145 para
= support
.processPara("«Yes, my Lord!»", true);
146 assertEquals(ParagraphType
.QUOTE
, para
.getType());
147 assertEquals(openDoubleQuote
+ "Yes, my Lord!"
148 + closeDoubleQuote
, para
.getContent());
150 para
= support
.processPara("'Yes, my Lord!'", true);
151 assertEquals(ParagraphType
.QUOTE
, para
.getType());
152 assertEquals(openQuote
+ "Yes, my Lord!" + closeQuote
,
155 para
= support
.processPara("‹Yes, my Lord!›", true);
156 assertEquals(ParagraphType
.QUOTE
, para
.getType());
157 assertEquals(openQuote
+ "Yes, my Lord!" + closeQuote
,
162 addTest(new TestCase(
163 "BasicSupport.processPara() double-simple quotes") {
165 public void setUp() throws Exception
{
171 public void tearDown() throws Exception
{
177 public void test() throws Exception
{
178 BasicSupportParaPublic support
= new BasicSupportParaPublic();
182 para
= support
.processPara("''Yes, my Lord!''", true);
183 assertEquals(ParagraphType
.QUOTE
, para
.getType());
184 assertEquals(openDoubleQuote
+ "Yes, my Lord!"
185 + closeDoubleQuote
, para
.getContent());
187 para
= support
.processPara("‹‹Yes, my Lord!››", true);
188 assertEquals(ParagraphType
.QUOTE
, para
.getType());
189 assertEquals(openDoubleQuote
+ "Yes, my Lord!"
190 + closeDoubleQuote
, para
.getContent());
194 addTest(new TestCase("BasicSupport.processPara() apostrophe") {
196 public void test() throws Exception
{
197 BasicSupportParaPublic support
= new BasicSupportParaPublic();
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());
208 addTest(new TestCase("BasicSupport.processPara() words count") {
210 public void test() throws Exception
{
211 BasicSupportParaPublic support
= new BasicSupportParaPublic();
215 para
= support
.processPara("«Yes, my Lord!»", true);
216 assertEquals(3, para
.getWords());
218 para
= support
.processPara("One, twee, trois.", true);
219 assertEquals(3, para
.getWords());
223 addTest(new TestCase("BasicSupport.requotify() words count") {
225 public void test() throws Exception
{
226 BasicSupportParaPublic support
= new BasicSupportParaPublic();
228 char openDoubleQuote
= Instance
.getInstance().getTrans()
229 .getCharacter(StringId
.OPEN_DOUBLE_QUOTE
);
230 char closeDoubleQuote
= Instance
.getInstance().getTrans()
231 .getCharacter(StringId
.CLOSE_DOUBLE_QUOTE
);
233 String content
= null;
234 Paragraph para
= null;
235 List
<Paragraph
> paras
= null;
238 content
= "One, twee, trois.";
239 para
= new Paragraph(ParagraphType
.NORMAL
, content
,
240 content
.split(" ").length
);
241 paras
= support
.requotify(para
, false);
243 for (Paragraph p
: paras
) {
244 words
+= p
.getWords();
246 assertEquals("Bad words count in a single paragraph",
247 para
.getWords(), words
);
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);
254 for (Paragraph p
: paras
) {
255 words
+= p
.getWords();
257 assertEquals("Bad words count in a single paragraph",
258 para
.getWords(), words
);
260 content
= openDoubleQuote
+ "Such a good idea!"
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);
267 for (Paragraph p
: paras
) {
268 words
+= p
.getWords();
271 "Bad words count in a requotified paragraph",
272 para
.getWords(), words
);
278 addSeries(new TestLauncher("Text", args
) {
280 addTest(new TestCase("Chapter detection simple") {
284 public void setUp() throws Exception
{
285 tmp
= File
.createTempFile("fanfix-text-file_", ".test");
286 IOUtils
.writeSmallFile(tmp
.getParentFile(),
287 tmp
.getName(), "TITLE"
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" //
301 public void tearDown() throws Exception
{
306 public void test() throws Exception
{
307 BasicSupport support
= BasicSupport
.getSupport(
308 SupportType
.TEXT
, tmp
.toURI().toURL());
310 Story story
= support
.process(null);
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());
320 addTest(new TestCase("Chapter detection with String 'Chapter'") {
324 public void setUp() throws Exception
{
325 tmp
= File
.createTempFile("fanfix-text-file_", ".test");
326 IOUtils
.writeSmallFile(tmp
.getParentFile(),
327 tmp
.getName(), "TITLE"
332 + "Chapter 0: Resumé" + "\n" + "\n"
333 + "'sume." + "\n" + "\n"
334 + "Chapter 1: chap1" + "\n" + "\n"
335 + "Chapter fout-la-merde" + "\n"
337 + "Fanfan." + "\n" + "\n"
338 + "Chapter 2: Chap2" + "\n" + "\n" //
343 public void tearDown() throws Exception
{
348 public void test() throws Exception
{
349 BasicSupport support
= BasicSupport
.getSupport(
350 SupportType
.TEXT
, tmp
.toURI().toURL());
352 Story story
= support
.process(null);
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());
365 class BasicSupportParaPublic
extends BasicSupportPara
{
366 public BasicSupportParaPublic() {
367 super(new BasicSupportHelper(), new BasicSupportImages());
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
);
378 // and make it public!
379 public List
<Paragraph
> makeParagraphs(BasicSupport support
,
380 URL source
, String content
, boolean html
, Progress pg
)
382 return super.makeParagraphs(support
, source
, content
, html
, pg
);
386 // and make it public!
387 public Paragraph
processPara(String line
, boolean html
) {
388 return super.processPara(line
, html
);
392 // and make it public!
393 public List
<Paragraph
> requotify(Paragraph para
, boolean html
) {
394 return super.requotify(para
, html
);