OLD | NEW |
1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 library string_scanner.string_scanner_test; | 5 import 'package:charcode/charcode.dart'; |
6 | |
7 import 'package:string_scanner/string_scanner.dart'; | 6 import 'package:string_scanner/string_scanner.dart'; |
8 import 'package:test/test.dart'; | 7 import 'package:test/test.dart'; |
9 | 8 |
10 void main() { | 9 void main() { |
11 group('with an empty string', () { | 10 group('with an empty string', () { |
12 var scanner; | 11 var scanner; |
13 setUp(() { | 12 setUp(() { |
14 scanner = new StringScanner(''); | 13 scanner = new StringScanner(''); |
15 }); | 14 }); |
16 | 15 |
(...skipping 19 matching lines...) Expand all Loading... |
36 expect(scanner.lastMatch, isNull); | 35 expect(scanner.lastMatch, isNull); |
37 expect(scanner.position, equals(0)); | 36 expect(scanner.position, equals(0)); |
38 }); | 37 }); |
39 | 38 |
40 test("peekChar returns null and doesn't change the state", () { | 39 test("peekChar returns null and doesn't change the state", () { |
41 expect(scanner.peekChar(), isNull); | 40 expect(scanner.peekChar(), isNull); |
42 expect(scanner.lastMatch, isNull); | 41 expect(scanner.lastMatch, isNull); |
43 expect(scanner.position, equals(0)); | 42 expect(scanner.position, equals(0)); |
44 }); | 43 }); |
45 | 44 |
| 45 test("scanChar returns false and doesn't change the state", () { |
| 46 expect(scanner.scanChar($f), isFalse); |
| 47 expect(scanner.lastMatch, isNull); |
| 48 expect(scanner.position, equals(0)); |
| 49 }); |
| 50 |
| 51 test("expectChar fails and doesn't change the state", () { |
| 52 expect(() => scanner.expectChar($f), throwsFormatException); |
| 53 expect(scanner.lastMatch, isNull); |
| 54 expect(scanner.position, equals(0)); |
| 55 }); |
| 56 |
46 test("scan returns false and doesn't change the state", () { | 57 test("scan returns false and doesn't change the state", () { |
47 expect(scanner.scan(new RegExp('.')), isFalse); | 58 expect(scanner.scan(new RegExp('.')), isFalse); |
48 expect(scanner.lastMatch, isNull); | 59 expect(scanner.lastMatch, isNull); |
49 expect(scanner.position, equals(0)); | 60 expect(scanner.position, equals(0)); |
50 }); | 61 }); |
51 | 62 |
52 test("expect throws a FormatException and doesn't change the state", () { | 63 test("expect throws a FormatException and doesn't change the state", () { |
53 expect(() => scanner.expect(new RegExp('.')), throwsFormatException); | 64 expect(() => scanner.expect(new RegExp('.')), throwsFormatException); |
54 expect(scanner.lastMatch, isNull); | 65 expect(scanner.lastMatch, isNull); |
55 expect(scanner.position, equals(0)); | 66 expect(scanner.position, equals(0)); |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
112 expect(scanner.lastMatch, isNull); | 123 expect(scanner.lastMatch, isNull); |
113 expect(scanner.position, equals(0)); | 124 expect(scanner.position, equals(0)); |
114 }); | 125 }); |
115 | 126 |
116 test('peekChar with an argument returns the nth character', () { | 127 test('peekChar with an argument returns the nth character', () { |
117 expect(scanner.peekChar(4), equals(0x62)); | 128 expect(scanner.peekChar(4), equals(0x62)); |
118 expect(scanner.lastMatch, isNull); | 129 expect(scanner.lastMatch, isNull); |
119 expect(scanner.position, equals(0)); | 130 expect(scanner.position, equals(0)); |
120 }); | 131 }); |
121 | 132 |
| 133 test("a matching scanChar returns true moves forward", () { |
| 134 expect(scanner.scanChar($f), isTrue); |
| 135 expect(scanner.lastMatch, isNull); |
| 136 expect(scanner.position, equals(1)); |
| 137 }); |
| 138 |
| 139 test("a non-matching scanChar returns false and does nothing", () { |
| 140 expect(scanner.scanChar($x), isFalse); |
| 141 expect(scanner.lastMatch, isNull); |
| 142 expect(scanner.position, equals(0)); |
| 143 }); |
| 144 |
| 145 test("a matching expectChar moves forward", () { |
| 146 scanner.expectChar($f); |
| 147 expect(scanner.lastMatch, isNull); |
| 148 expect(scanner.position, equals(1)); |
| 149 }); |
| 150 |
| 151 test("a non-matching expectChar fails", () { |
| 152 expect(() => scanner.expectChar($x), throwsFormatException); |
| 153 expect(scanner.lastMatch, isNull); |
| 154 expect(scanner.position, equals(0)); |
| 155 }); |
| 156 |
122 test("a matching scan returns true and changes the state", () { | 157 test("a matching scan returns true and changes the state", () { |
123 expect(scanner.scan(new RegExp('f(..)')), isTrue); | 158 expect(scanner.scan(new RegExp('f(..)')), isTrue); |
124 expect(scanner.lastMatch[1], equals('oo')); | 159 expect(scanner.lastMatch[1], equals('oo')); |
125 expect(scanner.position, equals(3)); | 160 expect(scanner.position, equals(3)); |
126 expect(scanner.rest, equals(' bar')); | 161 expect(scanner.rest, equals(' bar')); |
127 }); | 162 }); |
128 | 163 |
129 test("a non-matching scan returns false and sets lastMatch to null", () { | 164 test("a non-matching scan returns false and sets lastMatch to null", () { |
130 expect(scanner.matches(new RegExp('f(..)')), isTrue); | 165 expect(scanner.matches(new RegExp('f(..)')), isTrue); |
131 expect(scanner.lastMatch, isNotNull); | 166 expect(scanner.lastMatch, isNotNull); |
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
219 | 254 |
220 expect(scanner.scan(new RegExp(' b(..)')), isTrue); | 255 expect(scanner.scan(new RegExp(' b(..)')), isTrue); |
221 expect(scanner.lastMatch[1], equals('ar')); | 256 expect(scanner.lastMatch[1], equals('ar')); |
222 expect(scanner.position, equals(7)); | 257 expect(scanner.position, equals(7)); |
223 expect(scanner.rest, equals('')); | 258 expect(scanner.rest, equals('')); |
224 expect(scanner.isDone, isTrue); | 259 expect(scanner.isDone, isTrue); |
225 expect(scanner.expectDone, isNot(throwsFormatException)); | 260 expect(scanner.expectDone, isNot(throwsFormatException)); |
226 }); | 261 }); |
227 }); | 262 }); |
228 | 263 |
| 264 group('after a scan', () { |
| 265 var scanner; |
| 266 setUp(() { |
| 267 scanner = new StringScanner('foo bar'); |
| 268 expect(scanner.scan('foo'), isTrue); |
| 269 }); |
| 270 |
| 271 test('readChar returns the first character and unsets the last match', () { |
| 272 expect(scanner.readChar(), equals($space)); |
| 273 expect(scanner.lastMatch, isNull); |
| 274 expect(scanner.position, equals(4)); |
| 275 }); |
| 276 |
| 277 test('a matching scanChar returns true and unsets the last match', () { |
| 278 expect(scanner.scanChar($space), isTrue); |
| 279 expect(scanner.lastMatch, isNull); |
| 280 expect(scanner.position, equals(4)); |
| 281 }); |
| 282 |
| 283 test('a matching expectChar returns true and unsets the last match', () { |
| 284 scanner.expectChar($space); |
| 285 expect(scanner.lastMatch, isNull); |
| 286 expect(scanner.position, equals(4)); |
| 287 }); |
| 288 }); |
| 289 |
229 group('at the end of a string', () { | 290 group('at the end of a string', () { |
230 var scanner; | 291 var scanner; |
231 setUp(() { | 292 setUp(() { |
232 scanner = new StringScanner('foo bar'); | 293 scanner = new StringScanner('foo bar'); |
233 expect(scanner.scan('foo bar'), isTrue); | 294 expect(scanner.scan('foo bar'), isTrue); |
234 }); | 295 }); |
235 | 296 |
236 test('is done', () { | 297 test('is done', () { |
237 expect(scanner.isDone, isTrue); | 298 expect(scanner.isDone, isTrue); |
238 expect(scanner.expectDone, isNot(throwsFormatException)); | 299 expect(scanner.expectDone, isNot(throwsFormatException)); |
(...skipping 12 matching lines...) Expand all Loading... |
251 expect(scanner.lastMatch, isNotNull); | 312 expect(scanner.lastMatch, isNotNull); |
252 expect(scanner.position, equals(7)); | 313 expect(scanner.position, equals(7)); |
253 }); | 314 }); |
254 | 315 |
255 test("peekChar returns null and doesn't change the state", () { | 316 test("peekChar returns null and doesn't change the state", () { |
256 expect(scanner.peekChar(), isNull); | 317 expect(scanner.peekChar(), isNull); |
257 expect(scanner.lastMatch, isNotNull); | 318 expect(scanner.lastMatch, isNotNull); |
258 expect(scanner.position, equals(7)); | 319 expect(scanner.position, equals(7)); |
259 }); | 320 }); |
260 | 321 |
| 322 test("scanChar returns false and doesn't change the state", () { |
| 323 expect(scanner.scanChar($f), isFalse); |
| 324 expect(scanner.lastMatch, isNotNull); |
| 325 expect(scanner.position, equals(7)); |
| 326 }); |
| 327 |
| 328 test("expectChar fails and doesn't change the state", () { |
| 329 expect(() => scanner.expectChar($f), throwsFormatException); |
| 330 expect(scanner.lastMatch, isNotNull); |
| 331 expect(scanner.position, equals(7)); |
| 332 }); |
| 333 |
261 test("scan returns false and sets lastMatch to null", () { | 334 test("scan returns false and sets lastMatch to null", () { |
262 expect(scanner.scan(new RegExp('.')), isFalse); | 335 expect(scanner.scan(new RegExp('.')), isFalse); |
263 expect(scanner.lastMatch, isNull); | 336 expect(scanner.lastMatch, isNull); |
264 expect(scanner.position, equals(7)); | 337 expect(scanner.position, equals(7)); |
265 }); | 338 }); |
266 | 339 |
267 test("expect throws a FormatException and sets lastMatch to null", () { | 340 test("expect throws a FormatException and sets lastMatch to null", () { |
268 expect(() => scanner.expect(new RegExp('.')), throwsFormatException); | 341 expect(() => scanner.expect(new RegExp('.')), throwsFormatException); |
269 expect(scanner.lastMatch, isNull); | 342 expect(scanner.lastMatch, isNull); |
270 expect(scanner.position, equals(7)); | 343 expect(scanner.position, equals(7)); |
(...skipping 21 matching lines...) Expand all Loading... |
292 scanner.position = 1; | 365 scanner.position = 1; |
293 expect(scanner.position, equals(1)); | 366 expect(scanner.position, equals(1)); |
294 expect(scanner.rest, equals('oo bar')); | 367 expect(scanner.rest, equals('oo bar')); |
295 | 368 |
296 expect(scanner.scan(new RegExp('oo.')), isTrue); | 369 expect(scanner.scan(new RegExp('oo.')), isTrue); |
297 expect(scanner.lastMatch[0], equals('oo ')); | 370 expect(scanner.lastMatch[0], equals('oo ')); |
298 expect(scanner.position, equals(4)); | 371 expect(scanner.position, equals(4)); |
299 expect(scanner.rest, equals('bar')); | 372 expect(scanner.rest, equals('bar')); |
300 }); | 373 }); |
301 | 374 |
| 375 test('setting and resetting position clears lastMatch', () { |
| 376 var oldPosition = scanner.position; |
| 377 scanner.position = 1; |
| 378 scanner.position = oldPosition; |
| 379 expect(scanner.lastMatch, isNull); |
| 380 }); |
| 381 |
302 test('setting position beyond the string throws an ArgumentError', () { | 382 test('setting position beyond the string throws an ArgumentError', () { |
303 expect(() { | 383 expect(() { |
304 scanner.position = 8; | 384 scanner.position = 8; |
305 }, throwsArgumentError); | 385 }, throwsArgumentError); |
306 }); | 386 }); |
307 | 387 |
308 test('setting position to -1 throws an ArgumentError', () { | 388 test('setting position to -1 throws an ArgumentError', () { |
309 expect(() { | 389 expect(() { |
310 scanner.position = -1; | 390 scanner.position = -1; |
311 }, throwsArgumentError); | 391 }, throwsArgumentError); |
(...skipping 16 matching lines...) Expand all Loading... |
328 expect(() => new StringScanner('foo bar', position: -1), | 408 expect(() => new StringScanner('foo bar', position: -1), |
329 throwsArgumentError); | 409 throwsArgumentError); |
330 }); | 410 }); |
331 | 411 |
332 test('throws an ArgumentError if the position is beyond the string', () { | 412 test('throws an ArgumentError if the position is beyond the string', () { |
333 expect( | 413 expect( |
334 () => new StringScanner('foo bar', position: 8), throwsArgumentError); | 414 () => new StringScanner('foo bar', position: 8), throwsArgumentError); |
335 }); | 415 }); |
336 }); | 416 }); |
337 } | 417 } |
OLD | NEW |