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