KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > percederberg > grammatica > test > ParserTestCase


1 /*
2  * ParserTestCase.java
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public License
6  * as published by the Free Software Foundation; either version 2.1
7  * of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free
16  * Software Foundation, Inc., 59 Temple Place, Suite 330, Boston,
17  * MA 02111-1307, USA.
18  *
19  * Copyright (c) 2003-2005 Per Cederberg. All rights reserved.
20  */

21
22 package net.percederberg.grammatica.test;
23
24 import java.io.PrintWriter JavaDoc;
25 import java.io.StringWriter JavaDoc;
26
27 import junit.framework.TestCase;
28
29 import net.percederberg.grammatica.parser.Node;
30 import net.percederberg.grammatica.parser.ParseException;
31 import net.percederberg.grammatica.parser.Parser;
32 import net.percederberg.grammatica.parser.ParserCreationException;
33 import net.percederberg.grammatica.parser.ParserLogException;
34
35 /**
36  * Base class for all the parser test cases.
37  *
38  * @author Per Cederberg, <per at percederberg dot net>
39  * @version 1.0
40  */

41 abstract class ParserTestCase extends TestCase {
42
43     /**
44      * Creates a new test case.
45      *
46      * @param name the test case name
47      */

48     public ParserTestCase(String JavaDoc name) {
49         super(name);
50     }
51
52     /**
53      * Parses with the parser and checks the output. If the parsing
54      * failed or if the tree didn't match the specified output, a test
55      * failure will be reported.
56      *
57      * @param parser the parser to use
58      * @param output the expected parse tree
59      */

60     protected void parse(Parser parser, String JavaDoc output) {
61         try {
62             validateTree(parser.parse(), output);
63         } catch (ParserCreationException e) {
64             fail(e.getMessage());
65         } catch (ParserLogException e) {
66             fail(e.getError(0).getMessage());
67         }
68     }
69
70     /**
71      * Parses with the parser and checks the parse error. If the
72      * parsing succeeded or if the parse exception didn't match the
73      * specified values, a test failure will be reported.
74      *
75      * @param parser the parser to use
76      * @param type the parse error type
77      * @param line the line number
78      * @param column the column number
79      */

80     protected void failParse(Parser parser,
81                              int type,
82                              int line,
83                              int column) {
84
85         try {
86             parser.parse();
87             fail("parsing succeeded");
88         } catch (ParserCreationException e) {
89             fail(e.getMessage());
90         } catch (ParserLogException e) {
91             ParseException p = e.getError(0);
92
93             assertEquals("error count", 1, e.getErrorCount());
94             assertEquals("error type", type, p.getErrorType());
95             assertEquals("line number", line, p.getLine());
96             assertEquals("column number", column, p.getColumn());
97         }
98     }
99
100     /**
101      * Validates that a parse tree is identical to a string
102      * representation. If the two representations mismatch, a test
103      * failure will be reported.
104      *
105      * @param root the parse tree root node
106      * @param str the string representation
107      */

108     private void validateTree(Node root, String JavaDoc str) {
109         StringWriter JavaDoc output = new StringWriter JavaDoc();
110
111         root.printTo(new PrintWriter JavaDoc(output));
112         validateLines(str, output.toString());
113     }
114
115     /**
116      * Validates that two strings are identical. If the two strings
117      * mismatch, a test failure will be reported.
118      *
119      * @param expected the expected result
120      * @param result the result obtained
121      */

122     private void validateLines(String JavaDoc expected, String JavaDoc result) {
123         int line = 1;
124         String JavaDoc expectLine;
125         String JavaDoc resultLine;
126         int pos;
127
128         while (expected.length() > 0 || result.length() > 0) {
129             pos = expected.indexOf('\n');
130             if (pos >= 0) {
131                 expectLine = expected.substring(0, pos);
132                 expected = expected.substring(pos + 1);
133             } else {
134                 expectLine = expected;
135                 expected = "";
136             }
137             pos = result.indexOf('\n');
138             if (pos >= 0) {
139                 resultLine = result.substring(0, pos);
140                 result = result.substring(pos + 1);
141             } else {
142                 resultLine = result;
143                 result = "";
144             }
145             validateLine(line, expectLine, resultLine);
146             line++;
147         }
148     }
149
150     /**
151      * Validates that two strings are identical. If the two strings
152      * mismatch, a test failure will be reported.
153      *
154      * @param line the line number to report
155      * @param expected the expected result
156      * @param result the result obtained
157      */

158     private void validateLine(int line, String JavaDoc expected, String JavaDoc result) {
159         if (!expected.trim().equals(result.trim())) {
160             fail("on line: " + line + ", expected: '" + expected +
161                  "', found: '" + result + "'");
162         }
163     }
164 }
165
Popular Tags