]> gitweb.fperrin.net Git - DictionaryPC.git/blob - src/com/hughes/android/dictionary/parser/WikiLineReaderTest.java
6d275de79bb2af83c5cd3be40cd7b5eded3caf7d
[DictionaryPC.git] / src / com / hughes / android / dictionary / parser / WikiLineReaderTest.java
1 package com.hughes.android.dictionary.parser;
2
3 import java.util.Arrays;
4
5 import junit.framework.TestCase;
6
7 public class WikiLineReaderTest extends TestCase {
8   
9   public void testSimple() {
10     final String wikiText =
11       "Hi" + "\n" +
12       "Hello thad you're <!-- not --> '''pretty''' cool '''''over''''' there." + "\n" +
13       "hi <!--" + "\n" +
14       "multi-line" + "\n" +
15       "# comment -->" + "\n" +
16       "" + "\n" +
17       "asdf\n" + 
18       "# {{template_in_list}}" + "\n" +
19       "[[wikitext]]:[[wikitext]]" + "\n" +  // don't want this to trigger a list
20       "here's [[some blah|some]] wikitext." + "\n" +
21       "here's a {{template|this has an = sign|blah=2|blah2=3|" + "\n" +
22       "blah3=3,[[asdf]|[asdf asdf]|[asdf asdf asdf]],blah4=4}} and some more text." + "\n" +
23       "== Header 2 ==" + "\n" +
24       "{{some-func|blah={{nested-func|n2}}|blah2=asdf}}" + "\n" +
25       "{{unterminated}" + "\n" +
26       "[[unterminated]" + "\n" +
27       "=== {{header-template}} ===" + "\n";
28     
29     final String[] expected = new String[] {
30         "Hi",
31         "Hello thad you're '''pretty''' cool '''''over''''' there.",
32         "hi",
33         "",
34         "asdf",
35         "# {{template_in_list}}",
36         "[[wikitext]]:[[wikitext]]",
37         "here's [[some blah|some]] wikitext.",
38         "here's a {{template|this has an = sign|blah=2|blah2=3| blah3=3,[[asdf]|[asdf asdf]|[asdf asdf asdf]],blah4=4}} and some more text.",
39         "== Header 2 ==",
40         "{{some-func|blah={{nested-func|n2}}|blah2=asdf}}",
41         "{{unterminated}",
42         "[[unterminated]",
43         "=== {{header-template}} ===",
44     };
45     
46     final WikiLineReader wikiLineReader = new WikiLineReader(wikiText);
47     for (int i = 0; i < expected.length; ++i) {
48       assertEquals(expected[i], wikiLineReader.readLine());
49     }
50     final String end = wikiLineReader.readLine();
51     if (end != null) {
52       System.out.println(end);
53     }
54     assertNull(end);
55   }
56   
57   public void testWikiHeading() {
58     assertNull(WikiHeading.getHeading(""));
59     assertNull(WikiHeading.getHeading("="));
60     assertNull(WikiHeading.getHeading("=="));
61     assertNull(WikiHeading.getHeading("=a"));
62     assertNull(WikiHeading.getHeading("=a=="));
63     assertNull(WikiHeading.getHeading("===a=="));
64     assertNull(WikiHeading.getHeading("===a===="));
65     assertNull(WikiHeading.getHeading("a="));
66     assertEquals("a", WikiHeading.getHeading("=a=").name);
67     assertEquals(1, WikiHeading.getHeading("=a=").depth);
68     assertEquals("aa", WikiHeading.getHeading("==aa==").name);
69     assertEquals(2, WikiHeading.getHeading("==aa==").depth);
70   }
71
72   
73   public void testWikiFunction() {
74     assertNull(WikiFunction.getFunction(""));
75     assertNull(WikiFunction.getFunction("[[asdf]]"));
76     assertNull(WikiFunction.getFunction("asd [[asdf]]asdf "));
77     assertEquals("a", WikiFunction.getFunction("{{a}}").name);
78     assertEquals("a", WikiFunction.getFunction("{{a|b}}").name);
79     assertEquals("a", WikiFunction.getFunction("a{{a|b}}a").name);
80     assertEquals("a[[a]]", WikiFunction.getFunction("a{{a[[a]]|b}}a").name);
81     assertEquals("a", WikiFunction.getFunction("a{{a|b[[abc|def]]|[[fgh|jkl]]|qwer}}a").name);
82     assertEquals(Arrays.asList("a", "b[[abc|d=f]]", "qwer", "[[fgh|jkl]]", "qwer"), WikiFunction.getFunction("a{{a|b[[abc|d=f]]|qwer|[[fgh|jkl]]|qwer}}a").args);
83     assertEquals("[[abc|def]]", WikiFunction.getFunction("a{{a|b=[[abc|def]]|qwer|[[fgh|jkl]]|qwer={{asdf}}}}a").namedArgs.get("b"));
84     assertEquals("{{asdf}}", WikiFunction.getFunction("a{{a|b=[[abc|def]]|qwer|[[fgh|jkl]]|qwer={{asdf}}}}a").namedArgs.get("qwer"));
85   }
86
87 }