OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2013, 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 test.span_test; |
| 6 |
| 7 import 'package:unittest/unittest.dart'; |
| 8 import 'package:source_maps/span.dart'; |
| 9 |
| 10 const String TEST_FILE = ''' |
| 11 +23456789_ |
| 12 + _123456789_123456789_123456789_123456789_123456789_123456789_123456789_ |
| 13 + _123456789_1 |
| 14 123+56789_123456789_1234567 |
| 15 1234+6789_1234 |
| 16 12345+789_123456789_12345 |
| 17 123456+89_123456789_123456789_123456789_123456789_123456789_123456789_123456789 |
| 18 1234567+9_123456789_123456789_123456789_123456789_123456789_123456789_123 |
| 19 12345678+_123456789_123456789_123456789_123456789_1 |
| 20 123456789+123456789_123456789_12345678 |
| 21 123456789_+23456789_123456789_123456789_123 |
| 22 123456789_1+3456789_123456789 |
| 23 '''; |
| 24 |
| 25 List<int> newLines = TEST_FILE.split('\n').map((s) => s.length).toList(); |
| 26 |
| 27 main() { |
| 28 var file = new SourceFile.text('file', TEST_FILE); |
| 29 span(int start, int end) => file.span(start, end); |
| 30 loc(int offset) => file.location(offset); |
| 31 |
| 32 test('validate test input', () { |
| 33 expect(newLines, |
| 34 const [10, 80, 31, 27, 14, 25, 79, 73, 51, 38, 43, 29, 0]); |
| 35 }); |
| 36 |
| 37 test('get line and column', () { |
| 38 line(int n) => file.getLine(n); |
| 39 col(int n) => file.getColumn(file.getLine(n), n); |
| 40 |
| 41 expect(line(8), 0); |
| 42 expect(line(10), 0); |
| 43 expect(line(11), 1); |
| 44 expect(line(12), 1); |
| 45 expect(line(91), 1); |
| 46 expect(line(92), 2); |
| 47 expect(line(93), 2); |
| 48 expect(col(11), 0); |
| 49 expect(col(12), 1); |
| 50 expect(col(91), 80); |
| 51 expect(col(92), 0); |
| 52 expect(col(93), 1); |
| 53 |
| 54 int j = 0; |
| 55 int lineOffset = 0; |
| 56 for (int i = 0; i < TEST_FILE.length; i++) { |
| 57 if (i > lineOffset + newLines[j]) { |
| 58 lineOffset += newLines[j] + 1; |
| 59 j++; |
| 60 } |
| 61 expect(line(i), j, reason: 'position: $i'); |
| 62 expect(col(i), i - lineOffset, reason: 'position: $i'); |
| 63 } |
| 64 }); |
| 65 |
| 66 test('get text', () { |
| 67 // fifth line (including 4 new lines), columns 2 .. 11 |
| 68 var line = 10 + 80 + 31 + 27 + 4; |
| 69 expect(file.getText(line + 2, line + 11), '34+6789_1'); |
| 70 }); |
| 71 |
| 72 test('get location message', () { |
| 73 // fifth line (including 4 new lines), columns 2 .. 11 |
| 74 var line = 10 + 80 + 31 + 27 + 4; |
| 75 expect(file.getLocationMessage('the message', line + 2, line + 11), |
| 76 'file:5:3: the message\n' |
| 77 '1234+6789_1234\n' |
| 78 ' ^^^^^^^^^'); |
| 79 }); |
| 80 |
| 81 test('get location message - no file url', () { |
| 82 var line = 10 + 80 + 31 + 27 + 4; |
| 83 expect(new SourceFile.text(null, TEST_FILE).getLocationMessage( |
| 84 'the message', line + 2, line + 11), |
| 85 ':5:3: the message\n' |
| 86 '1234+6789_1234\n' |
| 87 ' ^^^^^^^^^'); |
| 88 }); |
| 89 |
| 90 test('location getters', () { |
| 91 expect(loc(8).line, 0); |
| 92 expect(loc(8).column, 8); |
| 93 expect(loc(9).line, 0); |
| 94 expect(loc(9).column, 9); |
| 95 expect(loc(8).formatString, 'file:1:9'); |
| 96 expect(loc(12).line, 1); |
| 97 expect(loc(12).column, 1); |
| 98 expect(loc(95).line, 2); |
| 99 expect(loc(95).column, 3); |
| 100 }); |
| 101 |
| 102 test('location compare', () { |
| 103 var list = [9, 8, 11, 14, 6, 6, 1, 1].map((n) => loc(n)).toList(); |
| 104 list.sort(); |
| 105 var lastOffset = 0; |
| 106 for (var location in list) { |
| 107 expect(location.offset, greaterThanOrEqualTo(lastOffset)); |
| 108 lastOffset = location.offset; |
| 109 } |
| 110 }); |
| 111 |
| 112 test('span getters', () { |
| 113 expect(span(8, 9).start.line, 0); |
| 114 expect(span(8, 9).start.column, 8); |
| 115 expect(span(8, 9).end.line, 0); |
| 116 expect(span(8, 9).end.column, 9); |
| 117 expect(span(8, 9).text, '9'); |
| 118 expect(span(8, 9).isIdentifier, false); |
| 119 expect(span(8, 9).formatLocation, 'file:1:9'); |
| 120 |
| 121 var line = 10 + 80 + 31 + 27 + 4; |
| 122 expect(span(line + 2, line + 11).getLocationMessage('the message'), |
| 123 'file:5:3: the message\n' |
| 124 '1234+6789_1234\n' |
| 125 ' ^^^^^^^^^'); |
| 126 |
| 127 expect(span(12, 95).start.line, 1); |
| 128 expect(span(12, 95).start.column, 1); |
| 129 expect(span(12, 95).end.line, 2); |
| 130 expect(span(12, 95).end.column, 3); |
| 131 expect(span(12, 95).text, |
| 132 '+ _123456789_123456789_123456789_123456789_123456789_1234567' |
| 133 '89_123456789_\n +'); |
| 134 expect(span(12, 95).formatLocation, 'file:2:2'); |
| 135 }); |
| 136 |
| 137 test('span union', () { |
| 138 var union = new FileSpan.union(span(8, 9), span(12, 95)); |
| 139 expect(union.start.offset, 8); |
| 140 expect(union.start.line, 0); |
| 141 expect(union.start.column, 8); |
| 142 expect(union.end.offset, 95); |
| 143 expect(union.end.line, 2); |
| 144 expect(union.end.column, 3); |
| 145 expect(union.text, |
| 146 '9_\n' |
| 147 ' + _123456789_123456789_123456789_123456789_123456789_' |
| 148 '123456789_123456789_\n +'); |
| 149 expect(union.formatLocation, 'file:1:9'); |
| 150 }); |
| 151 |
| 152 test('span compare', () { |
| 153 var list = [span(9, 10), span(8, 9), span(11, 12), span(14, 19), |
| 154 span(6, 12), span(6, 8), span(1, 9), span(1, 2)]; |
| 155 list.sort(); |
| 156 var lastStart = 0; |
| 157 var lastEnd = 0; |
| 158 for (var span in list) { |
| 159 expect(span.start.offset, greaterThanOrEqualTo(lastStart)); |
| 160 if (span.start.offset == lastStart) { |
| 161 expect(span.end.offset, greaterThanOrEqualTo(lastEnd)); |
| 162 } |
| 163 lastStart = span.start.offset; |
| 164 lastEnd = span.end.offset; |
| 165 } |
| 166 }); |
| 167 |
| 168 test('file segment', () { |
| 169 var segment = new SourceFileSegment('file', |
| 170 TEST_FILE.substring(12), loc(12)); |
| 171 sline(int n) => segment.getLine(n); |
| 172 scol(int n) => segment.getColumn(segment.getLine(n), n); |
| 173 |
| 174 line(int n) => file.getLine(n); |
| 175 col(int n) => file.getColumn(file.getLine(n), n); |
| 176 |
| 177 int j = 0; |
| 178 int lineOffset = 0; |
| 179 for (int i = 12; i < TEST_FILE.length; i++) { |
| 180 if (i > lineOffset + newLines[j]) { |
| 181 lineOffset += newLines[j] + 1; |
| 182 j++; |
| 183 } |
| 184 expect(segment.location(i - 12).offset, i); |
| 185 expect(segment.location(i - 12).line, line(i)); |
| 186 expect(segment.location(i - 12).column, col(i)); |
| 187 expect(segment.span(i - 12).start.offset, i); |
| 188 expect(segment.span(i - 12).start.line, line(i)); |
| 189 expect(segment.span(i - 12).start.column, col(i)); |
| 190 |
| 191 expect(sline(i), line(i)); |
| 192 expect(scol(i), col(i)); |
| 193 } |
| 194 }); |
| 195 |
| 196 test('span isIdentifier defaults to false', () { |
| 197 var start = new TestLocation(0); |
| 198 var end = new TestLocation(1); |
| 199 expect(new TestSpan(start, end).isIdentifier, false); |
| 200 expect(file.span(8, 9, null).isIdentifier, false); |
| 201 expect(new FixedSpan('', 8, 1, 8, isIdentifier: null).isIdentifier, false); |
| 202 }); |
| 203 } |
| 204 |
| 205 class TestSpan extends Span { |
| 206 TestSpan(Location start, Location end) : super(start, end, null); |
| 207 get text => null; |
| 208 } |
| 209 |
| 210 class TestLocation extends Location { |
| 211 String get sourceUrl => ''; |
| 212 TestLocation(int offset) : super(offset); |
| 213 get line => 0; |
| 214 get column => 0; |
| 215 } |
OLD | NEW |