KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * TestArithmeticParser.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.StringReader JavaDoc;
25
26 import net.percederberg.grammatica.parser.ParseException;
27 import net.percederberg.grammatica.parser.Parser;
28 import net.percederberg.grammatica.parser.ParserCreationException;
29
30 /**
31  * A test case for the generated ArithmeticParser class.
32  *
33  * @author Per Cederberg, <per at percederberg dot net>
34  * @version 1.5
35  */

36 public class TestArithmeticParser extends ParserTestCase {
37
38     /**
39      * The valid input string.
40      */

41     private static final String JavaDoc VALID_INPUT =
42         "1 + 2*a\n" +
43         " + 345";
44
45     /**
46      * The parse tree for the valid input string.
47      */

48     private static final String JavaDoc VALID_OUTPUT =
49         "Expression(2001)\n" +
50         " Term(2003)\n" +
51         " Factor(2005)\n" +
52         " Atom(2006)\n" +
53         " NUMBER(1007): \"1\", line: 1, col: 1\n" +
54         " ExpressionRest(2002)\n" +
55         " ADD(1001): \"+\", line: 1, col: 3\n" +
56         " Expression(2001)\n" +
57         " Term(2003)\n" +
58         " Factor(2005)\n" +
59         " Atom(2006)\n" +
60         " NUMBER(1007): \"2\", line: 1, col: 5\n" +
61         " TermRest(2004)\n" +
62         " MUL(1003): \"*\", line: 1, col: 6\n" +
63         " Term(2003)\n" +
64         " Factor(2005)\n" +
65         " Atom(2006)\n" +
66         " IDENTIFIER(1008): \"a\", line: 1, col: 7\n" +
67         " ExpressionRest(2002)\n" +
68         " ADD(1001): \"+\", line: 2, col: 2\n" +
69         " Expression(2001)\n" +
70         " Term(2003)\n" +
71         " Factor(2005)\n" +
72         " Atom(2006)\n" +
73         " NUMBER(1007): \"345\", line: 2, col: 4\n";
74
75     /**
76      * The unexpected EOF input string.
77      */

78     private static final String JavaDoc UNEXPECTED_EOF_INPUT = "1 *\t \n";
79
80     /**
81      * The unexpected character input string.
82      */

83     private static final String JavaDoc UNEXPECTED_CHAR_INPUT = "1\n # 4";
84
85     /**
86      * The unexpected token input string.
87      */

88     private static final String JavaDoc UNEXPECTED_TOKEN_INPUT = "1 + 2 3";
89
90     /**
91      * Creates a new test case.
92      *
93      * @param name the test case name
94      */

95     public TestArithmeticParser(String JavaDoc name) {
96         super(name);
97     }
98
99     /**
100      * Tests parsing a valid input string.
101      */

102     public void testValidInput() {
103         parse(createParser(VALID_INPUT), VALID_OUTPUT);
104     }
105
106     /**
107      * Tests parsing with an unexpected EOF error.
108      */

109     public void testUnexpectedEOF() {
110         failParse(createParser(UNEXPECTED_EOF_INPUT),
111                   ParseException.UNEXPECTED_EOF_ERROR,
112                   2,
113                   1);
114     }
115
116     /**
117      * Tests parsing with an unexpected character error.
118      */

119     public void testUnexpectedChar() {
120         failParse(createParser(UNEXPECTED_CHAR_INPUT),
121                   ParseException.UNEXPECTED_CHAR_ERROR,
122                   2,
123                   2);
124     }
125
126     /**
127      * Tests parsing with an unexpected token error.
128      */

129     public void testUnexpectedToken() {
130         failParse(createParser(UNEXPECTED_TOKEN_INPUT),
131                   ParseException.UNEXPECTED_TOKEN_ERROR,
132                   1,
133                   7);
134     }
135
136     /**
137      * Tests reusing the same parser for various different inputs.
138      */

139     public void testParserReusage() {
140         Parser p;
141
142         p = createParser(VALID_INPUT);
143         parse(p, VALID_OUTPUT);
144         p.getTokenizer().reset(new StringReader JavaDoc(UNEXPECTED_CHAR_INPUT));
145         failParse(p, ParseException.UNEXPECTED_CHAR_ERROR, 2, 2);
146         p.getTokenizer().reset(new StringReader JavaDoc(VALID_INPUT));
147         parse(p, VALID_OUTPUT);
148     }
149
150     /**
151      * Creates a new parser.
152      *
153      * @param input the input to parse
154      *
155      * @return the parser created
156      */

157     private Parser createParser(String JavaDoc input) {
158         Parser parser = null;
159
160         try {
161             parser = new ArithmeticParser(new StringReader JavaDoc(input));
162             parser.prepare();
163         } catch (ParserCreationException e) {
164             fail(e.getMessage());
165         }
166         return parser;
167     }
168 }
169
Popular Tags