1 package com.hughes.android.dictionary.parser;
6 import junit.framework.TestCase;
8 public class WikiParserTest extends TestCase {
10 public void testSimple() {
13 "Hello ''thad'' you're <!-- not --> '''pretty''' cool '''''over''''' there." + "\n" +
16 "# comment -->" + "\n" +
21 "# {{template_in_list}}" + "\n" +
24 "[[wikitext]]:[[wikitext]]" + "\n" + // don't want this to trigger a list
25 "here's [[some blah|some]] wikitext." + "\n" +
26 "here's a {{template|this has an = sign|blah=2|blah2=3|" + "\n" +
27 "blah3=3,[[asdf]|[asdf asdf]|[asdf asdf asdf]],blah4=4}} and some more text." + "\n" +
28 "== Header 2 ==" + "\n" +
29 "{{some-func|blah={{nested-func|n2}}|blah2=asdf}}" + "\n" +
30 "{{unterminated}" + "\n" +
31 // "==== Header 4 ====" + "\n" +
32 // "===== Header 5 =====" + "\n" +
33 "=== {{header-template}} ===" + "\n";
35 final String expected = "Hi Hello <i>thad</i> you're \n" +
37 " <b>pretty</b> cool <b><i>over</b></i> there. hi \n" +
47 "template:[template_in_list]{}\n" +
51 " Hi again. [[wikitext]]:[[wikitext]] here's [[some]] wikitext. here's a \n" +
52 "template:[template, this has an = sign]{blah=2, blah2=3, blah3=3,[[asdf]|[asdf asdf]|[asdf asdf asdf]],blah4=4}\n" +
53 " and some more text.\n" +
54 "HEADER Header 2 \n" +
56 "template:[some-func]{blah={{nested-func|n2}}, blah2=asdf}\n" +
58 "template:[unterminate]{}" + "\n" +
61 "template:[header-template]{}\n" +
64 final PrintWikiCallback callback = new PrintWikiCallback();
65 WikiParser.parse(text, callback);
66 assertEquals(expected, callback.builder.toString());
71 static final class PrintWikiCallback implements WikiCallback {
72 final StringBuilder builder = new StringBuilder();
75 public void onComment(String text) {
76 builder.append("\ncomment:").append(text).append("\n");
80 public void onFormatBold(boolean boldOn) {
81 builder.append(boldOn ? "<b>" : "</b>");
85 public void onFormatItalic(boolean italicOn) {
86 builder.append(italicOn ? "<i>" : "</i>");
90 public void onWikiLink(String[] args) {
91 builder.append("[[").append(args[args.length - 1]).append("]]");
95 public void onTemplate(final List<String> positionalArgs, final Map<String,String> namedArgs) {
96 builder.append("\ntemplate:").append(positionalArgs).append(namedArgs).append("\n");
100 public void onText(String text) {
101 builder.append(text);
105 public void onHeadingStart(int depth) {
106 builder.append("\nHEADER");
107 for (int i = 0; i < depth; ++i) {
113 public void onHeadingEnd(int depth) {
114 builder.append("\n");
118 public void onNewLine() {
123 public void onNewParagraph() {
124 builder.append("\n\n");
128 public void onListItemStart(String header, int[] section) {
129 builder.append("\n").append("LIST (").append(header).append(")");
133 public void onListItemEnd(String header, int[] section) {
134 //builder.append("\n");
138 public void onUnterminated(String start, String rest) {
139 //throw new RuntimeException("bad");
143 public void onInvalidHeaderEnd(String rest) {
144 throw new RuntimeException("bad");