OLD | NEW |
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 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 | 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 library test.span_test; | 5 library test.span_test; |
6 | 6 |
7 import 'package:unittest/unittest.dart'; | 7 import 'package:unittest/unittest.dart'; |
8 import 'package:source_maps/span.dart'; | 8 import 'package:source_maps/span.dart'; |
9 | 9 |
10 const String TEST_FILE = ''' | 10 const String TEST_FILE = ''' |
(...skipping 147 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
158 for (var span in list) { | 158 for (var span in list) { |
159 expect(span.start.offset, greaterThanOrEqualTo(lastStart)); | 159 expect(span.start.offset, greaterThanOrEqualTo(lastStart)); |
160 if (span.start.offset == lastStart) { | 160 if (span.start.offset == lastStart) { |
161 expect(span.end.offset, greaterThanOrEqualTo(lastEnd)); | 161 expect(span.end.offset, greaterThanOrEqualTo(lastEnd)); |
162 } | 162 } |
163 lastStart = span.start.offset; | 163 lastStart = span.start.offset; |
164 lastEnd = span.end.offset; | 164 lastEnd = span.end.offset; |
165 } | 165 } |
166 }); | 166 }); |
167 | 167 |
168 test('file segment', () { | 168 test('range check for large offsets', () { |
169 var segment = new SourceFileSegment('file', | 169 var start = TEST_FILE.length; |
170 TEST_FILE.substring(12), loc(12)); | 170 expect(file.getLocationMessage('the message', start, start + 9), |
| 171 'file:13:1: the message\n'); |
| 172 }); |
| 173 |
| 174 group('file segment', () { |
| 175 var baseOffset = 123; |
| 176 var segmentText = TEST_FILE.substring(baseOffset, TEST_FILE.length - 100); |
| 177 var segment = new SourceFileSegment('file', segmentText, loc(baseOffset)); |
171 sline(int n) => segment.getLine(n); | 178 sline(int n) => segment.getLine(n); |
172 scol(int n) => segment.getColumn(segment.getLine(n), n); | 179 scol(int n) => segment.getColumn(segment.getLine(n), n); |
173 | |
174 line(int n) => file.getLine(n); | 180 line(int n) => file.getLine(n); |
175 col(int n) => file.getColumn(file.getLine(n), n); | 181 col(int n) => file.getColumn(file.getLine(n), n); |
176 | 182 |
177 int j = 0; | 183 test('get line and column', () { |
178 int lineOffset = 0; | 184 int j = 0; |
179 for (int i = 12; i < TEST_FILE.length; i++) { | 185 int lineOffset = 0; |
180 if (i > lineOffset + newLines[j]) { | 186 for (int i = baseOffset; i < segmentText.length; i++) { |
181 lineOffset += newLines[j] + 1; | 187 if (i > lineOffset + newLines[j]) { |
182 j++; | 188 lineOffset += newLines[j] + 1; |
| 189 j++; |
| 190 } |
| 191 expect(segment.location(i - baseOffset).offset, i); |
| 192 expect(segment.location(i - baseOffset).line, line(i)); |
| 193 expect(segment.location(i - baseOffset).column, col(i)); |
| 194 expect(segment.span(i - baseOffset).start.offset, i); |
| 195 expect(segment.span(i - baseOffset).start.line, line(i)); |
| 196 expect(segment.span(i - baseOffset).start.column, col(i)); |
| 197 |
| 198 expect(sline(i), line(i)); |
| 199 expect(scol(i), col(i)); |
183 } | 200 } |
184 expect(segment.location(i - 12).offset, i); | 201 }); |
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 | 202 |
191 expect(sline(i), line(i)); | 203 test('get text', () { |
192 expect(scol(i), col(i)); | 204 var start = 10 + 80 + 31 + 27 + 4 + 2; |
193 } | 205 expect(segment.getText(start, start + 9), file.getText(start, start + 9)); |
| 206 }); |
| 207 |
| 208 group('location message', () { |
| 209 test('first line', () { |
| 210 var start = baseOffset + 7; |
| 211 expect(segment.getLocationMessage('the message', start, start + 2), |
| 212 file.getLocationMessage('the message', start, start + 2)); |
| 213 }); |
| 214 |
| 215 test('in a middle line', () { |
| 216 // Example from another test above: |
| 217 var start = 10 + 80 + 31 + 27 + 4 + 2; |
| 218 expect(segment.getLocationMessage('the message', start, start + 9), |
| 219 file.getLocationMessage('the message', start, start + 9)); |
| 220 }); |
| 221 |
| 222 test('last segment line', () { |
| 223 var start = segmentText.length - 4; |
| 224 expect(segment.getLocationMessage('the message', start, start + 2), |
| 225 file.getLocationMessage('the message', start, start + 2)); |
| 226 }); |
| 227 |
| 228 test('past segment, same as last segment line', () { |
| 229 var start = segmentText.length; |
| 230 expect(segment.getLocationMessage('the message', start, start + 2), |
| 231 file.getLocationMessage('the message', start, start + 2)); |
| 232 |
| 233 start = segmentText.length + 20; |
| 234 expect(segment.getLocationMessage('the message', start, start + 2), |
| 235 file.getLocationMessage('the message', start, start + 2)); |
| 236 }); |
| 237 |
| 238 test('past segment, past its line', () { |
| 239 var start = TEST_FILE.length - 2; |
| 240 expect(file.getLocationMessage('the message', start, start + 1), |
| 241 'file:12:29: the message\n' |
| 242 '123456789_1+3456789_123456789\n' |
| 243 ' ^'); |
| 244 |
| 245 // TODO(sigmund): consider also fixing this and report file:10:4 |
| 246 // The answer below is different because the segment parsing only knows |
| 247 // about the 10 lines it has (and nothing about the possible extra lines |
| 248 // afterwards) |
| 249 expect(segment.getLocationMessage('the message', start, start + 1), |
| 250 'file:10:112: the message\n'); |
| 251 |
| 252 // The number 112 includes the count of extra characters past the |
| 253 // segment, which we verify here: |
| 254 var lastSegmentLineStart = segmentText.lastIndexOf('\n'); |
| 255 expect(start - baseOffset - lastSegmentLineStart, 112); |
| 256 }); |
| 257 }); |
194 }); | 258 }); |
195 | 259 |
196 test('span isIdentifier defaults to false', () { | 260 test('span isIdentifier defaults to false', () { |
197 var start = new TestLocation(0); | 261 var start = new TestLocation(0); |
198 var end = new TestLocation(1); | 262 var end = new TestLocation(1); |
199 expect(new TestSpan(start, end).isIdentifier, false); | 263 expect(new TestSpan(start, end).isIdentifier, false); |
200 expect(file.span(8, 9, null).isIdentifier, false); | 264 expect(file.span(8, 9, null).isIdentifier, false); |
201 expect(new FixedSpan('', 8, 1, 8, isIdentifier: null).isIdentifier, false); | 265 expect(new FixedSpan('', 8, 1, 8, isIdentifier: null).isIdentifier, false); |
202 }); | 266 }); |
203 } | 267 } |
204 | 268 |
205 class TestSpan extends Span { | 269 class TestSpan extends Span { |
206 TestSpan(Location start, Location end) : super(start, end, null); | 270 TestSpan(Location start, Location end) : super(start, end, null); |
207 get text => null; | 271 get text => null; |
208 } | 272 } |
209 | 273 |
210 class TestLocation extends Location { | 274 class TestLocation extends Location { |
211 String get sourceUrl => ''; | 275 String get sourceUrl => ''; |
212 TestLocation(int offset) : super(offset); | 276 TestLocation(int offset) : super(offset); |
213 get line => 0; | 277 get line => 0; |
214 get column => 0; | 278 get column => 0; |
215 } | 279 } |
OLD | NEW |