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