1 package com.hughes.android.dictionary.parser;
3 import junit.framework.TestCase;
5 public class WikiParserTest extends TestCase {
7 public void testSimple() {
10 "Hello ''thad'' you're <!-- not --> '''pretty''' cool '''''over''''' there." + "\n" +
13 "# comment -->" + "\n" +
19 "here's [[some blah|some]] wikitext." + "\n" +
20 "here's a {{template|blah=2|blah2=3|" + "\n" +
21 "blah3=3}} and some more text." + "\n" +
22 "== Header 2 ==" + "\n" +
23 "=== {{header-template}} ===" + "\n";
25 final String expected = "Hi Hello <i>thad</i> you're \n" +
27 " <b>pretty</b> cool <b><i>over</b></i> there. hi \n" +
36 " Hi again. here's [[some]] wikitext. here's a \n" +
37 "template:template\n" +
38 " and some more text. \n" +
39 "HEADER Header 2 \n" +
42 "template:header-template\n" +
45 final PrintWikiCallback callback = new PrintWikiCallback();
46 WikiParser.parse(text, callback);
47 assertEquals(expected, callback.builder.toString());
52 static final class PrintWikiCallback implements WikiCallback {
53 final StringBuilder builder = new StringBuilder();
56 public void onComment(String text) {
57 builder.append("\ncomment:").append(text).append("\n");
61 public void onFormatBold(boolean boldOn) {
62 builder.append(boldOn ? "<b>" : "</b>");
66 public void onFormatItalic(boolean italicOn) {
67 builder.append(italicOn ? "<i>" : "</i>");
71 public void onWikiLink(String[] args) {
72 builder.append("[[").append(args[args.length - 1]).append("]]");
76 public void onTemplate(String[][] args) {
77 builder.append("\ntemplate:").append(args[0][0]).append("\n");
81 public void onText(String text) {
86 public void onHeadingStart(int depth) {
87 builder.append("\nHEADER");
88 for (int i = 0; i < depth; ++i) {
94 public void onHeadingEnd(int depth) {
99 public void onNewLine() {
104 public void onNewParagraph() {
105 builder.append("\n\n");
109 public void onListItemStart(String header, int[] section) {
110 builder.append(header);
114 public void onListItemEnd(String header, int[] section) {
115 builder.append("\n");
119 public void onUnterminated(String start, String rest) {
120 throw new RuntimeException("bad");
124 public void onInvalidHeaderEnd(String rest) {
125 throw new RuntimeException("bad");