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 // "==== Header 4 ====" + "\n" +
30 // "===== Header 5 =====" + "\n" +
31 "=== {{header-template}} ===" + "\n";
33 final String expected = "Hi Hello <i>thad</i> you're \n" +
35 " <b>pretty</b> cool <b><i>over</b></i> there. hi \n" +
45 "template:[template_in_list]{}\n" +
49 " Hi again. [[wikitext]]:[[wikitext]] here's [[some]] wikitext. here's a \n" +
50 "template:[template, this has an = sign]{blah=2, blah2=3, blah3=3,[[asdf]|[asdf asdf]|[asdf asdf asdf]],blah4=4}\n" +
51 " and some more text.\n" +
52 "HEADER Header 2 \n" +
55 "template:[header-template]{}\n" +
58 final PrintWikiCallback callback = new PrintWikiCallback();
59 WikiParser.parse(text, callback);
60 assertEquals(expected, callback.builder.toString());
65 static final class PrintWikiCallback implements WikiCallback {
66 final StringBuilder builder = new StringBuilder();
69 public void onComment(String text) {
70 builder.append("\ncomment:").append(text).append("\n");
74 public void onFormatBold(boolean boldOn) {
75 builder.append(boldOn ? "<b>" : "</b>");
79 public void onFormatItalic(boolean italicOn) {
80 builder.append(italicOn ? "<i>" : "</i>");
84 public void onWikiLink(String[] args) {
85 builder.append("[[").append(args[args.length - 1]).append("]]");
89 public void onTemplate(final List<String> positionalArgs, final Map<String,String> namedArgs) {
90 builder.append("\ntemplate:").append(positionalArgs).append(namedArgs).append("\n");
94 public void onText(String text) {
99 public void onHeadingStart(int depth) {
100 builder.append("\nHEADER");
101 for (int i = 0; i < depth; ++i) {
107 public void onHeadingEnd(int depth) {
108 builder.append("\n");
112 public void onNewLine() {
117 public void onNewParagraph() {
118 builder.append("\n\n");
122 public void onListItemStart(String header, int[] section) {
123 builder.append("\n").append("LIST (").append(header).append(")");
127 public void onListItemEnd(String header, int[] section) {
128 //builder.append("\n");
132 public void onUnterminated(String start, String rest) {
133 throw new RuntimeException("bad");
137 public void onInvalidHeaderEnd(String rest) {
138 throw new RuntimeException("bad");