1 61 62 63 64 65 package org.jaxen.saxpath.base; 66 67 import junit.framework.TestCase; 68 69 public class XPathLexerTest extends TestCase 70 { 71 private XPathLexer lexer; 72 private Token token; 73 74 public XPathLexerTest(String name) 75 { 76 super( name ); 77 } 78 79 public void tearDown() 80 { 81 this.lexer = null; 82 this.token = null; 83 } 84 85 public void testNamespace() 86 { 87 setText( "a:b" ); 88 89 nextToken(); 90 assertEquals( TokenTypes.IDENTIFIER, 91 this.token.getTokenType() ); 92 assertEquals( "a", 93 this.token.getTokenText() ); 94 95 nextToken(); 96 assertEquals( TokenTypes.COLON, 97 this.token.getTokenType() ); 98 99 nextToken(); 100 assertEquals( TokenTypes.IDENTIFIER, 101 this.token.getTokenType() ); 102 assertEquals( "b", 103 this.token.getTokenText() ); 104 } 105 106 public void testIdentifier() 107 { 108 setText( "foo" ); 109 110 nextToken(); 111 assertEquals( TokenTypes.IDENTIFIER, 112 this.token.getTokenType() ); 113 assertEquals( "foo", 114 this.token.getTokenText() ); 115 116 setText( "foo.bar" ); 117 118 nextToken(); 119 assertEquals( TokenTypes.IDENTIFIER, 120 this.token.getTokenType() ); 121 assertEquals( "foo.bar", 122 this.token.getTokenText() ); 123 } 124 125 126 130 public void testBurmeseIdentifierStart() 131 { 132 setText( "\u1000foo" ); 133 134 nextToken(); 135 assertEquals( TokenTypes.ERROR, 136 this.token.getTokenType() ); 137 138 } 139 140 public void testBurmeseIdentifierPart() 141 { 142 setText( "foo\u1000foo" ); 143 144 nextToken(); 145 assertEquals( "foo", 146 this.token.getTokenText() ); 147 nextToken(); 148 assertEquals( TokenTypes.ERROR, 149 this.token.getTokenType() ); 150 151 } 152 153 public void testIdentifierAndOperator() 154 { 155 setText( "foo and bar" ); 156 157 nextToken(); 158 assertEquals( TokenTypes.IDENTIFIER, 159 this.token.getTokenType() ); 160 assertEquals( "foo", 161 this.token.getTokenText() ); 162 163 nextToken(); 164 assertEquals( TokenTypes.AND, 165 this.token.getTokenType() ); 166 nextToken(); 167 assertEquals( TokenTypes.IDENTIFIER, 168 this.token.getTokenType() ); 169 assertEquals( "bar", 170 this.token.getTokenText() ); 171 } 172 173 public void testTrickyIdentifierAndOperator() 174 { 175 setText( "and and and" ); 176 177 nextToken(); 178 assertEquals( TokenTypes.IDENTIFIER, 179 this.token.getTokenType() ); 180 assertEquals( "and", 181 this.token.getTokenText() ); 182 183 nextToken(); 184 assertEquals( TokenTypes.AND, 185 this.token.getTokenType() ); 186 187 nextToken(); 188 assertEquals( TokenTypes.IDENTIFIER, 189 this.token.getTokenType() ); 190 assertEquals( "and", 191 this.token.getTokenText() ); 192 } 193 194 public void testInteger() 195 { 196 setText( "1234" ); 197 198 nextToken(); 199 assertEquals( TokenTypes.INTEGER, 200 this.token.getTokenType() ); 201 assertEquals( "1234", 202 this.token.getTokenText() ); 203 } 204 205 public void testDouble() 206 { 207 setText( "12.34" ); 208 209 nextToken(); 210 assertEquals( TokenTypes.DOUBLE, 211 this.token.getTokenType() ); 212 assertEquals( "12.34", 213 this.token.getTokenText() ); 214 } 215 216 public void testDoubleOnlyDecimal() 217 { 218 setText( ".34" ); 219 220 nextToken(); 221 assertEquals( TokenTypes.DOUBLE, 222 this.token.getTokenType() ); 223 assertEquals( ".34", 224 this.token.getTokenText() ); 225 } 226 227 public void testNumbersAndMode() 228 { 229 setText( "12.34 mod 3" ); 230 231 nextToken(); 232 assertEquals( TokenTypes.DOUBLE, 233 this.token.getTokenType() ); 234 assertEquals( "12.34", 235 this.token.getTokenText() ); 236 237 nextToken(); 238 assertEquals( TokenTypes.MOD, 239 this.token.getTokenType() ); 240 241 nextToken(); 242 assertEquals( TokenTypes.INTEGER, 243 this.token.getTokenType() ); 244 245 } 246 247 public void testSlash() 248 { 249 setText( "/" ); 250 251 nextToken(); 252 assertEquals( TokenTypes.SLASH, 253 this.token.getTokenType() ); 254 } 255 256 public void testDoubleSlash() 257 { 258 setText( "//" ); 259 260 nextToken(); 261 assertEquals( TokenTypes.DOUBLE_SLASH, 262 this.token.getTokenType() ); 263 } 264 265 public void testIdentifierWithColon() 266 { 267 setText ( "foo:bar" ); 268 269 nextToken(); 270 assertEquals( TokenTypes.IDENTIFIER, 271 this.token.getTokenType() ); 272 assertEquals( "foo", 273 this.token.getTokenText() ); 274 275 nextToken(); 276 assertEquals( TokenTypes.COLON, 277 this.token.getTokenType() ); 278 279 nextToken(); 280 assertEquals( TokenTypes.IDENTIFIER, 281 this.token.getTokenType() ); 282 assertEquals( "bar", 283 this.token.getTokenText() ); 284 } 285 286 public void testEOF() 287 { 288 setText( "foo" ); 289 290 nextToken(); 291 assertEquals( TokenTypes.IDENTIFIER, 292 this.token.getTokenType() ); 293 assertEquals( "foo", 294 this.token.getTokenText() ); 295 296 nextToken(); 297 assertEquals( TokenTypes.EOF, 298 this.token.getTokenType() ); 299 } 300 301 public void testWhitespace() 302 { 303 setText ( " / \tfoo:bar" ); 304 305 nextToken(); 306 307 assertEquals( TokenTypes.SLASH, 308 this.token.getTokenType() ); 309 nextToken(); 310 311 assertEquals( TokenTypes.IDENTIFIER, 312 this.token.getTokenType() ); 313 314 assertEquals( "foo", 315 this.token.getTokenText() ); 316 317 nextToken(); 318 319 assertEquals( TokenTypes.COLON, 320 this.token.getTokenType() ); 321 322 nextToken(); 323 324 assertEquals( TokenTypes.IDENTIFIER, 325 this.token.getTokenType() ); 326 327 assertEquals( "bar", 328 this.token.getTokenText() ); 329 } 330 331 private void nextToken() 332 { 333 this.token = this.lexer.nextToken(); 334 } 335 336 private void setText(String text) 337 { 338 this.lexer = new XPathLexer( text ); 339 } 340 } 341 | Popular Tags |