| 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 [LocationCollector]. |
| 6 | 6 |
| 7 import 'package:compiler/src/io/code_output.dart'; |
| 8 import 'package:compiler/src/io/location_provider.dart'; |
| 7 import 'package:expect/expect.dart'; | 9 import 'package:expect/expect.dart'; |
| 8 import 'package:compiler/src/io/code_output.dart'; | 10 import 'package:kernel/ast.dart' show Location; |
| 9 import 'package:compiler/src/io/line_column_provider.dart'; | |
| 10 | 11 |
| 11 import 'output_collector.dart'; | 12 import 'output_collector.dart'; |
| 12 | 13 |
| 13 test(List events, Map<int, List<int>> expectedPositions) { | 14 test(List events, Map<int, List<int>> expectedPositions) { |
| 14 BufferedOutputSink sink = new BufferedOutputSink(); | 15 BufferedOutputSink sink = new BufferedOutputSink(); |
| 15 LineColumnProvider lineColumnProvider = new LineColumnCollector(); | 16 LocationProvider locationProvider = new LocationCollector(); |
| 16 CodeOutput output = new StreamCodeOutput(sink, [lineColumnProvider]); | 17 CodeOutput output = new StreamCodeOutput(sink, [locationProvider]); |
| 17 for (var event in events) { | 18 for (var event in events) { |
| 18 if (event is String) { | 19 if (event is String) { |
| 19 output.add(event); | 20 output.add(event); |
| 20 } else if (event is CodeBuffer) { | 21 } else if (event is CodeBuffer) { |
| 21 output.addBuffer(event); | 22 output.addBuffer(event); |
| 22 } | 23 } |
| 23 } | 24 } |
| 24 output.close(); | 25 output.close(); |
| 25 | 26 |
| 26 expectedPositions.forEach((int offset, List<int> expectedPosition) { | 27 expectedPositions.forEach((int offset, List<int> expectedPosition) { |
| 27 if (expectedPosition == null) { | 28 if (expectedPosition == null) { |
| 28 Expect.throws( | 29 Expect.throws( |
| 29 () => lineColumnProvider.getLine(offset), | 30 () => locationProvider.getLocation(offset), |
| 30 (e) => true, | 31 (e) => true, |
| 31 'Expected out-of-bounds offset: $offset\n' | 32 'Expected out-of-bounds offset: $offset\n' |
| 32 'text:"""${sink.text}"""\n' | 33 'text:"""${sink.text}"""\n' |
| 33 'lineColumnProvider:$lineColumnProvider'); | 34 'locationProvider:$locationProvider'); |
| 34 } else { | 35 } else { |
| 35 int line = lineColumnProvider.getLine(offset); | 36 Location location = locationProvider.getLocation(offset); |
| 36 int column = lineColumnProvider.getColumn(line, offset); | 37 int line = location.line - 1; |
| 38 int column = location.column - 1; |
| 37 Expect.equals( | 39 Expect.equals( |
| 38 expectedPosition[0], | 40 expectedPosition[0], |
| 39 line, | 41 line, |
| 40 'Unexpected result: $offset -> $expectedPosition = [$line,$column]\n' | 42 'Unexpected result: $offset -> $expectedPosition = [$line,$column]\n' |
| 41 'text:"""${sink.text}"""\n' | 43 'text:"""${sink.text}"""\n' |
| 42 'lineColumnProvider:$lineColumnProvider'); | 44 'locationProvider:$locationProvider'); |
| 43 Expect.equals( | 45 Expect.equals( |
| 44 expectedPosition[1], | 46 expectedPosition[1], |
| 45 column, | 47 column, |
| 46 'Unexpected result: $offset -> $expectedPosition = [$line,$column]\n' | 48 'Unexpected result: $offset -> $expectedPosition = [$line,$column]\n' |
| 47 'text:"""${sink.text}"""\n' | 49 'text:"""${sink.text}"""\n' |
| 48 'lineColumnProvider:$lineColumnProvider'); | 50 'locationProvider:$locationProvider'); |
| 49 } | 51 } |
| 50 }); | 52 }); |
| 51 } | 53 } |
| 52 | 54 |
| 53 main() { | 55 main() { |
| 54 test([ | 56 test([ |
| 55 "" | 57 "" |
| 56 ], { | 58 ], { |
| 57 0: [0, 0], | 59 0: [0, 0], |
| 58 1: null | 60 1: null |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 100 test(["a", buffer2], positions); | 102 test(["a", buffer2], positions); |
| 101 | 103 |
| 102 CodeBuffer buffer3 = new CodeBuffer(); | 104 CodeBuffer buffer3 = new CodeBuffer(); |
| 103 buffer3.add("a"); | 105 buffer3.add("a"); |
| 104 test([buffer3, buffer2], positions); | 106 test([buffer3, buffer2], positions); |
| 105 | 107 |
| 106 CodeBuffer buffer4 = new CodeBuffer(); | 108 CodeBuffer buffer4 = new CodeBuffer(); |
| 107 buffer4.addBuffer(buffer3); | 109 buffer4.addBuffer(buffer3); |
| 108 test([buffer4, buffer2], positions); | 110 test([buffer4, buffer2], positions); |
| 109 } | 111 } |
| OLD | NEW |