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_format_test; | 5 library string_scanner.expect_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'; |
| 11 |
10 void main() { | 12 void main() { |
11 test('points to the first unconsumed character', () { | 13 test('points to the first unconsumed character', () { |
12 var scanner = new StringScanner('foo bar baz'); | 14 var scanner = new StringScanner('foo bar baz'); |
13 scanner.expect('foo '); | 15 scanner.expect('foo '); |
14 expect(() => scanner.expect('foo'), throwsFormattedError(''' | 16 expect(() => scanner.expect('foo'), throwsFormattedError(''' |
15 Expected "foo" on line 1, column 5. | 17 Error on line 1, column 5: expected "foo". |
16 foo bar baz | 18 foo bar baz |
17 ^''')); | 19 ^''')); |
18 }); | 20 }); |
19 | 21 |
20 test('prints the correct line', () { | 22 test('prints the correct line', () { |
21 var scanner = new StringScanner('foo bar baz\ndo re mi\nearth fire water'); | 23 var scanner = new StringScanner('foo bar baz\ndo re mi\nearth fire water'); |
22 scanner.expect('foo bar baz\ndo '); | 24 scanner.expect('foo bar baz\ndo '); |
23 expect(() => scanner.expect('foo'), throwsFormattedError(''' | 25 expect(() => scanner.expect('foo'), throwsFormattedError(''' |
24 Expected "foo" on line 2, column 4. | 26 Error on line 2, column 4: expected "foo". |
25 do re mi | 27 do re mi |
26 ^''')); | 28 ^''')); |
27 }); | 29 }); |
28 | 30 |
29 test('handles the beginning of the string correctly', () { | 31 test('handles the beginning of the string correctly', () { |
30 var scanner = new StringScanner('foo bar baz'); | 32 var scanner = new StringScanner('foo bar baz'); |
31 expect(() => scanner.expect('zap'), throwsFormattedError(''' | 33 expect(() => scanner.expect('zap'), throwsFormattedError(''' |
32 Expected "zap" on line 1, column 1. | 34 Error on line 1, column 1: expected "zap". |
33 foo bar baz | 35 foo bar baz |
34 ^''')); | 36 ^''')); |
35 }); | 37 }); |
36 | 38 |
37 test('handles the end of the string correctly', () { | 39 test('handles the end of the string correctly', () { |
38 var scanner = new StringScanner('foo bar baz'); | 40 var scanner = new StringScanner('foo bar baz'); |
39 scanner.expect('foo bar baz'); | 41 scanner.expect('foo bar baz'); |
40 expect(() => scanner.expect('bang'), throwsFormattedError(''' | 42 expect(() => scanner.expect('bang'), throwsFormattedError(''' |
41 Expected "bang" on line 1, column 12. | 43 Error on line 1, column 12: expected "bang". |
42 foo bar baz | 44 foo bar baz |
43 ^''')); | 45 ^''')); |
44 }); | 46 }); |
45 | 47 |
46 test('handles an empty string correctly', () { | 48 test('handles an empty string correctly', () { |
47 expect(() => new StringScanner('').expect('foo'), throwsFormattedError(''' | 49 expect(() => new StringScanner('').expect('foo'), throwsFormattedError(''' |
48 Expected "foo" on line 1, column 1. | 50 Error on line 1, column 1: expected "foo". |
49 | 51 |
50 ^''')); | 52 ^''')); |
51 }); | 53 }); |
52 | 54 |
53 group("expected name", () { | 55 group("expected name", () { |
54 test("uses the provided name", () { | 56 test("uses the provided name", () { |
55 expect(() => new StringScanner('').expect('foo bar', name: 'zap'), | 57 expect(() => new StringScanner('').expect('foo bar', name: 'zap'), |
56 throwsFormattedError(''' | 58 throwsFormattedError(''' |
57 Expected zap on line 1, column 1. | 59 Error on line 1, column 1: expected zap. |
58 | 60 |
59 ^''')); | 61 ^''')); |
60 }); | 62 }); |
61 | 63 |
62 test("escapes string quotes", () { | 64 test("escapes string quotes", () { |
63 expect(() => new StringScanner('').expect('foo"bar'), | 65 expect(() => new StringScanner('').expect('foo"bar'), |
64 throwsFormattedError(''' | 66 throwsFormattedError(''' |
65 Expected "foo\\"bar" on line 1, column 1. | 67 Error on line 1, column 1: expected "foo\\"bar". |
66 | 68 |
67 ^''')); | 69 ^''')); |
68 }); | 70 }); |
69 | 71 |
70 test("escapes string backslashes", () { | 72 test("escapes string backslashes", () { |
71 expect(() => new StringScanner('').expect('foo\\bar'), | 73 expect(() => new StringScanner('').expect('foo\\bar'), |
72 throwsFormattedError(''' | 74 throwsFormattedError(''' |
73 Expected "foo\\\\bar" on line 1, column 1. | 75 Error on line 1, column 1: expected "foo\\\\bar". |
74 | 76 |
75 ^''')); | 77 ^''')); |
76 }); | 78 }); |
77 | 79 |
78 test("prints PERL-style regexps", () { | 80 test("prints PERL-style regexps", () { |
79 expect(() => new StringScanner('').expect(new RegExp(r'foo')), | 81 expect(() => new StringScanner('').expect(new RegExp(r'foo')), |
80 throwsFormattedError(''' | 82 throwsFormattedError(''' |
81 Expected /foo/ on line 1, column 1. | 83 Error on line 1, column 1: expected /foo/. |
82 | 84 |
83 ^''')); | 85 ^''')); |
84 }); | 86 }); |
85 | 87 |
86 test("escape regexp forward slashes", () { | 88 test("escape regexp forward slashes", () { |
87 expect(() => new StringScanner('').expect(new RegExp(r'foo/bar')), | 89 expect(() => new StringScanner('').expect(new RegExp(r'foo/bar')), |
88 throwsFormattedError(''' | 90 throwsFormattedError(''' |
89 Expected /foo\\/bar/ on line 1, column 1. | 91 Error on line 1, column 1: expected /foo\\/bar/. |
90 | 92 |
91 ^''')); | 93 ^''')); |
92 }); | 94 }); |
93 | 95 |
94 test("does not escape regexp backslashes", () { | 96 test("does not escape regexp backslashes", () { |
95 expect(() => new StringScanner('').expect(new RegExp(r'foo\bar')), | 97 expect(() => new StringScanner('').expect(new RegExp(r'foo\bar')), |
96 throwsFormattedError(''' | 98 throwsFormattedError(''' |
97 Expected /foo\\bar/ on line 1, column 1. | 99 Error on line 1, column 1: expected /foo\\bar/. |
98 | 100 |
99 ^''')); | 101 ^''')); |
100 }); | 102 }); |
101 }); | 103 }); |
102 } | 104 } |
103 | |
104 Matcher throwsFormattedError(String format) { | |
105 return throwsA(predicate((error) { | |
106 expect(error, isFormatException); | |
107 expect(error.message, equals(format)); | |
108 return true; | |
109 })); | |
110 } | |
OLD | NEW |