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 import 'package:charcode/charcode.dart'; |
5 import 'package:string_scanner/string_scanner.dart'; | 6 import 'package:string_scanner/string_scanner.dart'; |
6 import 'package:test/test.dart'; | 7 import 'package:test/test.dart'; |
7 | 8 |
8 void main() { | 9 void main() { |
9 var scanner; | 10 var scanner; |
10 setUp(() { | 11 setUp(() { |
11 scanner = new LineScanner('foo\nbar\r\nbaz'); | 12 scanner = new LineScanner('foo\nbar\r\nbaz'); |
12 }); | 13 }); |
13 | 14 |
14 test('begins with line and column 0', () { | 15 test('begins with line and column 0', () { |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
73 scanner.readChar(); | 74 scanner.readChar(); |
74 expect(scanner.line, equals(1)); | 75 expect(scanner.line, equals(1)); |
75 expect(scanner.column, equals(4)); | 76 expect(scanner.column, equals(4)); |
76 | 77 |
77 scanner.readChar(); | 78 scanner.readChar(); |
78 expect(scanner.line, equals(2)); | 79 expect(scanner.line, equals(2)); |
79 expect(scanner.column, equals(0)); | 80 expect(scanner.column, equals(0)); |
80 }); | 81 }); |
81 }); | 82 }); |
82 | 83 |
| 84 group("scanChar()", () { |
| 85 test("on a non-newline character increases the column but not the line", |
| 86 () { |
| 87 scanner.scanChar($f); |
| 88 expect(scanner.line, equals(0)); |
| 89 expect(scanner.column, equals(1)); |
| 90 }); |
| 91 |
| 92 test("consuming a newline resets the column and increases the line", () { |
| 93 scanner.expect('foo'); |
| 94 expect(scanner.line, equals(0)); |
| 95 expect(scanner.column, equals(3)); |
| 96 |
| 97 scanner.scanChar($lf); |
| 98 expect(scanner.line, equals(1)); |
| 99 expect(scanner.column, equals(0)); |
| 100 }); |
| 101 |
| 102 test("consuming halfway through a CR LF doesn't count as a line", () { |
| 103 scanner.expect('foo\nbar'); |
| 104 expect(scanner.line, equals(1)); |
| 105 expect(scanner.column, equals(3)); |
| 106 |
| 107 scanner.scanChar($cr); |
| 108 expect(scanner.line, equals(1)); |
| 109 expect(scanner.column, equals(4)); |
| 110 |
| 111 scanner.scanChar($lf); |
| 112 expect(scanner.line, equals(2)); |
| 113 expect(scanner.column, equals(0)); |
| 114 }); |
| 115 }); |
| 116 |
83 group("position=", () { | 117 group("position=", () { |
84 test("forward through newlines sets the line and column", () { | 118 test("forward through newlines sets the line and column", () { |
85 scanner.position = 10; // "foo\nbar\r\nb" | 119 scanner.position = 10; // "foo\nbar\r\nb" |
86 expect(scanner.line, equals(2)); | 120 expect(scanner.line, equals(2)); |
87 expect(scanner.column, equals(1)); | 121 expect(scanner.column, equals(1)); |
88 }); | 122 }); |
89 | 123 |
90 test("forward through no newlines sets the column", () { | 124 test("forward through no newlines sets the column", () { |
91 scanner.position = 2; // "fo" | 125 scanner.position = 2; // "fo" |
92 expect(scanner.line, equals(0)); | 126 expect(scanner.line, equals(0)); |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
125 expect(scanner.column, equals(1)); | 159 expect(scanner.column, equals(1)); |
126 }); | 160 }); |
127 | 161 |
128 test("state= rejects a foreign state", () { | 162 test("state= rejects a foreign state", () { |
129 scanner.scan('foo\nb'); | 163 scanner.scan('foo\nb'); |
130 | 164 |
131 expect(() => new LineScanner(scanner.string).state = scanner.state, | 165 expect(() => new LineScanner(scanner.string).state = scanner.state, |
132 throwsArgumentError); | 166 throwsArgumentError); |
133 }); | 167 }); |
134 } | 168 } |
OLD | NEW |