| 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:unittest/unittest.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("scan returns false and doesn't change the state", () { |
| 35 expect(scanner.scan(new RegExp('.')), isFalse); |
| 36 expect(scanner.lastMatch, isNull); |
| 37 expect(scanner.position, equals(0)); |
| 38 }); |
| 39 |
| 40 test("expect throws a FormatException and doesn't change the state", () { |
| 41 expect(() => scanner.expect(new RegExp('.')), throwsFormatException); |
| 42 expect(scanner.lastMatch, isNull); |
| 43 expect(scanner.position, equals(0)); |
| 44 }); |
| 45 |
| 46 test("matches returns false and doesn't change the state", () { |
| 47 expect(scanner.matches(new RegExp('.')), isFalse); |
| 48 expect(scanner.lastMatch, isNull); |
| 49 expect(scanner.position, equals(0)); |
| 50 }); |
| 51 |
| 52 test('setting position to 1 throws an ArgumentError', () { |
| 53 expect(() { |
| 54 scanner.position = 1; |
| 55 }, throwsArgumentError); |
| 56 }); |
| 57 |
| 58 test('setting position to -1 throws an ArgumentError', () { |
| 59 expect(() { |
| 60 scanner.position = -1; |
| 61 }, throwsArgumentError); |
| 62 }); |
| 63 }); |
| 64 |
| 65 group('at the beginning of a string', () { |
| 66 var scanner; |
| 67 setUp(() { |
| 68 scanner = new StringScanner('foo bar'); |
| 69 }); |
| 70 |
| 71 test('is not done', () { |
| 72 expect(scanner.isDone, isFalse); |
| 73 expect(scanner.expectDone, throwsFormatException); |
| 74 }); |
| 75 |
| 76 test('rest is the whole string', () { |
| 77 expect(scanner.rest, equals('foo bar')); |
| 78 }); |
| 79 |
| 80 test('lastMatch is null', () { |
| 81 expect(scanner.lastMatch, isNull); |
| 82 }); |
| 83 |
| 84 test('position is zero', () { |
| 85 expect(scanner.position, equals(0)); |
| 86 }); |
| 87 |
| 88 test("a matching scan returns true and changes the state", () { |
| 89 expect(scanner.scan(new RegExp('f(..)')), isTrue); |
| 90 expect(scanner.lastMatch[1], equals('oo')); |
| 91 expect(scanner.position, equals(3)); |
| 92 expect(scanner.rest, equals(' bar')); |
| 93 }); |
| 94 |
| 95 test("a non-matching scan returns false and sets lastMatch to null", () { |
| 96 expect(scanner.matches(new RegExp('f(..)')), isTrue); |
| 97 expect(scanner.lastMatch, isNotNull); |
| 98 |
| 99 expect(scanner.scan(new RegExp('b(..)')), isFalse); |
| 100 expect(scanner.lastMatch, isNull); |
| 101 expect(scanner.position, equals(0)); |
| 102 expect(scanner.rest, equals('foo bar')); |
| 103 }); |
| 104 |
| 105 test("a matching expect changes the state", () { |
| 106 scanner.expect(new RegExp('f(..)')); |
| 107 expect(scanner.lastMatch[1], equals('oo')); |
| 108 expect(scanner.position, equals(3)); |
| 109 expect(scanner.rest, equals(' bar')); |
| 110 }); |
| 111 |
| 112 test("a non-matching expect throws a FormatException and sets lastMatch to " |
| 113 "null", () { |
| 114 expect(scanner.matches(new RegExp('f(..)')), isTrue); |
| 115 expect(scanner.lastMatch, isNotNull); |
| 116 |
| 117 expect(() => scanner.expect(new RegExp('b(..)')), throwsFormatException); |
| 118 expect(scanner.lastMatch, isNull); |
| 119 expect(scanner.position, equals(0)); |
| 120 expect(scanner.rest, equals('foo bar')); |
| 121 }); |
| 122 |
| 123 test("a matching matches returns true and only changes lastMatch", () { |
| 124 expect(scanner.matches(new RegExp('f(..)')), isTrue); |
| 125 expect(scanner.lastMatch[1], equals('oo')); |
| 126 expect(scanner.position, equals(0)); |
| 127 expect(scanner.rest, equals('foo bar')); |
| 128 }); |
| 129 |
| 130 test("a non-matching matches returns false and doesn't change the state", |
| 131 () { |
| 132 expect(scanner.matches(new RegExp('b(..)')), isFalse); |
| 133 expect(scanner.lastMatch, isNull); |
| 134 expect(scanner.position, equals(0)); |
| 135 expect(scanner.rest, equals('foo bar')); |
| 136 }); |
| 137 |
| 138 test('setting position to 1 moves the cursor forward', () { |
| 139 scanner.position = 1; |
| 140 expect(scanner.position, equals(1)); |
| 141 expect(scanner.rest, equals('oo bar')); |
| 142 |
| 143 expect(scanner.scan(new RegExp('oo.')), isTrue); |
| 144 expect(scanner.lastMatch[0], equals('oo ')); |
| 145 expect(scanner.position, equals(4)); |
| 146 expect(scanner.rest, equals('bar')); |
| 147 }); |
| 148 |
| 149 test('setting position beyond the string throws an ArgumentError', () { |
| 150 expect(() { |
| 151 scanner.position = 8; |
| 152 }, throwsArgumentError); |
| 153 }); |
| 154 |
| 155 test('setting position to -1 throws an ArgumentError', () { |
| 156 expect(() { |
| 157 scanner.position = -1; |
| 158 }, throwsArgumentError); |
| 159 }); |
| 160 |
| 161 test('scan accepts any Pattern', () { |
| 162 expect(scanner.scan('foo'), isTrue); |
| 163 expect(scanner.lastMatch[0], equals('foo')); |
| 164 expect(scanner.position, equals(3)); |
| 165 expect(scanner.rest, equals(' bar')); |
| 166 }); |
| 167 |
| 168 test('scans multiple times', () { |
| 169 expect(scanner.scan(new RegExp('f(..)')), isTrue); |
| 170 expect(scanner.lastMatch[1], equals('oo')); |
| 171 expect(scanner.position, equals(3)); |
| 172 expect(scanner.rest, equals(' bar')); |
| 173 |
| 174 expect(scanner.scan(new RegExp(' b(..)')), isTrue); |
| 175 expect(scanner.lastMatch[1], equals('ar')); |
| 176 expect(scanner.position, equals(7)); |
| 177 expect(scanner.rest, equals('')); |
| 178 expect(scanner.isDone, isTrue); |
| 179 expect(scanner.expectDone, isNot(throwsFormatException)); |
| 180 }); |
| 181 }); |
| 182 |
| 183 group('at the end of a string', () { |
| 184 var scanner; |
| 185 setUp(() { |
| 186 scanner = new StringScanner('foo bar'); |
| 187 expect(scanner.scan('foo bar'), isTrue); |
| 188 }); |
| 189 |
| 190 test('is done', () { |
| 191 expect(scanner.isDone, isTrue); |
| 192 expect(scanner.expectDone, isNot(throwsFormatException)); |
| 193 }); |
| 194 |
| 195 test('rest is empty', () { |
| 196 expect(scanner.rest, isEmpty); |
| 197 }); |
| 198 |
| 199 test('position is zero', () { |
| 200 expect(scanner.position, equals(7)); |
| 201 }); |
| 202 |
| 203 test("scan returns false and sets lastMatch to null", () { |
| 204 expect(scanner.scan(new RegExp('.')), isFalse); |
| 205 expect(scanner.lastMatch, isNull); |
| 206 expect(scanner.position, equals(7)); |
| 207 }); |
| 208 |
| 209 test("expect throws a FormatException and sets lastMatch to null", () { |
| 210 expect(() => scanner.expect(new RegExp('.')), throwsFormatException); |
| 211 expect(scanner.lastMatch, isNull); |
| 212 expect(scanner.position, equals(7)); |
| 213 }); |
| 214 |
| 215 test("matches returns false sets lastMatch to null", () { |
| 216 expect(scanner.matches(new RegExp('.')), isFalse); |
| 217 expect(scanner.lastMatch, isNull); |
| 218 expect(scanner.position, equals(7)); |
| 219 }); |
| 220 |
| 221 test('setting position to 1 moves the cursor backward', () { |
| 222 scanner.position = 1; |
| 223 expect(scanner.position, equals(1)); |
| 224 expect(scanner.rest, equals('oo bar')); |
| 225 |
| 226 expect(scanner.scan(new RegExp('oo.')), isTrue); |
| 227 expect(scanner.lastMatch[0], equals('oo ')); |
| 228 expect(scanner.position, equals(4)); |
| 229 expect(scanner.rest, equals('bar')); |
| 230 }); |
| 231 |
| 232 test('setting position beyond the string throws an ArgumentError', () { |
| 233 expect(() { |
| 234 scanner.position = 8; |
| 235 }, throwsArgumentError); |
| 236 }); |
| 237 |
| 238 test('setting position to -1 throws an ArgumentError', () { |
| 239 expect(() { |
| 240 scanner.position = -1; |
| 241 }, throwsArgumentError); |
| 242 }); |
| 243 }); |
| 244 |
| 245 group('a scanner constructed with a custom position', () { |
| 246 test('starts scanning from that position', () { |
| 247 var scanner = new StringScanner('foo bar', position: 1); |
| 248 expect(scanner.position, equals(1)); |
| 249 expect(scanner.rest, equals('oo bar')); |
| 250 |
| 251 expect(scanner.scan(new RegExp('oo.')), isTrue); |
| 252 expect(scanner.lastMatch[0], equals('oo ')); |
| 253 expect(scanner.position, equals(4)); |
| 254 expect(scanner.rest, equals('bar')); |
| 255 }); |
| 256 |
| 257 test('throws an ArgumentError if the position is -1', () { |
| 258 expect(() => new StringScanner('foo bar', position: -1), |
| 259 throwsArgumentError); |
| 260 }); |
| 261 |
| 262 test('throws an ArgumentError if the position is beyond the string', () { |
| 263 expect(() => new StringScanner('foo bar', position: 8), |
| 264 throwsArgumentError); |
| 265 }); |
| 266 }); |
| 267 } |
| OLD | NEW |