KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * TestRegexpParser.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 RegexpParser class.
32  *
33  * @author Per Cederberg, <per at percederberg dot net>
34  * @version 1.0
35  */

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

41     private static final String JavaDoc VALID_INPUT =
42         "[a-z.]+(a|b).?";
43
44     /**
45      * The parse tree for the valid input string.
46      */

47     private static final String JavaDoc VALID_OUTPUT =
48         "Expr(2001)\n" +
49         " Term(2002)\n" +
50         " Fact(2003)\n" +
51         " Atom(2004)\n" +
52         " LEFT_BRACKET(1003): \"[\", line: 1, col: 1\n" +
53         " CharacterSet(2006)\n" +
54         " Character(2007)\n" +
55         " CHAR(1014): \"a\", line: 1, col: 2\n" +
56         " Character(2007)\n" +
57         " CHAR(1014): \"-\", line: 1, col: 3\n" +
58         " Character(2007)\n" +
59         " CHAR(1014): \"z\", line: 1, col: 4\n" +
60         " Character(2007)\n" +
61         " DOT(1011): \".\", line: 1, col: 5\n" +
62         " RIGHT_BRACKET(1004): \"]\", line: 1, col: 6\n" +
63         " AtomModifier(2005)\n" +
64         " PLUS(1009): \"+\", line: 1, col: 7\n" +
65         " Fact(2003)\n" +
66         " Atom(2004)\n" +
67         " LEFT_PAREN(1001): \"(\", line: 1, col: 8\n" +
68         " Expr(2001)\n" +
69         " Term(2002)\n" +
70         " Fact(2003)\n" +
71         " Atom(2004)\n" +
72         " CHAR(1014): \"a\", line: 1, col: 9\n" +
73         " VERTICAL_BAR(1010): \"|\", line: 1, col: 10\n" +
74         " Expr(2001)\n" +
75         " Term(2002)\n" +
76         " Fact(2003)\n" +
77         " Atom(2004)\n" +
78         " CHAR(1014): \"b\", line: 1, col: 11\n" +
79         " RIGHT_PAREN(1002): \")\", line: 1, col: 12\n" +
80         " Fact(2003)\n" +
81         " Atom(2004)\n" +
82         " DOT(1011): \".\", line: 1, col: 13\n" +
83         " AtomModifier(2005)\n" +
84         " QUESTION(1007): \"?\", line: 1, col: 14\n";
85
86     /**
87      * The unexpected EOF input string.
88      */

89     private static final String JavaDoc UNEXPECTED_EOF_INPUT = "(abc";
90
91     /**
92      * The unexpected character input string.
93      */

94     private static final String JavaDoc UNEXPECTED_CHAR_INPUT = "a\nb";
95
96     /**
97      * The unexpected token input string.
98      */

99     private static final String JavaDoc UNEXPECTED_TOKEN_INPUT = "abc)";
100
101     /**
102      * Creates a new test case.
103      *
104      * @param name the test case name
105      */

106     public TestRegexpParser(String JavaDoc name) {
107         super(name);
108     }
109
110     /**
111      * Tests parsing a valid input string.
112      */

113     public void testValidInput() {
114         parse(createParser(VALID_INPUT), VALID_OUTPUT);
115     }
116
117     /**
118      * Tests parsing with an unexpected EOF error.
119      */

120     public void testUnexpectedEOF() {
121         failParse(createParser(UNEXPECTED_EOF_INPUT),
122                   ParseException.UNEXPECTED_EOF_ERROR,
123                   1,
124                   5);
125     }
126
127     /**
128      * Tests parsing with an unexpected character error.
129      */

130     public void testUnexpectedChar() {
131         failParse(createParser(UNEXPECTED_CHAR_INPUT),
132                   ParseException.UNEXPECTED_CHAR_ERROR,
133                   1,
134                   2);
135     }
136
137     /**
138      * Tests parsing with an unexpected token error.
139      */

140     public void testUnexpectedToken() {
141         failParse(createParser(UNEXPECTED_TOKEN_INPUT),
142                   ParseException.UNEXPECTED_TOKEN_ERROR,
143                   1,
144                   4);
145     }
146
147     /**
148      * Creates a new parser.
149      *
150      * @param input the input to parse
151      *
152      * @return the parser created
153      */

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