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 library string_scanner.string_scanner_test; |
6 | 6 |
7 import 'package:string_scanner/string_scanner.dart'; | 7 import 'package:string_scanner/string_scanner.dart'; |
8 import 'package:unittest/unittest.dart'; | 8 import 'package:unittest/unittest.dart'; |
9 | 9 |
10 void main() { | 10 void main() { |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
54 expect(scanner.lastMatch, isNull); | 54 expect(scanner.lastMatch, isNull); |
55 expect(scanner.position, equals(0)); | 55 expect(scanner.position, equals(0)); |
56 }); | 56 }); |
57 | 57 |
58 test("matches returns false and doesn't change the state", () { | 58 test("matches returns false and doesn't change the state", () { |
59 expect(scanner.matches(new RegExp('.')), isFalse); | 59 expect(scanner.matches(new RegExp('.')), isFalse); |
60 expect(scanner.lastMatch, isNull); | 60 expect(scanner.lastMatch, isNull); |
61 expect(scanner.position, equals(0)); | 61 expect(scanner.position, equals(0)); |
62 }); | 62 }); |
63 | 63 |
| 64 test("substring returns the empty string", () { |
| 65 expect(scanner.substring(0), isEmpty); |
| 66 }); |
| 67 |
64 test('setting position to 1 throws an ArgumentError', () { | 68 test('setting position to 1 throws an ArgumentError', () { |
65 expect(() { | 69 expect(() { |
66 scanner.position = 1; | 70 scanner.position = 1; |
67 }, throwsArgumentError); | 71 }, throwsArgumentError); |
68 }); | 72 }); |
69 | 73 |
70 test('setting position to -1 throws an ArgumentError', () { | 74 test('setting position to -1 throws an ArgumentError', () { |
71 expect(() { | 75 expect(() { |
72 scanner.position = -1; | 76 scanner.position = -1; |
73 }, throwsArgumentError); | 77 }, throwsArgumentError); |
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
158 }); | 162 }); |
159 | 163 |
160 test("a non-matching matches returns false and doesn't change the state", | 164 test("a non-matching matches returns false and doesn't change the state", |
161 () { | 165 () { |
162 expect(scanner.matches(new RegExp('b(..)')), isFalse); | 166 expect(scanner.matches(new RegExp('b(..)')), isFalse); |
163 expect(scanner.lastMatch, isNull); | 167 expect(scanner.lastMatch, isNull); |
164 expect(scanner.position, equals(0)); | 168 expect(scanner.position, equals(0)); |
165 expect(scanner.rest, equals('foo bar')); | 169 expect(scanner.rest, equals('foo bar')); |
166 }); | 170 }); |
167 | 171 |
| 172 test("substring from the beginning returns the empty string", () { |
| 173 expect(scanner.substring(0), isEmpty); |
| 174 }); |
| 175 |
| 176 test("substring with a custom end returns the substring", () { |
| 177 expect(scanner.substring(0, 3), equals('foo')); |
| 178 }); |
| 179 |
| 180 test("substring with the string length returns the whole string", () { |
| 181 expect(scanner.substring(0, 7), equals('foo bar')); |
| 182 }); |
| 183 |
168 test('setting position to 1 moves the cursor forward', () { | 184 test('setting position to 1 moves the cursor forward', () { |
169 scanner.position = 1; | 185 scanner.position = 1; |
170 expect(scanner.position, equals(1)); | 186 expect(scanner.position, equals(1)); |
171 expect(scanner.rest, equals('oo bar')); | 187 expect(scanner.rest, equals('oo bar')); |
172 | 188 |
173 expect(scanner.scan(new RegExp('oo.')), isTrue); | 189 expect(scanner.scan(new RegExp('oo.')), isTrue); |
174 expect(scanner.lastMatch[0], equals('oo ')); | 190 expect(scanner.lastMatch[0], equals('oo ')); |
175 expect(scanner.position, equals(4)); | 191 expect(scanner.position, equals(4)); |
176 expect(scanner.rest, equals('bar')); | 192 expect(scanner.rest, equals('bar')); |
177 }); | 193 }); |
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
253 expect(scanner.lastMatch, isNull); | 269 expect(scanner.lastMatch, isNull); |
254 expect(scanner.position, equals(7)); | 270 expect(scanner.position, equals(7)); |
255 }); | 271 }); |
256 | 272 |
257 test("matches returns false sets lastMatch to null", () { | 273 test("matches returns false sets lastMatch to null", () { |
258 expect(scanner.matches(new RegExp('.')), isFalse); | 274 expect(scanner.matches(new RegExp('.')), isFalse); |
259 expect(scanner.lastMatch, isNull); | 275 expect(scanner.lastMatch, isNull); |
260 expect(scanner.position, equals(7)); | 276 expect(scanner.position, equals(7)); |
261 }); | 277 }); |
262 | 278 |
| 279 test("substring from the beginning returns the whole string", () { |
| 280 expect(scanner.substring(0), equals('foo bar')); |
| 281 }); |
| 282 |
| 283 test("substring with a custom start returns a substring from there", () { |
| 284 expect(scanner.substring(4), equals('bar')); |
| 285 }); |
| 286 |
| 287 test("substring with a custom start and end returns that substring", () { |
| 288 expect(scanner.substring(3, 5), equals(' b')); |
| 289 }); |
| 290 |
263 test('setting position to 1 moves the cursor backward', () { | 291 test('setting position to 1 moves the cursor backward', () { |
264 scanner.position = 1; | 292 scanner.position = 1; |
265 expect(scanner.position, equals(1)); | 293 expect(scanner.position, equals(1)); |
266 expect(scanner.rest, equals('oo bar')); | 294 expect(scanner.rest, equals('oo bar')); |
267 | 295 |
268 expect(scanner.scan(new RegExp('oo.')), isTrue); | 296 expect(scanner.scan(new RegExp('oo.')), isTrue); |
269 expect(scanner.lastMatch[0], equals('oo ')); | 297 expect(scanner.lastMatch[0], equals('oo ')); |
270 expect(scanner.position, equals(4)); | 298 expect(scanner.position, equals(4)); |
271 expect(scanner.rest, equals('bar')); | 299 expect(scanner.rest, equals('bar')); |
272 }); | 300 }); |
(...skipping 27 matching lines...) Expand all Loading... |
300 expect(() => new StringScanner('foo bar', position: -1), | 328 expect(() => new StringScanner('foo bar', position: -1), |
301 throwsArgumentError); | 329 throwsArgumentError); |
302 }); | 330 }); |
303 | 331 |
304 test('throws an ArgumentError if the position is beyond the string', () { | 332 test('throws an ArgumentError if the position is beyond the string', () { |
305 expect(() => new StringScanner('foo bar', position: 8), | 333 expect(() => new StringScanner('foo bar', position: 8), |
306 throwsArgumentError); | 334 throwsArgumentError); |
307 }); | 335 }); |
308 }); | 336 }); |
309 } | 337 } |
OLD | NEW |