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 shelf.string_scanner_test; |
| 6 |
| 7 import 'package:shelf/src/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 }); |
| 20 |
| 21 test('rest is empty', () { |
| 22 expect(scanner.rest, isEmpty); |
| 23 }); |
| 24 |
| 25 test('lastMatch is null', () { |
| 26 expect(scanner.lastMatch, isNull); |
| 27 }); |
| 28 |
| 29 test('position is zero', () { |
| 30 expect(scanner.position, equals(0)); |
| 31 }); |
| 32 |
| 33 test("scan returns false and doesn't change the state", () { |
| 34 expect(scanner.scan(new RegExp('.')), isFalse); |
| 35 expect(scanner.lastMatch, isNull); |
| 36 expect(scanner.position, equals(0)); |
| 37 }); |
| 38 |
| 39 test("expect throws a FormatException and doesn't change the state", () { |
| 40 expect(() => scanner.expect(new RegExp('.'), 'error'), |
| 41 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 }); |
| 74 |
| 75 test('rest is the whole string', () { |
| 76 expect(scanner.rest, equals('foo bar')); |
| 77 }); |
| 78 |
| 79 test('lastMatch is null', () { |
| 80 expect(scanner.lastMatch, isNull); |
| 81 }); |
| 82 |
| 83 test('position is zero', () { |
| 84 expect(scanner.position, equals(0)); |
| 85 }); |
| 86 |
| 87 test("a matching scan returns true and changes the state", () { |
| 88 expect(scanner.scan(new RegExp('f(..)')), isTrue); |
| 89 expect(scanner.lastMatch[1], equals('oo')); |
| 90 expect(scanner.position, equals(3)); |
| 91 expect(scanner.rest, equals(' bar')); |
| 92 }); |
| 93 |
| 94 test("a non-matching scan returns false and sets lastMatch to null", () { |
| 95 expect(scanner.matches(new RegExp('f(..)')), isTrue); |
| 96 expect(scanner.lastMatch, isNotNull); |
| 97 |
| 98 expect(scanner.scan(new RegExp('b(..)')), isFalse); |
| 99 expect(scanner.lastMatch, isNull); |
| 100 expect(scanner.position, equals(0)); |
| 101 expect(scanner.rest, equals('foo bar')); |
| 102 }); |
| 103 |
| 104 test("a matching expect changes the state", () { |
| 105 scanner.expect(new RegExp('f(..)'), 'error'); |
| 106 expect(scanner.lastMatch[1], equals('oo')); |
| 107 expect(scanner.position, equals(3)); |
| 108 expect(scanner.rest, equals(' bar')); |
| 109 }); |
| 110 |
| 111 test("a non-matching expect throws a FormatException and sets lastMatch to " |
| 112 "null", () { |
| 113 expect(scanner.matches(new RegExp('f(..)')), isTrue); |
| 114 expect(scanner.lastMatch, isNotNull); |
| 115 |
| 116 expect(() => scanner.expect(new RegExp('b(..)'), 'error'), |
| 117 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 }); |
| 180 }); |
| 181 |
| 182 group('at the end of a string', () { |
| 183 var scanner; |
| 184 setUp(() { |
| 185 scanner = new StringScanner('foo bar'); |
| 186 expect(scanner.scan('foo bar'), isTrue); |
| 187 }); |
| 188 |
| 189 test('is done', () { |
| 190 expect(scanner.isDone, isTrue); |
| 191 }); |
| 192 |
| 193 test('rest is empty', () { |
| 194 expect(scanner.rest, isEmpty); |
| 195 }); |
| 196 |
| 197 test('position is zero', () { |
| 198 expect(scanner.position, equals(7)); |
| 199 }); |
| 200 |
| 201 test("scan returns false and sets lastMatch to null", () { |
| 202 expect(scanner.scan(new RegExp('.')), isFalse); |
| 203 expect(scanner.lastMatch, isNull); |
| 204 expect(scanner.position, equals(7)); |
| 205 }); |
| 206 |
| 207 test("expect throws a FormatException and sets lastMatch to null", () { |
| 208 expect(() => scanner.expect(new RegExp('.'), 'error'), |
| 209 throwsFormatException); |
| 210 expect(scanner.lastMatch, isNull); |
| 211 expect(scanner.position, equals(7)); |
| 212 }); |
| 213 |
| 214 test("matches returns false sets lastMatch to null", () { |
| 215 expect(scanner.matches(new RegExp('.')), isFalse); |
| 216 expect(scanner.lastMatch, isNull); |
| 217 expect(scanner.position, equals(7)); |
| 218 }); |
| 219 |
| 220 test('setting position to 1 moves the cursor backward', () { |
| 221 scanner.position = 1; |
| 222 expect(scanner.position, equals(1)); |
| 223 expect(scanner.rest, equals('oo bar')); |
| 224 |
| 225 expect(scanner.scan(new RegExp('oo.')), isTrue); |
| 226 expect(scanner.lastMatch[0], equals('oo ')); |
| 227 expect(scanner.position, equals(4)); |
| 228 expect(scanner.rest, equals('bar')); |
| 229 }); |
| 230 |
| 231 test('setting position beyond the string throws an ArgumentError', () { |
| 232 expect(() { |
| 233 scanner.position = 8; |
| 234 }, throwsArgumentError); |
| 235 }); |
| 236 |
| 237 test('setting position to -1 throws an ArgumentError', () { |
| 238 expect(() { |
| 239 scanner.position = -1; |
| 240 }, throwsArgumentError); |
| 241 }); |
| 242 }); |
| 243 |
| 244 group('a scanner constructed with a custom position', () { |
| 245 test('starts scanning from that position', () { |
| 246 var scanner = new StringScanner('foo bar', position: 1); |
| 247 expect(scanner.position, equals(1)); |
| 248 expect(scanner.rest, equals('oo bar')); |
| 249 |
| 250 expect(scanner.scan(new RegExp('oo.')), isTrue); |
| 251 expect(scanner.lastMatch[0], equals('oo ')); |
| 252 expect(scanner.position, equals(4)); |
| 253 expect(scanner.rest, equals('bar')); |
| 254 }); |
| 255 |
| 256 test('throws an ArgumentError if the position is -1', () { |
| 257 expect(() => new StringScanner('foo bar', position: -1), |
| 258 throwsArgumentError); |
| 259 }); |
| 260 |
| 261 test('throws an ArgumentError if the position is beyond the string', () { |
| 262 expect(() => new StringScanner('foo bar', position: 8), |
| 263 throwsArgumentError); |
| 264 }); |
| 265 }); |
| 266 } |
OLD | NEW |