001: // Copyright (C) 2003,2004,2005 by Object Mentor, Inc. All rights reserved.
002: // Released under the terms of the GNU General Public License version 2 or later.
003: package fitnesse.wikitext.widgets;
004:
005: import fitnesse.wikitext.*;
006: import junit.swingui.TestRunner;
007:
008: public class ListWidgetTest extends AbstractWidget {
009: private MockWidgetRoot widgetRoot;
010:
011: public static void main(String[] args) {
012: TestRunner.main(new String[] { "ListWidgetTest" });
013: }
014:
015: protected void setUp() throws Exception {
016: widgetRoot = new MockWidgetRoot();
017: }
018:
019: public void testRegexp() throws Exception {
020: assertMatchEquals(" *Item1", " *Item1");
021: assertMatchEquals(" *Item1\n *Item2", " *Item1\n *Item2");
022: assertMatchEquals(" * Item1\n * Item2\n",
023: " * Item1\n * Item2\n");
024: assertMatchEquals(" *Item1\n *Item1a", " *Item1\n *Item1a");
025: assertMatchEquals("*Item1 *Item1a", null);
026: assertMatchEquals(". *Item1", null);
027: assertMatchEquals(" 1Item1", " 1Item1");
028: assertMatchEquals("\nWikiTextExample *hello\n", null);
029: assertMatches(" * 50 ways to leave your lover.");
030: }
031:
032: public void testSimpleList() throws Exception {
033: formsSimpleOneElementList("Item1");
034: formsSimpleOneElementList("50 ways to leave your lover");
035: }
036:
037: private void formsSimpleOneElementList(String itemText)
038: throws Exception {
039: ListWidget list = new ListWidget(widgetRoot, " *" + itemText);
040: assertTrue("should not be ordered", !list.isOrdered());
041: assertEquals(0, list.getLevel());
042: assertEquals(1, list.numberOfChildren());
043: WikiWidget child = list.nextChild();
044: assertEquals(ListItemWidget.class, child.getClass());
045: ListItemWidget item = (ListItemWidget) child;
046: assertEquals(1, item.numberOfChildren());
047: child = item.nextChild();
048: assertEquals(TextWidget.class, child.getClass());
049: assertEquals(itemText, ((TextWidget) child).getText());
050: }
051:
052: public void testSimpleOrderedList() throws Exception {
053: ListWidget list = new ListWidget(widgetRoot, " 1Item1");
054: assertTrue("should be ordered", list.isOrdered());
055: assertEquals(0, list.getLevel());
056: assertEquals(1, list.numberOfChildren());
057: }
058:
059: public void testMultipleItems() throws Exception {
060: ListWidget list = new ListWidget(widgetRoot, " *Item1\n *Item2");
061: assertEquals(2, list.numberOfChildren());
062: assertEquals(ListItemWidget.class, list.nextChild().getClass());
063: assertEquals(ListItemWidget.class, list.nextChild().getClass());
064: }
065:
066: public void testMultiLevelList() throws Exception {
067: ListWidget list = new ListWidget(widgetRoot,
068: " *Item1\n 1Item1a\n *Item2");
069: assertEquals(3, list.numberOfChildren());
070: assertEquals(0, list.getLevel());
071: assertEquals(ListItemWidget.class, list.nextChild().getClass());
072:
073: WikiWidget child = list.nextChild();
074: assertEquals(ListWidget.class, child.getClass());
075: ListWidget childList = (ListWidget) child;
076: assertEquals(1, childList.getLevel());
077: assertTrue("should be ordered", childList.isOrdered());
078:
079: assertEquals(ListItemWidget.class, list.nextChild().getClass());
080: }
081:
082: public void testHtml() throws Exception {
083: compareHtmlResult(" *Item1", "<ul>\n\t<li>Item1</li>\n</ul>\n");
084: compareHtmlResult(" 1Item1", "<ol>\n\t<li>Item1</li>\n</ol>\n");
085: compareHtmlResult(" *Item1\n 0Item1a",
086: "<ul>\n\t<li>Item1</li>\n\t<ol>\n\t\t<li>Item1a</li>\n\t</ol>\n</ul>\n");
087: compareHtmlResult(" * 50 ways to leave your lover",
088: "<ul>\n\t<li>50 ways to leave your lover</li>\n</ul>\n");
089: }
090:
091: private void compareHtmlResult(String s1, String s2)
092: throws Exception {
093: ListWidget list = new ListWidget(widgetRoot, s1);
094: assertEquals(s2, list.render());
095: }
096:
097: protected String getRegexp() {
098: return ListWidget.REGEXP;
099: }
100:
101: }
|