| 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.line_scanner_test; | 5 library string_scanner.line_scanner_test; |
| 6 | 6 |
| 7 import 'package:string_scanner/string_scanner.dart'; | 7 import 'package:string_scanner/string_scanner.dart'; |
| 8 import 'package:test/test.dart'; | 8 import 'package:test/test.dart'; |
| 9 | 9 |
| 10 void main() { | 10 void main() { |
| 11 var scanner; | 11 var scanner; |
| 12 setUp(() { | 12 setUp(() { |
| 13 scanner = new LineScanner('foo\nbar\nbaz'); | 13 scanner = new LineScanner('foo\nbar\r\nbaz'); |
| 14 }); | 14 }); |
| 15 | 15 |
| 16 test('begins with line and column 0', () { | 16 test('begins with line and column 0', () { |
| 17 expect(scanner.line, equals(0)); | 17 expect(scanner.line, equals(0)); |
| 18 expect(scanner.column, equals(0)); | 18 expect(scanner.column, equals(0)); |
| 19 }); | 19 }); |
| 20 | 20 |
| 21 group("scan()", () { | 21 group("scan()", () { |
| 22 test("consuming no newlines increases the column but not the line", () { | 22 test("consuming no newlines increases the column but not the line", () { |
| 23 scanner.scan('foo'); | 23 scanner.scan('foo'); |
| 24 expect(scanner.line, equals(0)); | 24 expect(scanner.line, equals(0)); |
| 25 expect(scanner.column, equals(3)); | 25 expect(scanner.column, equals(3)); |
| 26 }); | 26 }); |
| 27 | 27 |
| 28 test("consuming a newline resets the column and increases the line", () { | 28 test("consuming a newline resets the column and increases the line", () { |
| 29 scanner.expect('foo\nba'); | 29 scanner.expect('foo\nba'); |
| 30 expect(scanner.line, equals(1)); | 30 expect(scanner.line, equals(1)); |
| 31 expect(scanner.column, equals(2)); | 31 expect(scanner.column, equals(2)); |
| 32 }); | 32 }); |
| 33 | 33 |
| 34 test("consuming multiple newlines resets the column and increases the line", | 34 test("consuming multiple newlines resets the column and increases the line", |
| 35 () { | 35 () { |
| 36 scanner.expect('foo\nbar\nb'); | 36 scanner.expect('foo\nbar\r\nb'); |
| 37 expect(scanner.line, equals(2)); | 37 expect(scanner.line, equals(2)); |
| 38 expect(scanner.column, equals(1)); | 38 expect(scanner.column, equals(1)); |
| 39 }); | 39 }); |
| 40 |
| 41 test("consuming halfway through a CR LF doesn't count as a line", () { |
| 42 scanner.expect('foo\nbar\r'); |
| 43 expect(scanner.line, equals(1)); |
| 44 expect(scanner.column, equals(4)); |
| 45 |
| 46 scanner.expect('\nb'); |
| 47 expect(scanner.line, equals(2)); |
| 48 expect(scanner.column, equals(1)); |
| 49 }); |
| 40 }); | 50 }); |
| 41 | 51 |
| 42 group("readChar()", () { | 52 group("readChar()", () { |
| 43 test("on a non-newline character increases the column but not the line", | 53 test("on a non-newline character increases the column but not the line", |
| 44 () { | 54 () { |
| 45 scanner.readChar(); | 55 scanner.readChar(); |
| 46 expect(scanner.line, equals(0)); | 56 expect(scanner.line, equals(0)); |
| 47 expect(scanner.column, equals(1)); | 57 expect(scanner.column, equals(1)); |
| 48 }); | 58 }); |
| 49 | 59 |
| 50 test("consuming a newline resets the column and increases the line", () { | 60 test("consuming a newline resets the column and increases the line", () { |
| 51 scanner.expect('foo'); | 61 scanner.expect('foo'); |
| 52 expect(scanner.line, equals(0)); | 62 expect(scanner.line, equals(0)); |
| 53 expect(scanner.column, equals(3)); | 63 expect(scanner.column, equals(3)); |
| 54 | 64 |
| 55 scanner.readChar(); | 65 scanner.readChar(); |
| 56 expect(scanner.line, equals(1)); | 66 expect(scanner.line, equals(1)); |
| 57 expect(scanner.column, equals(0)); | 67 expect(scanner.column, equals(0)); |
| 58 }); | 68 }); |
| 69 |
| 70 test("consuming halfway through a CR LF doesn't count as a line", () { |
| 71 scanner.expect('foo\nbar'); |
| 72 expect(scanner.line, equals(1)); |
| 73 expect(scanner.column, equals(3)); |
| 74 |
| 75 scanner.readChar(); |
| 76 expect(scanner.line, equals(1)); |
| 77 expect(scanner.column, equals(4)); |
| 78 |
| 79 scanner.readChar(); |
| 80 expect(scanner.line, equals(2)); |
| 81 expect(scanner.column, equals(0)); |
| 82 }); |
| 59 }); | 83 }); |
| 60 | 84 |
| 61 group("position=", () { | 85 group("position=", () { |
| 62 test("forward through newlines sets the line and column", () { | 86 test("forward through newlines sets the line and column", () { |
| 63 scanner.position = 9; // "foo\nbar\nb" | 87 scanner.position = 10; // "foo\nbar\r\nb" |
| 64 expect(scanner.line, equals(2)); | 88 expect(scanner.line, equals(2)); |
| 65 expect(scanner.column, equals(1)); | 89 expect(scanner.column, equals(1)); |
| 66 }); | 90 }); |
| 67 | 91 |
| 68 test("forward through no newlines sets the column", () { | 92 test("forward through no newlines sets the column", () { |
| 69 scanner.position = 2; // "fo" | 93 scanner.position = 2; // "fo" |
| 70 expect(scanner.line, equals(0)); | 94 expect(scanner.line, equals(0)); |
| 71 expect(scanner.column, equals(2)); | 95 expect(scanner.column, equals(2)); |
| 72 }); | 96 }); |
| 73 | 97 |
| 74 test("backward through newlines sets the line and column", () { | 98 test("backward through newlines sets the line and column", () { |
| 75 scanner.scan("foo\nbar\nbaz"); | 99 scanner.scan("foo\nbar\r\nbaz"); |
| 76 scanner.position = 2; // "fo" | 100 scanner.position = 2; // "fo" |
| 77 expect(scanner.line, equals(0)); | 101 expect(scanner.line, equals(0)); |
| 78 expect(scanner.column, equals(2)); | 102 expect(scanner.column, equals(2)); |
| 79 }); | 103 }); |
| 80 | 104 |
| 81 test("backward through no newlines sets the column", () { | 105 test("backward through no newlines sets the column", () { |
| 82 scanner.scan("foo\nbar\nbaz"); | 106 scanner.scan("foo\nbar\r\nbaz"); |
| 83 scanner.position = 9; // "foo\nbar\nb" | 107 scanner.position = 10; // "foo\nbar\r\nb" |
| 84 expect(scanner.line, equals(2)); | 108 expect(scanner.line, equals(2)); |
| 85 expect(scanner.column, equals(1)); | 109 expect(scanner.column, equals(1)); |
| 86 }); | 110 }); |
| 111 |
| 112 test("forward halfway through a CR LF doesn't count as a line", () { |
| 113 scanner.position = 8; // "foo\nbar\r" |
| 114 expect(scanner.line, equals(1)); |
| 115 expect(scanner.column, equals(4)); |
| 116 }); |
| 87 }); | 117 }); |
| 88 | 118 |
| 89 test("state= restores the line, column, and position", () { | 119 test("state= restores the line, column, and position", () { |
| 90 scanner.scan('foo\nb'); | 120 scanner.scan('foo\nb'); |
| 91 var state = scanner.state; | 121 var state = scanner.state; |
| 92 | 122 |
| 93 scanner.scan('ar\nba'); | 123 scanner.scan('ar\nba'); |
| 94 scanner.state = state; | 124 scanner.state = state; |
| 95 expect(scanner.rest, equals('ar\nbaz')); | 125 expect(scanner.rest, equals('ar\r\nbaz')); |
| 96 expect(scanner.line, equals(1)); | 126 expect(scanner.line, equals(1)); |
| 97 expect(scanner.column, equals(1)); | 127 expect(scanner.column, equals(1)); |
| 98 }); | 128 }); |
| 99 | 129 |
| 100 test("state= rejects a foreign state", () { | 130 test("state= rejects a foreign state", () { |
| 101 scanner.scan('foo\nb'); | 131 scanner.scan('foo\nb'); |
| 102 | 132 |
| 103 expect(() => new LineScanner(scanner.string).state = scanner.state, | 133 expect(() => new LineScanner(scanner.string).state = scanner.state, |
| 104 throwsArgumentError); | 134 throwsArgumentError); |
| 105 }); | 135 }); |
| 106 } | 136 } |
| OLD | NEW |