1 package org.jruby.util; 2 3 4 import java.util.regex.Pattern ; 5 6 import junit.framework.TestCase; 7 8 12 public class StringScannerTest extends TestCase { 13 14 private final static Pattern WORD_CHARS = Pattern.compile("\\w+"); 15 private final static Pattern WHITESPACE = Pattern.compile("\\s+"); 16 17 private final static String DATE_STRING = "Fri Dec 12 1975 14:39"; 18 19 22 protected void setUp() throws Exception { 23 super.setUp(); 24 } 25 26 29 protected void tearDown() throws Exception { 30 super.tearDown(); 31 } 32 33 public void testCreate() throws Exception { 34 StringScanner ss = new StringScanner("Test String"); 35 36 assertEquals("Test String", ss.getString()); 37 38 assertEquals(0, ss.getPos()); 39 40 assertFalse("matched() must return false after create", ss.matched()); 41 42 assertEquals(-1, ss.matchedSize()); 43 44 try { 45 ss.unscan(); 46 fail("unscan() called after create must throw exception."); 47 } catch (IllegalStateException e) { 48 49 } 50 } 51 52 public void testSetString() throws Exception { 53 StringScanner ss = new StringScanner("Test String"); 54 55 ss.scan(WORD_CHARS); 56 57 ss.setString("test string"); 58 59 assertEquals("test string", ss.getString()); 60 61 assertEquals(0, ss.getPos()); 62 63 assertFalse("matched() must return false after setString is called", ss.matched()); 64 65 assertEquals(-1, ss.matchedSize()); 66 67 try { 68 ss.unscan(); 69 fail("unscan() must throw exception after setString() is called."); 70 } catch (IllegalStateException e) { 71 72 } 73 } 74 75 public void testEOS() throws Exception { 76 StringScanner ss = new StringScanner("Test String"); 77 78 assertFalse("New StringScanner with non-empty string returned true for isEndOfString", ss.isEndOfString()); 79 80 ss = new StringScanner(""); 81 82 assertTrue("New StringScanner with empty string returned false for isEndOfString", ss.isEndOfString()); 83 } 84 85 86 public void testGetChar() throws Exception { 87 StringScanner ss = new StringScanner("12"); 88 89 char val = ss.getChar(); 90 91 assertEquals('1', val); 92 93 assertFalse(ss.isEndOfString()); 94 95 val = ss.getChar(); 96 97 assertEquals('2', val); 98 assertEquals("2", ss.matchedValue()); 99 100 assertTrue(ss.isEndOfString()); 101 102 val = ss.getChar(); 103 104 assertEquals(0, val); 105 } 106 107 public void testScan() throws Exception { 108 StringScanner ss = new StringScanner("Test String"); 109 110 CharSequence cs = ss.scan(WORD_CHARS); 111 assertEquals("Test", cs); 112 assertTrue("matched() should have returned true", ss.matched()); 113 assertEquals("Test", ss.matchedValue()); 114 115 cs = ss.scan(WORD_CHARS); 116 assertNull("Non match should return null", cs); 117 118 cs = ss.scan(WHITESPACE); 119 cs = ss.scan(WORD_CHARS); 120 assertEquals("String", cs); 121 122 assertTrue("isEndOfString should be true", ss.isEndOfString()); 123 } 124 125 public void testScanUntil() throws Exception { 126 StringScanner ss = new StringScanner(DATE_STRING); 127 128 CharSequence cs = ss.scanUntil(Pattern.compile("1")); 129 130 assertEquals("Fri Dec 1", cs); 131 132 assertTrue("matched() must return true after successful scanUntil()", ss.matched()); 133 134 assertEquals(9, ss.getPos()); 135 136 assertEquals("1", ss.matchedValue()); 137 } 138 139 public void testCheckUntil() throws Exception { 140 StringScanner ss = new StringScanner(DATE_STRING); 141 142 CharSequence cs = ss.checkUntil(Pattern.compile("1")); 143 144 assertEquals("Fri Dec 1", cs); 145 146 assertEquals(0, ss.getPos()); 147 148 assertEquals("1", ss.matchedValue()); 149 } 150 151 public void testSkipUntil() throws Exception { 152 StringScanner ss = new StringScanner(DATE_STRING); 153 154 assertEquals(9, ss.skipUntil(Pattern.compile("1"))); 155 156 assertEquals("1", ss.matchedValue()); 157 } 158 159 public void testPos() throws Exception { 160 StringScanner ss = new StringScanner("word 123"); 161 162 assertEquals(0, ss.getPos()); 163 164 ss.getChar(); 165 166 assertEquals(1, ss.getPos()); 167 168 ss.scan(WORD_CHARS); 169 170 assertEquals(4, ss.getPos()); 171 } 172 173 public void testIsBeginningOfLine() throws Exception { 174 StringScanner ss = new StringScanner("Test\nString\n"); 175 176 assertTrue(ss.isBeginningOfLine()); 177 178 ss.scan(WORD_CHARS); 179 180 assertFalse(ss.isBeginningOfLine()); 181 182 ss.scan(WHITESPACE); 183 184 assertTrue(ss.isBeginningOfLine()); 185 } 186 187 public void testMatched() throws Exception { 188 StringScanner ss = new StringScanner(DATE_STRING); 189 190 assertEquals(3, ss.matches(WORD_CHARS)); 191 192 assertEquals(0, ss.getPos()); 193 194 assertEquals("Fri", ss.matchedValue()); 195 196 assertEquals(3, ss.matchedSize()); 197 198 assertTrue("matched() should have returned true", ss.matched()); 199 } 200 201 public void testCheck() throws Exception { 202 StringScanner ss = new StringScanner(DATE_STRING); 203 204 assertEquals("Fri", ss.check(WORD_CHARS)); 205 206 assertEquals(0, ss.getPos()); 207 208 assertEquals("Fri", ss.matchedValue()); 209 210 assertEquals(3, ss.matchedSize()); 211 212 assertTrue("matched() should have returned true", ss.matched()); 213 } 214 215 public void testSkip() throws Exception { 216 StringScanner ss = new StringScanner(DATE_STRING); 217 218 assertEquals(3, ss.skip(WORD_CHARS)); 219 220 assertEquals(-1, ss.skip(WORD_CHARS)); 221 } 222 223 public void testExists() throws Exception { 224 StringScanner ss = new StringScanner("test string"); 225 226 assertEquals(3, ss.exists(Pattern.compile("s"))); 227 228 assertEquals(0, ss.getPos()); 229 230 assertTrue(ss.matched()); 231 232 assertEquals(-1, ss.exists(Pattern.compile("z"))); 233 } 234 235 public void testUnscan() throws Exception { 236 StringScanner ss = new StringScanner(DATE_STRING); 237 238 ss.scan(WORD_CHARS); 239 240 ss.unscan(); 241 242 assertEquals(0, ss.getPos()); 243 244 assertFalse("matched() must return false after unscan()", ss.matched()); 245 246 assertEquals(-1, ss.matchedSize()); 247 248 assertNull("matchedValue() must return null after unscan()", ss.matchedValue()); 249 250 ss.scan(WHITESPACE); 251 252 try { 253 ss.unscan(); 254 fail("unscan() called after an unmatched scan must throw exception."); 255 } catch (IllegalStateException e) { 256 257 } 258 259 ss.skip(WORD_CHARS); 260 261 ss.unscan(); 262 263 assertEquals(0, ss.getPos()); 264 } 265 266 public void testAppend() throws Exception { 267 StringScanner ss = new StringScanner(DATE_STRING); 268 269 ss.scan(WORD_CHARS); 270 271 ss.append(" +1000 GMT"); 272 273 assertEquals(3, ss.getPos()); 274 275 assertEquals(DATE_STRING + " +1000 GMT", ss.getString()); 276 } 277 278 public void testReset() throws Exception { 279 StringScanner ss = new StringScanner(DATE_STRING); 280 281 ss.scan(WORD_CHARS); 282 283 ss.reset(); 284 285 assertEquals(0, ss.getPos()); 286 287 assertFalse("matched() must return false after reset()", ss.matched()); 288 289 assertEquals(-1, ss.matchedSize()); 290 291 assertNull("matchedValue() must return null after reset()", ss.matchedValue()); 292 293 try { 294 ss.unscan(); 295 fail("unscan() must throw exception after reset()"); 296 } catch (IllegalStateException e) { 297 298 } 299 } 300 301 public void testGrouping() throws Exception { 302 StringScanner ss = new StringScanner(DATE_STRING); 303 304 ss.scan(Pattern.compile("(\\w+) (\\w+) (\\d+)")); 305 306 assertEquals("Fri", ss.group(1)); 307 308 assertEquals("Dec", ss.group(2)); 309 310 assertEquals("12", ss.group(3)); 311 312 assertNull(ss.group(4)); 313 314 ss.scan(WORD_CHARS); 315 316 assertNull(ss.group(1)); 317 318 char c = ss.getChar(); 319 assertEquals(c, ss.group(0).charAt(0)); 320 } 321 322 public void testPrePostMatch() throws Exception { 323 StringScanner ss = new StringScanner(DATE_STRING); 324 325 CharSequence cs = ss.scan(WORD_CHARS); 326 327 ss.scan(WORD_CHARS); 328 assertNull(ss.preMatch()); 329 assertNull(ss.postMatch()); 330 331 ss.scan(WHITESPACE); 332 333 assertEquals("Fri", ss.preMatch()); 334 assertEquals("Dec 12 1975 14:39", ss.postMatch()); 335 336 ss.getChar(); 337 338 assertEquals("Fri ", ss.preMatch()); 339 assertEquals("ec 12 1975 14:39", ss.postMatch()); 340 341 ss.reset(); 342 343 ss.scanUntil(Pattern.compile("1")); 344 345 assertEquals("Fri Dec ", ss.preMatch()); 346 assertEquals("2 1975 14:39", ss.postMatch()); 347 } 348 349 public void testTerminate() throws Exception { 350 StringScanner ss = new StringScanner(DATE_STRING); 351 352 ss.scan(WORD_CHARS); 353 354 ss.terminate(); 355 356 assertTrue("endOfString() should return true after terminate().", ss.isEndOfString()); 357 358 assertFalse("matched() should return false after terminate().", ss.matched()); 359 } 360 361 public void testPeek() throws Exception { 362 StringScanner ss = new StringScanner("test string"); 363 364 assertEquals("test st", ss.peek(7)); 365 366 assertEquals(0, ss.getPos()); 367 368 assertEquals("test string", ss.peek(300)); 369 } 370 371 public void testRest() throws Exception { 372 StringScanner ss = new StringScanner("test string"); 373 374 ss.scan(WORD_CHARS); 375 376 assertEquals(" string", ss.rest()); 377 378 ss.terminate(); 379 380 assertEquals("", ss.rest()); 381 } 382 383 public void testSetPos() throws Exception { 384 StringScanner ss = new StringScanner("test string"); 385 386 ss.setPos(5); 387 388 assertEquals("string", ss.rest()); 389 390 try { 391 ss.setPos(300); 392 fail("setPos() with value greater than the length of the string should throw exception."); 393 } catch (IllegalArgumentException e) { 394 395 } 396 } 397 } 398 | Popular Tags |