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" +
23 "[[wikitext]]:[[wikitext]]" + "\n" + // don't want this to trigger a list
24 "here's [[some blah|some]] wikitext." + "\n" +
25 "here's a {{template|blah=2|blah2=3|" + "\n" +
26 "blah3=3}} and some more text." + "\n" +
27 "== Header 2 ==" + "\n" +
28 // "==== Header 4 ====" + "\n" +
29 // "===== Header 5 =====" + "\n" +
30 "=== {{header-template}} ===" + "\n";
32 final String expected = "Hi Hello <i>thad</i> you're \n" +
34 " <b>pretty</b> cool <b><i>over</b></i> there. hi \n" +
45 " Hi again. [[wikitext]]:[[wikitext]] here's [[some]] wikitext. here's a \n" +
46 "template:[template]{blah=2, blah2=3, blah3=3}\n" +
47 " and some more text.\n" +
48 "HEADER Header 2 \n" +
51 "template:[header-template]{}\n" +
54 final PrintWikiCallback callback = new PrintWikiCallback();
55 WikiParser.parse(text, callback);
56 assertEquals(expected, callback.builder.toString());
61 static final class PrintWikiCallback implements WikiCallback {
62 final StringBuilder builder = new StringBuilder();
65 public void onComment(String text) {
66 builder.append("\ncomment:").append(text).append("\n");
70 public void onFormatBold(boolean boldOn) {
71 builder.append(boldOn ? "<b>" : "</b>");
75 public void onFormatItalic(boolean italicOn) {
76 builder.append(italicOn ? "<i>" : "</i>");
80 public void onWikiLink(String[] args) {
81 builder.append("[[").append(args[args.length - 1]).append("]]");
85 public void onTemplate(final List<String> positionalArgs, final Map<String,String> namedArgs) {
86 builder.append("\ntemplate:").append(positionalArgs).append(namedArgs).append("\n");
90 public void onText(String text) {
95 public void onHeadingStart(int depth) {
96 builder.append("\nHEADER");
97 for (int i = 0; i < depth; ++i) {
103 public void onHeadingEnd(int depth) {
104 builder.append("\n");
108 public void onNewLine() {
113 public void onNewParagraph() {
114 builder.append("\n\n");
118 public void onListItemStart(String header, int[] section) {
119 builder.append("\n").append(header);
123 public void onListItemEnd(String header, int[] section) {
124 //builder.append("\n");
128 public void onUnterminated(String start, String rest) {
129 throw new RuntimeException("bad");
133 public void onInvalidHeaderEnd(String rest) {
134 throw new RuntimeException("bad");