| 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.error_test; | |
| 6 | |
| 7 import 'package:string_scanner/string_scanner.dart'; | |
| 8 import 'package:unittest/unittest.dart'; | |
| 9 | |
| 10 import 'utils.dart'; | |
| 11 | |
| 12 void main() { | |
| 13 test('defaults to the last match', () { | |
| 14 var scanner = new StringScanner('foo bar baz'); | |
| 15 scanner.expect('foo '); | |
| 16 scanner.expect('bar'); | |
| 17 expect(() => scanner.error('oh no!'), throwsStringScannerException('bar')); | |
| 18 }); | |
| 19 | |
| 20 group("with match", () { | |
| 21 test('supports an earlier match', () { | |
| 22 var scanner = new StringScanner('foo bar baz'); | |
| 23 scanner.expect('foo '); | |
| 24 var match = scanner.lastMatch; | |
| 25 scanner.expect('bar'); | |
| 26 expect(() => scanner.error('oh no!', match: match), | |
| 27 throwsStringScannerException('foo ')); | |
| 28 }); | |
| 29 | |
| 30 test('supports a match on a previous line', () { | |
| 31 var scanner = new StringScanner('foo bar baz\ndo re mi\nearth fire water')
; | |
| 32 scanner.expect('foo bar baz\ndo '); | |
| 33 scanner.expect('re'); | |
| 34 var match = scanner.lastMatch; | |
| 35 scanner.expect(' mi\nearth '); | |
| 36 expect(() => scanner.error('oh no!', match: match), | |
| 37 throwsStringScannerException('re')); | |
| 38 }); | |
| 39 | |
| 40 test('supports a multiline match', () { | |
| 41 var scanner = new StringScanner('foo bar baz\ndo re mi\nearth fire water')
; | |
| 42 scanner.expect('foo bar '); | |
| 43 scanner.expect('baz\ndo'); | |
| 44 var match = scanner.lastMatch; | |
| 45 scanner.expect(' re mi'); | |
| 46 expect(() => scanner.error('oh no!', match: match), | |
| 47 throwsStringScannerException('baz\ndo')); | |
| 48 }); | |
| 49 | |
| 50 test('supports a match after position', () { | |
| 51 var scanner = new StringScanner('foo bar baz'); | |
| 52 scanner.expect('foo '); | |
| 53 scanner.expect('bar'); | |
| 54 var match = scanner.lastMatch; | |
| 55 scanner.position = 0; | |
| 56 expect(() => scanner.error('oh no!', match: match), | |
| 57 throwsStringScannerException('bar')); | |
| 58 }); | |
| 59 }); | |
| 60 | |
| 61 group("with position and/or length", () { | |
| 62 test('defaults to length 1', () { | |
| 63 var scanner = new StringScanner('foo bar baz'); | |
| 64 scanner.expect('foo '); | |
| 65 expect(() => scanner.error('oh no!', position: 1), | |
| 66 throwsStringScannerException('o')); | |
| 67 }); | |
| 68 | |
| 69 test('defaults to the current position', () { | |
| 70 var scanner = new StringScanner('foo bar baz'); | |
| 71 scanner.expect('foo '); | |
| 72 expect(() => scanner.error('oh no!', length: 3), | |
| 73 throwsStringScannerException('bar')); | |
| 74 }); | |
| 75 | |
| 76 test('supports an earlier position', () { | |
| 77 var scanner = new StringScanner('foo bar baz'); | |
| 78 scanner.expect('foo '); | |
| 79 expect(() => scanner.error('oh no!', position: 1, length: 2), | |
| 80 throwsStringScannerException('oo')); | |
| 81 }); | |
| 82 | |
| 83 test('supports a position on a previous line', () { | |
| 84 var scanner = new StringScanner('foo bar baz\ndo re mi\nearth fire water')
; | |
| 85 scanner.expect('foo bar baz\ndo re mi\nearth'); | |
| 86 expect(() => scanner.error('oh no!', position: 15, length: 2), | |
| 87 throwsStringScannerException('re')); | |
| 88 }); | |
| 89 | |
| 90 test('supports a multiline length', () { | |
| 91 var scanner = new StringScanner('foo bar baz\ndo re mi\nearth fire water')
; | |
| 92 scanner.expect('foo bar baz\ndo re mi\nearth'); | |
| 93 expect(() => scanner.error('oh no!', position: 8, length: 8), | |
| 94 throwsStringScannerException('baz\ndo r')); | |
| 95 }); | |
| 96 | |
| 97 test('supports a position after the current one', () { | |
| 98 var scanner = new StringScanner('foo bar baz'); | |
| 99 expect(() => scanner.error('oh no!', position: 4, length: 3), | |
| 100 throwsStringScannerException('bar')); | |
| 101 }); | |
| 102 | |
| 103 test('supports a length of zero', () { | |
| 104 var scanner = new StringScanner('foo bar baz'); | |
| 105 expect(() => scanner.error('oh no!', position: 4, length: 0), | |
| 106 throwsStringScannerException('')); | |
| 107 }); | |
| 108 }); | |
| 109 | |
| 110 group("argument errors", () { | |
| 111 var scanner; | |
| 112 setUp(() { | |
| 113 scanner = new StringScanner('foo bar baz'); | |
| 114 scanner.scan('foo'); | |
| 115 }); | |
| 116 | |
| 117 test("if match is passed with position", () { | |
| 118 expect( | |
| 119 () => scanner.error("oh no!", match: scanner.lastMatch, position: 1), | |
| 120 throwsArgumentError); | |
| 121 }); | |
| 122 | |
| 123 test("if match is passed with length", () { | |
| 124 expect( | |
| 125 () => scanner.error("oh no!", match: scanner.lastMatch, length: 1), | |
| 126 throwsArgumentError); | |
| 127 }); | |
| 128 | |
| 129 test("if position is negative", () { | |
| 130 expect(() => scanner.error("oh no!", position: -1), throwsArgumentError); | |
| 131 }); | |
| 132 | |
| 133 test("if position is outside the string", () { | |
| 134 expect(() => scanner.error("oh no!", position: 100), throwsArgumentError); | |
| 135 }); | |
| 136 | |
| 137 test("if position + length is outside the string", () { | |
| 138 expect(() => scanner.error("oh no!", position: 7, length: 7), | |
| 139 throwsArgumentError); | |
| 140 }); | |
| 141 | |
| 142 test("if length is negative", () { | |
| 143 expect(() => scanner.error("oh no!", length: -1), throwsArgumentError); | |
| 144 }); | |
| 145 }); | |
| 146 } | |
| OLD | NEW |