OLD | NEW |
1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2015, 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 // Unittest for the [LineColumnCollector]. | 5 // Unittest for the [LineColumnCollector]. |
6 | 6 |
7 import 'package:expect/expect.dart'; | 7 import 'package:expect/expect.dart'; |
8 import 'package:compiler/src/io/code_output.dart'; | 8 import 'package:compiler/src/io/code_output.dart'; |
9 import 'package:compiler/src/io/line_column_provider.dart'; | 9 import 'package:compiler/src/io/line_column_provider.dart'; |
10 | 10 |
11 import 'output_collector.dart'; | 11 import 'output_collector.dart'; |
12 | 12 |
13 test(List events, Map<int, List<int>> expectedPositions) { | 13 test(List events, Map<int, List<int>> expectedPositions) { |
14 BufferedEventSink sink = new BufferedEventSink(); | 14 BufferedEventSink sink = new BufferedEventSink(); |
15 LineColumnProvider lineColumnProvider = new LineColumnCollector(); | 15 LineColumnProvider lineColumnProvider = new LineColumnCollector(); |
16 CodeOutput output = new StreamCodeOutput(sink, [lineColumnProvider]); | 16 CodeOutput output = new StreamCodeOutput(sink, [lineColumnProvider]); |
17 for (var event in events) { | 17 for (var event in events) { |
18 if (event is String) { | 18 if (event is String) { |
19 output.add(event); | 19 output.add(event); |
20 } else if (event is CodeBuffer) { | 20 } else if (event is CodeBuffer) { |
21 output.addBuffer(event); | 21 output.addBuffer(event); |
22 } | 22 } |
23 } | 23 } |
24 output.close(); | 24 output.close(); |
25 | 25 |
26 expectedPositions.forEach((int offset, List<int> expectedPosition) { | 26 expectedPositions.forEach((int offset, List<int> expectedPosition) { |
27 if (expectedPosition == null) { | 27 if (expectedPosition == null) { |
28 Expect.throws(() => lineColumnProvider.getLine(offset), | 28 Expect.throws( |
29 (e) => true, | 29 () => lineColumnProvider.getLine(offset), |
30 'Expected out-of-bounds offset: $offset\n' | 30 (e) => true, |
31 'text:"""${sink.text}"""\n' | 31 'Expected out-of-bounds offset: $offset\n' |
32 'lineColumnProvider:$lineColumnProvider'); | 32 'text:"""${sink.text}"""\n' |
| 33 'lineColumnProvider:$lineColumnProvider'); |
33 } else { | 34 } else { |
34 int line = lineColumnProvider.getLine(offset); | 35 int line = lineColumnProvider.getLine(offset); |
35 int column = lineColumnProvider.getColumn(line, offset); | 36 int column = lineColumnProvider.getColumn(line, offset); |
36 Expect.equals(expectedPosition[0], line, | 37 Expect.equals( |
| 38 expectedPosition[0], |
| 39 line, |
37 'Unexpected result: $offset -> $expectedPosition = [$line,$column]\n' | 40 'Unexpected result: $offset -> $expectedPosition = [$line,$column]\n' |
38 'text:"""${sink.text}"""\n' | 41 'text:"""${sink.text}"""\n' |
39 'lineColumnProvider:$lineColumnProvider'); | 42 'lineColumnProvider:$lineColumnProvider'); |
40 Expect.equals(expectedPosition[1], column, | 43 Expect.equals( |
| 44 expectedPosition[1], |
| 45 column, |
41 'Unexpected result: $offset -> $expectedPosition = [$line,$column]\n' | 46 'Unexpected result: $offset -> $expectedPosition = [$line,$column]\n' |
42 'text:"""${sink.text}"""\n' | 47 'text:"""${sink.text}"""\n' |
43 'lineColumnProvider:$lineColumnProvider'); | 48 'lineColumnProvider:$lineColumnProvider'); |
44 } | 49 } |
45 }); | 50 }); |
46 } | 51 } |
47 | 52 |
48 main() { | 53 main() { |
49 test([""], {0: [0, 0], 1: null}); | 54 test([ |
| 55 "" |
| 56 ], { |
| 57 0: [0, 0], |
| 58 1: null |
| 59 }); |
50 | 60 |
51 test([" "], {0: [0, 0], 1: [0, 1], 2: null}); | 61 test([ |
| 62 " " |
| 63 ], { |
| 64 0: [0, 0], |
| 65 1: [0, 1], |
| 66 2: null |
| 67 }); |
52 | 68 |
53 test(["\n "], {0: [0, 0], 1: [1, 0], 2: [1, 1], 3: null}); | 69 test([ |
| 70 "\n " |
| 71 ], { |
| 72 0: [0, 0], |
| 73 1: [1, 0], |
| 74 2: [1, 1], |
| 75 3: null |
| 76 }); |
54 | 77 |
55 Map positions = {0: [0, 0], | 78 Map positions = { |
56 1: [0, 1], | 79 0: [0, 0], |
57 2: [1, 0], | 80 1: [0, 1], |
58 3: [1, 1], | 81 2: [1, 0], |
59 4: [2, 0], | 82 3: [1, 1], |
60 5: [2, 1], | 83 4: [2, 0], |
61 6: null}; | 84 5: [2, 1], |
| 85 6: null |
| 86 }; |
62 | 87 |
63 test(["a\nb\nc"], positions); | 88 test(["a\nb\nc"], positions); |
64 | 89 |
65 test(["a", "\nb\nc"], positions); | 90 test(["a", "\nb\nc"], positions); |
66 | 91 |
67 test(["a", "\n", "b\nc"], positions); | 92 test(["a", "\n", "b\nc"], positions); |
68 | 93 |
69 CodeBuffer buffer1 = new CodeBuffer(); | 94 CodeBuffer buffer1 = new CodeBuffer(); |
70 buffer1.add("a\nb\nc"); | 95 buffer1.add("a\nb\nc"); |
71 test([buffer1], positions); | 96 test([buffer1], positions); |
72 | 97 |
73 CodeBuffer buffer2 = new CodeBuffer(); | 98 CodeBuffer buffer2 = new CodeBuffer(); |
74 buffer2.add("\nb\nc"); | 99 buffer2.add("\nb\nc"); |
75 test(["a", buffer2], positions); | 100 test(["a", buffer2], positions); |
76 | 101 |
77 CodeBuffer buffer3 = new CodeBuffer(); | 102 CodeBuffer buffer3 = new CodeBuffer(); |
78 buffer3.add("a"); | 103 buffer3.add("a"); |
79 test([buffer3, buffer2], positions); | 104 test([buffer3, buffer2], positions); |
80 | 105 |
81 CodeBuffer buffer4 = new CodeBuffer(); | 106 CodeBuffer buffer4 = new CodeBuffer(); |
82 buffer4.addBuffer(buffer3); | 107 buffer4.addBuffer(buffer3); |
83 test([buffer4, buffer2], positions); | 108 test([buffer4, buffer2], positions); |
84 } | 109 } |
OLD | NEW |