001: // Modified or written by Object Mentor, Inc. for inclusion with FitNesse.
002: // Copyright (c) 2002 Cunningham & Cunningham, Inc.
003: // Released under the terms of the GNU General Public License version 2 or later.
004: package fit;
005:
006: import junit.framework.TestCase;
007: import fit.exception.FitParseException;
008:
009: public class ParseTest extends TestCase {
010: public void testParsing() throws Exception {
011: Parse p = new Parse("leader<Table foo=2>body</table>trailer",
012: new String[] { "table" });
013: assertEquals("leader", p.leader);
014: assertEquals("<Table foo=2>", p.tag);
015: assertEquals("body", p.body);
016: assertEquals("trailer", p.trailer);
017: }
018:
019: public void testRecursing() throws Exception {
020: Parse p = new Parse(
021: "leader<table><TR><Td>body</tD></TR></table>trailer");
022: assertEquals(null, p.body);
023: assertEquals(null, p.parts.body);
024: assertEquals("body", p.parts.parts.body);
025: }
026:
027: public void testIterating() throws Exception {
028: Parse p = new Parse(
029: "leader<table><tr><td>one</td><td>two</td><td>three</td></tr></table>trailer");
030: assertEquals("one", p.parts.parts.body);
031: assertEquals("two", p.parts.parts.more.body);
032: assertEquals("three", p.parts.parts.more.more.body);
033: }
034:
035: public void testIndexing() throws Exception {
036: Parse p = new Parse(
037: "leader<table><tr><td>one</td><td>two</td><td>three</td></tr><tr><td>four</td></tr></table>trailer");
038: assertEquals("one", p.at(0, 0, 0).body);
039: assertEquals("two", p.at(0, 0, 1).body);
040: assertEquals("three", p.at(0, 0, 2).body);
041: assertEquals("three", p.at(0, 0, 3).body);
042: assertEquals("three", p.at(0, 0, 4).body);
043: assertEquals("four", p.at(0, 1, 0).body);
044: assertEquals("four", p.at(0, 1, 1).body);
045: assertEquals("four", p.at(0, 2, 0).body);
046: assertEquals(1, p.size());
047: assertEquals(2, p.parts.size());
048: assertEquals(3, p.parts.parts.size());
049: assertEquals("one", p.leaf().body);
050: assertEquals("four", p.parts.last().leaf().body);
051: }
052:
053: public void testParseException() {
054: try {
055: new Parse(
056: "leader<table><tr><th>one</th><th>two</th><th>three</th></tr><tr><td>four</td></tr></table>trailer");
057: } catch (FitParseException e) {
058: assertEquals(17, e.getErrorOffset());
059: assertEquals("Can't find tag: td", e.getMessage());
060: return;
061: }
062: fail("exptected exception not thrown");
063: }
064:
065: public void testText() throws Exception {
066: String tags[] = { "td" };
067: Parse p = new Parse("<td>a<b</td>", tags);
068: assertEquals("a<b", p.body);
069: assertEquals("a<b", p.text());
070: p = new Parse("<td>\ta>b & b>c && </td>",
071: tags);
072: assertEquals("a>b & b>c &&", p.text());
073: p = new Parse("<td>\ta>b & b>c & </td>",
074: tags);
075: assertEquals("a>b & b>c &", p.text());
076: p = new Parse(
077: "<TD><P><FONT FACE=\"Arial\" SIZE=2>GroupTestFixture</FONT></TD>",
078: tags);
079: assertEquals("GroupTestFixture", p.text());
080: }
081:
082: public void testUnescape() {
083: assertEquals("a<b", Parse.unescape("a<b"));
084: assertEquals("a>b & b>c &&", Parse
085: .unescape("a>b & b>c &&"));
086: assertEquals("&&", Parse.unescape("&amp;&amp;"));
087: assertEquals("a>b & b>c &&", Parse
088: .unescape("a>b & b>c &&"));
089: }
090:
091: public void testUnformat() {
092: assertEquals("ab", Parse.unformat("<font size=+1>a</font>b"));
093: assertEquals("ab", Parse.unformat("a<font size=+1>b</font>"));
094: assertEquals("a<b", Parse.unformat("a<b"));
095: }
096:
097: public void testFindNestedEnd() throws FitParseException {
098: assertEquals(0, Parse.findMatchingEndTag("</t>", 0, "t", 0));
099: assertEquals(7, Parse.findMatchingEndTag("<t></t></t>", 0, "t",
100: 0));
101: assertEquals(14, Parse.findMatchingEndTag("<t></t><t></t></t>",
102: 0, "t", 0));
103: }
104:
105: public void testNestedTables() throws Exception {
106: String nestedTable = "<table><tr><td>embedded</td></tr></table>";
107: Parse p = new Parse(
108: "<table><tr><td>"
109: + nestedTable
110: + "</td></tr>"
111: + "<tr><td>two</td></tr><tr><td>three</td></tr></table>trailer");
112: Parse sub = p.at(0, 0, 0).parts;
113: assertEquals(1, p.size());
114: assertEquals(3, p.parts.size());
115:
116: assertEquals(1, sub.at(0, 0, 0).size());
117: assertEquals("embedded", sub.at(0, 0, 0).body);
118: assertEquals(1, sub.size());
119: assertEquals(1, sub.parts.size());
120: assertEquals(1, sub.parts.parts.size());
121:
122: assertEquals("two", p.at(0, 1, 0).body);
123: assertEquals("three", p.at(0, 2, 0).body);
124: assertEquals(1, p.at(0, 1, 0).size());
125: assertEquals(1, p.at(0, 2, 0).size());
126: }
127:
128: public void testNestedTables2() throws Exception {
129: String nestedTable = "<table><tr><td>embedded</td></tr></table>";
130: String nestedTable2 = "<table><tr><td>" + nestedTable
131: + "</td></tr><tr><td>two</td></tr></table>";
132: Parse p = new Parse("<table><tr><td>one</td></tr><tr><td>"
133: + nestedTable2 + "</td></tr>"
134: + "<tr><td>three</td></tr></table>trailer");
135:
136: assertEquals(1, p.size());
137: assertEquals(3, p.parts.size());
138:
139: assertEquals("one", p.at(0, 0, 0).body);
140: assertEquals("three", p.at(0, 2, 0).body);
141: assertEquals(1, p.at(0, 0, 0).size());
142: assertEquals(1, p.at(0, 2, 0).size());
143:
144: Parse sub = p.at(0, 1, 0).parts;
145: assertEquals(2, sub.parts.size());
146: assertEquals(1, sub.at(0, 0, 0).size());
147: Parse subSub = sub.at(0, 0, 0).parts;
148:
149: assertEquals("embedded", subSub.at(0, 0, 0).body);
150: assertEquals(1, subSub.at(0, 0, 0).size());
151:
152: assertEquals("two", sub.at(0, 1, 0).body);
153: assertEquals(1, sub.at(0, 1, 0).size());
154: }
155:
156: }
|