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