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