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