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