1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64 package org.jaxen.saxpath.base;
65
66 import junit.framework.TestCase;
67
68 public class XPathLexerTokenTest extends TestCase
69 {
70 private XPathLexer lexer;
71
72 public XPathLexerTokenTest(String name)
73 {
74 super( name );
75 }
76
77 public void setUp()
78 {
79 this.lexer = new XPathLexer();
80 }
81
82 public void tearDown()
83 {
84 this.lexer = null;
85 }
86
87 public void testIdentifier()
88 {
89 runTest( "identifier", new int[]{ TokenTypes.IDENTIFIER, TokenTypes.EOF } );
90 }
91
92 public void testNumberInteger()
93 {
94 runTest( "42", new int[]{ TokenTypes.INTEGER, TokenTypes.EOF } );
95 }
96
97 public void testNumberDouble()
98 {
99 runTest( "42.42", new int[]{ TokenTypes.DOUBLE, TokenTypes.EOF } );
100 }
101
102 public void testComma()
103 {
104 runTest( ",", new int[]{ TokenTypes.COMMA, TokenTypes.EOF } );
105 }
106
107 public void testEquals()
108 {
109 runTest( "=", new int[]{ TokenTypes.EQUALS, TokenTypes.EOF } );
110 }
111
112 public void testMinus()
113 {
114 runTest( "-", new int[]{ TokenTypes.MINUS, TokenTypes.EOF } );
115 }
116
117 public void testPlus()
118 {
119 runTest( "+", new int[]{ TokenTypes.PLUS, TokenTypes.EOF } );
120 }
121
122 public void testDollar()
123 {
124 runTest( "$", new int[]{ TokenTypes.DOLLAR, TokenTypes.EOF } );
125 }
126
127 public void testPipe()
128 {
129 runTest( "|", new int[]{ TokenTypes.PIPE, TokenTypes.EOF } );
130 }
131
132 public void testAt()
133 {
134 runTest( "@", new int[]{ TokenTypes.AT, TokenTypes.EOF } );
135 }
136
137 public void testColon()
138 {
139 runTest( ":", new int[]{ TokenTypes.COLON, TokenTypes.EOF } );
140 }
141
142 public void testDoubleColon()
143 {
144 runTest( "::", new int[]{ TokenTypes.DOUBLE_COLON, TokenTypes.EOF } );
145 }
146
147 public void testNot()
148 {
149 runTest( "!", new int[]{ TokenTypes.NOT, TokenTypes.EOF } );
150 }
151
152 public void testNotEquals()
153 {
154 runTest( "!=", new int[]{ TokenTypes.NOT_EQUALS, TokenTypes.EOF } );
155 }
156
157 public void testStar()
158 {
159 runTest( "*", new int[]{ TokenTypes.STAR, TokenTypes.EOF } );
160 }
161
162 public void testLiteralSingleQuote()
163 {
164 runTest( "'literal'", new int[]{ TokenTypes.LITERAL, TokenTypes.EOF } );
165 }
166
167 public void testLiteralDoubleQuote()
168 {
169 runTest( "\"literal\"", new int[]{ TokenTypes.LITERAL, TokenTypes.EOF } );
170 }
171
172 public void testSingleDot()
173 {
174 runTest( ".", new int[]{ TokenTypes.DOT, TokenTypes.EOF } );
175 }
176
177 public void testDoubleDot()
178 {
179 runTest( "..", new int[]{ TokenTypes.DOT_DOT, TokenTypes.EOF });
180 }
181
182 public void testLeftBracket()
183 {
184 runTest( "[", new int[]{ TokenTypes.LEFT_BRACKET, TokenTypes.EOF } );
185 }
186
187 public void testRightBracket()
188 {
189 runTest( "]", new int[]{ TokenTypes.RIGHT_BRACKET, TokenTypes.EOF } );
190 }
191
192 public void testLeftParen()
193 {
194 runTest( "(", new int[]{ TokenTypes.LEFT_PAREN, TokenTypes.EOF } );
195 }
196
197 public void testSingleSlash()
198 {
199 runTest( "/", new int[]{ TokenTypes.SLASH, TokenTypes.EOF } );
200 }
201
202 public void testDoubleSlash()
203 {
204 runTest( "//", new int[]{ TokenTypes.DOUBLE_SLASH, TokenTypes.EOF } );
205 }
206
207 public void testLessThan()
208 {
209 runTest( "<", new int[]{ TokenTypes.LESS_THAN, TokenTypes.EOF } );
210 }
211
212 public void testLessThanEquals()
213 {
214 runTest( "<=", new int[]{ TokenTypes.LESS_THAN_EQUALS, TokenTypes.EOF } );
215 }
216
217 public void testGreaterThan()
218 {
219 runTest( ">", new int[]{ TokenTypes.GREATER_THAN, TokenTypes.EOF } );
220 }
221
222 public void testGreaterThanEquals()
223 {
224 runTest( ">=", new int[]{ TokenTypes.GREATER_THAN_EQUALS, TokenTypes.EOF } );
225 }
226
227 public void testOperatorAnd()
228 {
229 runTest( "identifier and", new int[]{ TokenTypes.IDENTIFIER, TokenTypes.AND, TokenTypes.EOF } );
230 }
231
232 public void testOperatorOr()
233 {
234 runTest( "identifier or", new int[]{ TokenTypes.IDENTIFIER, TokenTypes.OR, TokenTypes.EOF } );
235 }
236
237 public void testOperatorMod()
238 {
239 runTest( "identifier mod", new int[]{ TokenTypes.IDENTIFIER, TokenTypes.MOD, TokenTypes.EOF } );
240 }
241
242 public void testOperatorDiv()
243 {
244 runTest( "identifier div", new int[]{ TokenTypes.IDENTIFIER, TokenTypes.DIV } );
245 }
246
247 public void testWhitespace()
248 {
249 runTest( " \t \t \t", new int[]{ TokenTypes.EOF } );
250 }
251
252 private void runTest(String text,
253 int[] expectedTokens)
254 {
255 this.lexer.setXPath( text );
256
257 int tokenType = 0;
258 Token token = null;
259
260 for ( int i = 0 ; i < expectedTokens.length ; ++i )
261 {
262 tokenType = expectedTokens[i];
263
264 token = this.lexer.nextToken();
265
266 assertNotNull( token );
267
268 assertEquals( tokenType,
269 token.getTokenType() );
270 }
271 }
272 }