| 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 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 65 | 65 |
| 66 test('get text', () { | 66 test('get text', () { |
| 67 // fifth line (including 4 new lines), columns 2 .. 11 | 67 // fifth line (including 4 new lines), columns 2 .. 11 |
| 68 var line = 10 + 80 + 31 + 27 + 4; | 68 var line = 10 + 80 + 31 + 27 + 4; |
| 69 expect(file.getText(line + 2, line + 11), '34+6789_1'); | 69 expect(file.getText(line + 2, line + 11), '34+6789_1'); |
| 70 }); | 70 }); |
| 71 | 71 |
| 72 group('location message', () { | 72 group('location message', () { |
| 73 test('first line', () { | 73 test('first line', () { |
| 74 expect(file.getLocationMessage('the message', 1, 3), | 74 expect(file.getLocationMessage('the message', 1, 3), |
| 75 'file:1:2: the message\n' | 75 'line 1, column 2 of file: the message\n' |
| 76 '+23456789_\n' | 76 '+23456789_\n' |
| 77 ' ^^'); | 77 ' ^^'); |
| 78 }); | 78 }); |
| 79 | 79 |
| 80 test('in the middle of the file', () { | 80 test('in the middle of the file', () { |
| 81 // fifth line (including 4 new lines), columns 2 .. 11 | 81 // fifth line (including 4 new lines), columns 2 .. 11 |
| 82 var line = 10 + 80 + 31 + 27 + 4; | 82 var line = 10 + 80 + 31 + 27 + 4; |
| 83 expect(file.getLocationMessage('the message', line + 2, line + 11), | 83 expect(file.getLocationMessage('the message', line + 2, line + 11), |
| 84 'file:5:3: the message\n' | 84 'line 5, column 3 of file: the message\n' |
| 85 '1234+6789_1234\n' | 85 '1234+6789_1234\n' |
| 86 ' ^^^^^^^^^'); | 86 ' ^^^^^^^^^'); |
| 87 }); | 87 }); |
| 88 | 88 |
| 89 test('no file url', () { | 89 test('no file url', () { |
| 90 var line = 10 + 80 + 31 + 27 + 4; | 90 var line = 10 + 80 + 31 + 27 + 4; |
| 91 expect(new SourceFile.text(null, TEST_FILE).getLocationMessage( | 91 expect(new SourceFile.text(null, TEST_FILE).getLocationMessage( |
| 92 'the message', line + 2, line + 11), | 92 'the message', line + 2, line + 11), |
| 93 ':5:3: the message\n' | 93 'line 5, column 3: the message\n' |
| 94 '1234+6789_1234\n' | 94 '1234+6789_1234\n' |
| 95 ' ^^^^^^^^^'); | 95 ' ^^^^^^^^^'); |
| 96 }); | 96 }); |
| 97 | 97 |
| 98 test('penultimate line', () { | 98 test('penultimate line', () { |
| 99 // We search '\n' backwards twice because last line is \n terminated: | 99 // We search '\n' backwards twice because last line is \n terminated: |
| 100 int index = TEST_FILE.lastIndexOf('\n'); | 100 int index = TEST_FILE.lastIndexOf('\n'); |
| 101 var start = TEST_FILE.lastIndexOf('\n', index - 1) - 3; | 101 var start = TEST_FILE.lastIndexOf('\n', index - 1) - 3; |
| 102 expect(file.getLocationMessage('the message', start, start + 2), | 102 expect(file.getLocationMessage('the message', start, start + 2), |
| 103 'file:11:41: the message\n' | 103 'line 11, column 41 of file: the message\n' |
| 104 '123456789_+23456789_123456789_123456789_123\n' | 104 '123456789_+23456789_123456789_123456789_123\n' |
| 105 ' ^^'); | 105 ' ^^'); |
| 106 }); | 106 }); |
| 107 | 107 |
| 108 test('last line', () { | 108 test('last line', () { |
| 109 var start = TEST_FILE.lastIndexOf('\n') - 2; | 109 var start = TEST_FILE.lastIndexOf('\n') - 2; |
| 110 expect(file.getLocationMessage('the message', start, start + 1), | 110 expect(file.getLocationMessage('the message', start, start + 1), |
| 111 'file:12:28: the message\n' | 111 'line 12, column 28 of file: the message\n' |
| 112 '123456789_1+3456789_123456789\n' | 112 '123456789_1+3456789_123456789\n' |
| 113 ' ^'); | 113 ' ^'); |
| 114 }); | 114 }); |
| 115 | 115 |
| 116 group('no trailing empty-line at the end -', () { | 116 group('no trailing empty-line at the end -', () { |
| 117 var text = TEST_FILE.substring(0, TEST_FILE.length - 1); | 117 var text = TEST_FILE.substring(0, TEST_FILE.length - 1); |
| 118 var file2 = new SourceFile.text('file', text); | 118 var file2 = new SourceFile.text('file', text); |
| 119 | 119 |
| 120 test('penultimate line', () { | 120 test('penultimate line', () { |
| 121 var start = text.lastIndexOf('\n') - 3; | 121 var start = text.lastIndexOf('\n') - 3; |
| 122 expect(file2.getLocationMessage('the message', start, start + 2), | 122 expect(file2.getLocationMessage('the message', start, start + 2), |
| 123 'file:11:41: the message\n' | 123 'line 11, column 41 of file: the message\n' |
| 124 '123456789_+23456789_123456789_123456789_123\n' | 124 '123456789_+23456789_123456789_123456789_123\n' |
| 125 ' ^^'); | 125 ' ^^'); |
| 126 }); | 126 }); |
| 127 | 127 |
| 128 test('last line', () { | 128 test('last line', () { |
| 129 var start = text.length - 2; | 129 var start = text.length - 2; |
| 130 expect(file2.getLocationMessage('the message', start, start + 1), | 130 expect(file2.getLocationMessage('the message', start, start + 1), |
| 131 'file:12:28: the message\n' | 131 'line 12, column 28 of file: the message\n' |
| 132 '123456789_1+3456789_123456789\n' | 132 '123456789_1+3456789_123456789\n' |
| 133 ' ^'); | 133 ' ^'); |
| 134 }); | 134 }); |
| 135 }); | 135 }); |
| 136 | 136 |
| 137 test('single line', () { | 137 test('single line', () { |
| 138 var text = "this is a single line"; | 138 var text = "this is a single line"; |
| 139 int start = text.indexOf(' ') + 1; | 139 int start = text.indexOf(' ') + 1; |
| 140 var file2 = new SourceFile.text('file', text); | 140 var file2 = new SourceFile.text('file', text); |
| 141 expect(file2.getLocationMessage('the message', start, start + 2), | 141 expect(file2.getLocationMessage('the message', start, start + 2), |
| 142 'file:1:${start + 1}: the message\n' | 142 'line 1, column ${start + 1} of file: the message\n' |
| 143 'this is a single line\n' | 143 'this is a single line\n' |
| 144 ' ^^'); | 144 ' ^^'); |
| 145 }); | 145 }); |
| 146 }); | 146 }); |
| 147 | 147 |
| 148 test('location getters', () { | 148 test('location getters', () { |
| 149 expect(loc(8).line, 0); | 149 expect(loc(8).line, 0); |
| 150 expect(loc(8).column, 8); | 150 expect(loc(8).column, 8); |
| 151 expect(loc(9).line, 0); | 151 expect(loc(9).line, 0); |
| 152 expect(loc(9).column, 9); | 152 expect(loc(9).column, 9); |
| (...skipping 18 matching lines...) Expand all Loading... |
| 171 expect(span(8, 9).start.line, 0); | 171 expect(span(8, 9).start.line, 0); |
| 172 expect(span(8, 9).start.column, 8); | 172 expect(span(8, 9).start.column, 8); |
| 173 expect(span(8, 9).end.line, 0); | 173 expect(span(8, 9).end.line, 0); |
| 174 expect(span(8, 9).end.column, 9); | 174 expect(span(8, 9).end.column, 9); |
| 175 expect(span(8, 9).text, '9'); | 175 expect(span(8, 9).text, '9'); |
| 176 expect(span(8, 9).isIdentifier, false); | 176 expect(span(8, 9).isIdentifier, false); |
| 177 expect(span(8, 9).formatLocation, 'file:1:9'); | 177 expect(span(8, 9).formatLocation, 'file:1:9'); |
| 178 | 178 |
| 179 var line = 10 + 80 + 31 + 27 + 4; | 179 var line = 10 + 80 + 31 + 27 + 4; |
| 180 expect(span(line + 2, line + 11).getLocationMessage('the message'), | 180 expect(span(line + 2, line + 11).getLocationMessage('the message'), |
| 181 'file:5:3: the message\n' | 181 'line 5, column 3 of file: the message\n' |
| 182 '1234+6789_1234\n' | 182 '1234+6789_1234\n' |
| 183 ' ^^^^^^^^^'); | 183 ' ^^^^^^^^^'); |
| 184 | 184 |
| 185 expect(span(12, 95).start.line, 1); | 185 expect(span(12, 95).start.line, 1); |
| 186 expect(span(12, 95).start.column, 1); | 186 expect(span(12, 95).start.column, 1); |
| 187 expect(span(12, 95).end.line, 2); | 187 expect(span(12, 95).end.line, 2); |
| 188 expect(span(12, 95).end.column, 3); | 188 expect(span(12, 95).end.column, 3); |
| 189 expect(span(12, 95).text, | 189 expect(span(12, 95).text, |
| 190 '+ _123456789_123456789_123456789_123456789_123456789_1234567' | 190 '+ _123456789_123456789_123456789_123456789_123456789_1234567' |
| 191 '89_123456789_\n +'); | 191 '89_123456789_\n +'); |
| (...skipping 27 matching lines...) Expand all Loading... |
| 219 expect(span.end.offset, greaterThanOrEqualTo(lastEnd)); | 219 expect(span.end.offset, greaterThanOrEqualTo(lastEnd)); |
| 220 } | 220 } |
| 221 lastStart = span.start.offset; | 221 lastStart = span.start.offset; |
| 222 lastEnd = span.end.offset; | 222 lastEnd = span.end.offset; |
| 223 } | 223 } |
| 224 }); | 224 }); |
| 225 | 225 |
| 226 test('range check for large offsets', () { | 226 test('range check for large offsets', () { |
| 227 var start = TEST_FILE.length; | 227 var start = TEST_FILE.length; |
| 228 expect(file.getLocationMessage('the message', start, start + 9), | 228 expect(file.getLocationMessage('the message', start, start + 9), |
| 229 'file:13:1: the message\n'); | 229 'line 13, column 1 of file: the message\n'); |
| 230 }); | 230 }); |
| 231 | 231 |
| 232 group('file segment', () { | 232 group('file segment', () { |
| 233 var baseOffset = 123; | 233 var baseOffset = 123; |
| 234 var segmentText = TEST_FILE.substring(baseOffset, TEST_FILE.length - 100); | 234 var segmentText = TEST_FILE.substring(baseOffset, TEST_FILE.length - 100); |
| 235 var segment = new SourceFileSegment('file', segmentText, loc(baseOffset)); | 235 var segment = new SourceFileSegment('file', segmentText, loc(baseOffset)); |
| 236 sline(int n) => segment.getLine(n); | 236 sline(int n) => segment.getLine(n); |
| 237 scol(int n) => segment.getColumn(segment.getLine(n), n); | 237 scol(int n) => segment.getColumn(segment.getLine(n), n); |
| 238 line(int n) => file.getLine(n); | 238 line(int n) => file.getLine(n); |
| 239 col(int n) => file.getColumn(file.getLine(n), n); | 239 col(int n) => file.getColumn(file.getLine(n), n); |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 289 file.getLocationMessage('the message', start, start + 2)); | 289 file.getLocationMessage('the message', start, start + 2)); |
| 290 | 290 |
| 291 start = segmentText.length + 20; | 291 start = segmentText.length + 20; |
| 292 expect(segment.getLocationMessage('the message', start, start + 2), | 292 expect(segment.getLocationMessage('the message', start, start + 2), |
| 293 file.getLocationMessage('the message', start, start + 2)); | 293 file.getLocationMessage('the message', start, start + 2)); |
| 294 }); | 294 }); |
| 295 | 295 |
| 296 test('past segment, past its line', () { | 296 test('past segment, past its line', () { |
| 297 var start = TEST_FILE.length - 2; | 297 var start = TEST_FILE.length - 2; |
| 298 expect(file.getLocationMessage('the message', start, start + 1), | 298 expect(file.getLocationMessage('the message', start, start + 1), |
| 299 'file:12:29: the message\n' | 299 'line 12, column 29 of file: the message\n' |
| 300 '123456789_1+3456789_123456789\n' | 300 '123456789_1+3456789_123456789\n' |
| 301 ' ^'); | 301 ' ^'); |
| 302 | 302 |
| 303 // The answer below is different because the segment parsing only knows | 303 // The answer below is different because the segment parsing only knows |
| 304 // about the 10 lines it has (and nothing about the possible extra lines | 304 // about the 10 lines it has (and nothing about the possible extra lines |
| 305 // afterwards) | 305 // afterwards) |
| 306 expect(segment.getLocationMessage('the message', start, start + 1), | 306 expect(segment.getLocationMessage('the message', start, start + 1), |
| 307 'file:11:1: the message\n'); | 307 'line 11, column 1 of file: the message\n'); |
| 308 }); | 308 }); |
| 309 }); | 309 }); |
| 310 }); | 310 }); |
| 311 | 311 |
| 312 test('span isIdentifier defaults to false', () { | 312 test('span isIdentifier defaults to false', () { |
| 313 var start = new TestLocation(0); | 313 var start = new TestLocation(0); |
| 314 var end = new TestLocation(1); | 314 var end = new TestLocation(1); |
| 315 expect(new TestSpan(start, end).isIdentifier, false); | 315 expect(new TestSpan(start, end).isIdentifier, false); |
| 316 expect(file.span(8, 9, null).isIdentifier, false); | 316 expect(file.span(8, 9, null).isIdentifier, false); |
| 317 expect(new FixedSpan('', 8, 1, 8, isIdentifier: null).isIdentifier, false); | 317 expect(new FixedSpan('', 8, 1, 8, isIdentifier: null).isIdentifier, false); |
| (...skipping 14 matching lines...) Expand all Loading... |
| 332 TestSpan(Location start, Location end) : super(start, end, null); | 332 TestSpan(Location start, Location end) : super(start, end, null); |
| 333 get text => null; | 333 get text => null; |
| 334 } | 334 } |
| 335 | 335 |
| 336 class TestLocation extends Location { | 336 class TestLocation extends Location { |
| 337 String get sourceUrl => ''; | 337 String get sourceUrl => ''; |
| 338 TestLocation(int offset) : super(offset); | 338 TestLocation(int offset) : super(offset); |
| 339 get line => 0; | 339 get line => 0; |
| 340 get column => 0; | 340 get column => 0; |
| 341 } | 341 } |
| OLD | NEW |