OLD | NEW |
(Empty) | |
| 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 |
| 3 // BSD-style license that can be found in the LICENSE file. |
| 4 |
| 5 library string_scanner.string_scanner_test; |
| 6 |
| 7 import 'package:string_scanner/string_scanner.dart'; |
| 8 import 'package:test/test.dart'; |
| 9 |
| 10 void main() { |
| 11 group('with an empty string', () { |
| 12 var scanner; |
| 13 setUp(() { |
| 14 scanner = new StringScanner(''); |
| 15 }); |
| 16 |
| 17 test('is done', () { |
| 18 expect(scanner.isDone, isTrue); |
| 19 expect(scanner.expectDone, isNot(throwsFormatException)); |
| 20 }); |
| 21 |
| 22 test('rest is empty', () { |
| 23 expect(scanner.rest, isEmpty); |
| 24 }); |
| 25 |
| 26 test('lastMatch is null', () { |
| 27 expect(scanner.lastMatch, isNull); |
| 28 }); |
| 29 |
| 30 test('position is zero', () { |
| 31 expect(scanner.position, equals(0)); |
| 32 }); |
| 33 |
| 34 test("readChar fails and doesn't change the state", () { |
| 35 expect(scanner.readChar, throwsFormatException); |
| 36 expect(scanner.lastMatch, isNull); |
| 37 expect(scanner.position, equals(0)); |
| 38 }); |
| 39 |
| 40 test("peekChar returns null and doesn't change the state", () { |
| 41 expect(scanner.peekChar(), isNull); |
| 42 expect(scanner.lastMatch, isNull); |
| 43 expect(scanner.position, equals(0)); |
| 44 }); |
| 45 |
| 46 test("scan returns false and doesn't change the state", () { |
| 47 expect(scanner.scan(new RegExp('.')), isFalse); |
| 48 expect(scanner.lastMatch, isNull); |
| 49 expect(scanner.position, equals(0)); |
| 50 }); |
| 51 |
| 52 test("expect throws a FormatException and doesn't change the state", () { |
| 53 expect(() => scanner.expect(new RegExp('.')), throwsFormatException); |
| 54 expect(scanner.lastMatch, isNull); |
| 55 expect(scanner.position, equals(0)); |
| 56 }); |
| 57 |
| 58 test("matches returns false and doesn't change the state", () { |
| 59 expect(scanner.matches(new RegExp('.')), isFalse); |
| 60 expect(scanner.lastMatch, isNull); |
| 61 expect(scanner.position, equals(0)); |
| 62 }); |
| 63 |
| 64 test("substring returns the empty string", () { |
| 65 expect(scanner.substring(0), isEmpty); |
| 66 }); |
| 67 |
| 68 test('setting position to 1 throws an ArgumentError', () { |
| 69 expect(() { |
| 70 scanner.position = 1; |
| 71 }, throwsArgumentError); |
| 72 }); |
| 73 |
| 74 test('setting position to -1 throws an ArgumentError', () { |
| 75 expect(() { |
| 76 scanner.position = -1; |
| 77 }, throwsArgumentError); |
| 78 }); |
| 79 }); |
| 80 |
| 81 group('at the beginning of a string', () { |
| 82 var scanner; |
| 83 setUp(() { |
| 84 scanner = new StringScanner('foo bar'); |
| 85 }); |
| 86 |
| 87 test('is not done', () { |
| 88 expect(scanner.isDone, isFalse); |
| 89 expect(scanner.expectDone, throwsFormatException); |
| 90 }); |
| 91 |
| 92 test('rest is the whole string', () { |
| 93 expect(scanner.rest, equals('foo bar')); |
| 94 }); |
| 95 |
| 96 test('lastMatch is null', () { |
| 97 expect(scanner.lastMatch, isNull); |
| 98 }); |
| 99 |
| 100 test('position is zero', () { |
| 101 expect(scanner.position, equals(0)); |
| 102 }); |
| 103 |
| 104 test('readChar returns the first character and moves forward', () { |
| 105 expect(scanner.readChar(), equals(0x66)); |
| 106 expect(scanner.lastMatch, isNull); |
| 107 expect(scanner.position, equals(1)); |
| 108 }); |
| 109 |
| 110 test('peekChar returns the first character', () { |
| 111 expect(scanner.peekChar(), equals(0x66)); |
| 112 expect(scanner.lastMatch, isNull); |
| 113 expect(scanner.position, equals(0)); |
| 114 }); |
| 115 |
| 116 test('peekChar with an argument returns the nth character', () { |
| 117 expect(scanner.peekChar(4), equals(0x62)); |
| 118 expect(scanner.lastMatch, isNull); |
| 119 expect(scanner.position, equals(0)); |
| 120 }); |
| 121 |
| 122 test("a matching scan returns true and changes the state", () { |
| 123 expect(scanner.scan(new RegExp('f(..)')), isTrue); |
| 124 expect(scanner.lastMatch[1], equals('oo')); |
| 125 expect(scanner.position, equals(3)); |
| 126 expect(scanner.rest, equals(' bar')); |
| 127 }); |
| 128 |
| 129 test("a non-matching scan returns false and sets lastMatch to null", () { |
| 130 expect(scanner.matches(new RegExp('f(..)')), isTrue); |
| 131 expect(scanner.lastMatch, isNotNull); |
| 132 |
| 133 expect(scanner.scan(new RegExp('b(..)')), isFalse); |
| 134 expect(scanner.lastMatch, isNull); |
| 135 expect(scanner.position, equals(0)); |
| 136 expect(scanner.rest, equals('foo bar')); |
| 137 }); |
| 138 |
| 139 test("a matching expect changes the state", () { |
| 140 scanner.expect(new RegExp('f(..)')); |
| 141 expect(scanner.lastMatch[1], equals('oo')); |
| 142 expect(scanner.position, equals(3)); |
| 143 expect(scanner.rest, equals(' bar')); |
| 144 }); |
| 145 |
| 146 test("a non-matching expect throws a FormatException and sets lastMatch to " |
| 147 "null", () { |
| 148 expect(scanner.matches(new RegExp('f(..)')), isTrue); |
| 149 expect(scanner.lastMatch, isNotNull); |
| 150 |
| 151 expect(() => scanner.expect(new RegExp('b(..)')), throwsFormatException); |
| 152 expect(scanner.lastMatch, isNull); |
| 153 expect(scanner.position, equals(0)); |
| 154 expect(scanner.rest, equals('foo bar')); |
| 155 }); |
| 156 |
| 157 test("a matching matches returns true and only changes lastMatch", () { |
| 158 expect(scanner.matches(new RegExp('f(..)')), isTrue); |
| 159 expect(scanner.lastMatch[1], equals('oo')); |
| 160 expect(scanner.position, equals(0)); |
| 161 expect(scanner.rest, equals('foo bar')); |
| 162 }); |
| 163 |
| 164 test("a non-matching matches returns false and doesn't change the state", |
| 165 () { |
| 166 expect(scanner.matches(new RegExp('b(..)')), isFalse); |
| 167 expect(scanner.lastMatch, isNull); |
| 168 expect(scanner.position, equals(0)); |
| 169 expect(scanner.rest, equals('foo bar')); |
| 170 }); |
| 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 |
| 184 test('setting position to 1 moves the cursor forward', () { |
| 185 scanner.position = 1; |
| 186 expect(scanner.position, equals(1)); |
| 187 expect(scanner.rest, equals('oo bar')); |
| 188 |
| 189 expect(scanner.scan(new RegExp('oo.')), isTrue); |
| 190 expect(scanner.lastMatch[0], equals('oo ')); |
| 191 expect(scanner.position, equals(4)); |
| 192 expect(scanner.rest, equals('bar')); |
| 193 }); |
| 194 |
| 195 test('setting position beyond the string throws an ArgumentError', () { |
| 196 expect(() { |
| 197 scanner.position = 8; |
| 198 }, throwsArgumentError); |
| 199 }); |
| 200 |
| 201 test('setting position to -1 throws an ArgumentError', () { |
| 202 expect(() { |
| 203 scanner.position = -1; |
| 204 }, throwsArgumentError); |
| 205 }); |
| 206 |
| 207 test('scan accepts any Pattern', () { |
| 208 expect(scanner.scan('foo'), isTrue); |
| 209 expect(scanner.lastMatch[0], equals('foo')); |
| 210 expect(scanner.position, equals(3)); |
| 211 expect(scanner.rest, equals(' bar')); |
| 212 }); |
| 213 |
| 214 test('scans multiple times', () { |
| 215 expect(scanner.scan(new RegExp('f(..)')), isTrue); |
| 216 expect(scanner.lastMatch[1], equals('oo')); |
| 217 expect(scanner.position, equals(3)); |
| 218 expect(scanner.rest, equals(' bar')); |
| 219 |
| 220 expect(scanner.scan(new RegExp(' b(..)')), isTrue); |
| 221 expect(scanner.lastMatch[1], equals('ar')); |
| 222 expect(scanner.position, equals(7)); |
| 223 expect(scanner.rest, equals('')); |
| 224 expect(scanner.isDone, isTrue); |
| 225 expect(scanner.expectDone, isNot(throwsFormatException)); |
| 226 }); |
| 227 }); |
| 228 |
| 229 group('at the end of a string', () { |
| 230 var scanner; |
| 231 setUp(() { |
| 232 scanner = new StringScanner('foo bar'); |
| 233 expect(scanner.scan('foo bar'), isTrue); |
| 234 }); |
| 235 |
| 236 test('is done', () { |
| 237 expect(scanner.isDone, isTrue); |
| 238 expect(scanner.expectDone, isNot(throwsFormatException)); |
| 239 }); |
| 240 |
| 241 test('rest is empty', () { |
| 242 expect(scanner.rest, isEmpty); |
| 243 }); |
| 244 |
| 245 test('position is zero', () { |
| 246 expect(scanner.position, equals(7)); |
| 247 }); |
| 248 |
| 249 test("readChar fails and doesn't change the state", () { |
| 250 expect(scanner.readChar, throwsFormatException); |
| 251 expect(scanner.lastMatch, isNotNull); |
| 252 expect(scanner.position, equals(7)); |
| 253 }); |
| 254 |
| 255 test("peekChar returns null and doesn't change the state", () { |
| 256 expect(scanner.peekChar(), isNull); |
| 257 expect(scanner.lastMatch, isNotNull); |
| 258 expect(scanner.position, equals(7)); |
| 259 }); |
| 260 |
| 261 test("scan returns false and sets lastMatch to null", () { |
| 262 expect(scanner.scan(new RegExp('.')), isFalse); |
| 263 expect(scanner.lastMatch, isNull); |
| 264 expect(scanner.position, equals(7)); |
| 265 }); |
| 266 |
| 267 test("expect throws a FormatException and sets lastMatch to null", () { |
| 268 expect(() => scanner.expect(new RegExp('.')), throwsFormatException); |
| 269 expect(scanner.lastMatch, isNull); |
| 270 expect(scanner.position, equals(7)); |
| 271 }); |
| 272 |
| 273 test("matches returns false sets lastMatch to null", () { |
| 274 expect(scanner.matches(new RegExp('.')), isFalse); |
| 275 expect(scanner.lastMatch, isNull); |
| 276 expect(scanner.position, equals(7)); |
| 277 }); |
| 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 |
| 291 test('setting position to 1 moves the cursor backward', () { |
| 292 scanner.position = 1; |
| 293 expect(scanner.position, equals(1)); |
| 294 expect(scanner.rest, equals('oo bar')); |
| 295 |
| 296 expect(scanner.scan(new RegExp('oo.')), isTrue); |
| 297 expect(scanner.lastMatch[0], equals('oo ')); |
| 298 expect(scanner.position, equals(4)); |
| 299 expect(scanner.rest, equals('bar')); |
| 300 }); |
| 301 |
| 302 test('setting position beyond the string throws an ArgumentError', () { |
| 303 expect(() { |
| 304 scanner.position = 8; |
| 305 }, throwsArgumentError); |
| 306 }); |
| 307 |
| 308 test('setting position to -1 throws an ArgumentError', () { |
| 309 expect(() { |
| 310 scanner.position = -1; |
| 311 }, throwsArgumentError); |
| 312 }); |
| 313 }); |
| 314 |
| 315 group('a scanner constructed with a custom position', () { |
| 316 test('starts scanning from that position', () { |
| 317 var scanner = new StringScanner('foo bar', position: 1); |
| 318 expect(scanner.position, equals(1)); |
| 319 expect(scanner.rest, equals('oo bar')); |
| 320 |
| 321 expect(scanner.scan(new RegExp('oo.')), isTrue); |
| 322 expect(scanner.lastMatch[0], equals('oo ')); |
| 323 expect(scanner.position, equals(4)); |
| 324 expect(scanner.rest, equals('bar')); |
| 325 }); |
| 326 |
| 327 test('throws an ArgumentError if the position is -1', () { |
| 328 expect(() => new StringScanner('foo bar', position: -1), |
| 329 throwsArgumentError); |
| 330 }); |
| 331 |
| 332 test('throws an ArgumentError if the position is beyond the string', () { |
| 333 expect( |
| 334 () => new StringScanner('foo bar', position: 8), throwsArgumentError); |
| 335 }); |
| 336 }); |
| 337 } |
OLD | NEW |