OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2014, 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 import 'package:test/test.dart'; |
| 6 import 'package:source_span/source_span.dart'; |
| 7 |
| 8 main() { |
| 9 var file; |
| 10 setUp(() { |
| 11 file = new SourceFile(""" |
| 12 foo bar baz |
| 13 whiz bang boom |
| 14 zip zap zop""", url: "foo.dart"); |
| 15 }); |
| 16 |
| 17 group("errors", () { |
| 18 group("for span()", () { |
| 19 test("end must come after start", () { |
| 20 expect(() => file.span(10, 5), throwsArgumentError); |
| 21 }); |
| 22 |
| 23 test("start may not be negative", () { |
| 24 expect(() => file.span(-1, 5), throwsRangeError); |
| 25 }); |
| 26 |
| 27 test("end may not be outside the file", () { |
| 28 expect(() => file.span(10, 100), throwsRangeError); |
| 29 }); |
| 30 }); |
| 31 |
| 32 group("for location()", () { |
| 33 test("offset may not be negative", () { |
| 34 expect(() => file.location(-1), throwsRangeError); |
| 35 }); |
| 36 |
| 37 test("offset may not be outside the file", () { |
| 38 expect(() => file.location(100), throwsRangeError); |
| 39 }); |
| 40 }); |
| 41 |
| 42 group("for getLine()", () { |
| 43 test("offset may not be negative", () { |
| 44 expect(() => file.getLine(-1), throwsRangeError); |
| 45 }); |
| 46 |
| 47 test("offset may not be outside the file", () { |
| 48 expect(() => file.getLine(100), throwsRangeError); |
| 49 }); |
| 50 }); |
| 51 |
| 52 group("for getColumn()", () { |
| 53 test("offset may not be negative", () { |
| 54 expect(() => file.getColumn(-1), throwsRangeError); |
| 55 }); |
| 56 |
| 57 test("offset may not be outside the file", () { |
| 58 expect(() => file.getColumn(100), throwsRangeError); |
| 59 }); |
| 60 |
| 61 test("line may not be negative", () { |
| 62 expect(() => file.getColumn(1, line: -1), throwsRangeError); |
| 63 }); |
| 64 |
| 65 test("line may not be outside the file", () { |
| 66 expect(() => file.getColumn(1, line: 100), throwsRangeError); |
| 67 }); |
| 68 |
| 69 test("line must be accurate", () { |
| 70 expect(() => file.getColumn(1, line: 1), throwsRangeError); |
| 71 }); |
| 72 }); |
| 73 |
| 74 group("getOffset()", () { |
| 75 test("line may not be negative", () { |
| 76 expect(() => file.getOffset(-1), throwsRangeError); |
| 77 }); |
| 78 |
| 79 test("column may not be negative", () { |
| 80 expect(() => file.getOffset(1, -1), throwsRangeError); |
| 81 }); |
| 82 |
| 83 test("line may not be outside the file", () { |
| 84 expect(() => file.getOffset(100), throwsRangeError); |
| 85 }); |
| 86 |
| 87 test("column may not be outside the file", () { |
| 88 expect(() => file.getOffset(2, 100), throwsRangeError); |
| 89 }); |
| 90 |
| 91 test("column may not be outside the line", () { |
| 92 expect(() => file.getOffset(1, 20), throwsRangeError); |
| 93 }); |
| 94 }); |
| 95 |
| 96 group("for getText()", () { |
| 97 test("end must come after start", () { |
| 98 expect(() => file.getText(10, 5), throwsArgumentError); |
| 99 }); |
| 100 |
| 101 test("start may not be negative", () { |
| 102 expect(() => file.getText(-1, 5), throwsRangeError); |
| 103 }); |
| 104 |
| 105 test("end may not be outside the file", () { |
| 106 expect(() => file.getText(10, 100), throwsRangeError); |
| 107 }); |
| 108 }); |
| 109 |
| 110 group("for span().union()", () { |
| 111 test("source URLs must match", () { |
| 112 var other = new SourceSpan( |
| 113 new SourceLocation(10), new SourceLocation(11), "_"); |
| 114 |
| 115 expect(() => file.span(9, 10).union(other), throwsArgumentError); |
| 116 }); |
| 117 |
| 118 test("spans may not be disjoint", () { |
| 119 expect(() => file.span(9, 10).union(file.span(11, 12)), |
| 120 throwsArgumentError); |
| 121 }); |
| 122 }); |
| 123 |
| 124 test("for span().expand() source URLs must match", () { |
| 125 var other = new SourceFile(""" |
| 126 foo bar baz |
| 127 whiz bang boom |
| 128 zip zap zop""", url: "bar.dart").span(10, 11); |
| 129 |
| 130 expect(() => file.span(9, 10).expand(other), throwsArgumentError); |
| 131 }); |
| 132 }); |
| 133 |
| 134 test('fields work correctly', () { |
| 135 expect(file.url, equals(Uri.parse("foo.dart"))); |
| 136 expect(file.lines, equals(3)); |
| 137 expect(file.length, equals(38)); |
| 138 }); |
| 139 |
| 140 group("new SourceFile()", () { |
| 141 test("handles CRLF correctly", () { |
| 142 expect(new SourceFile("foo\r\nbar").getLine(6), equals(1)); |
| 143 }); |
| 144 |
| 145 test("handles a lone CR correctly", () { |
| 146 expect(new SourceFile("foo\rbar").getLine(5), equals(1)); |
| 147 }); |
| 148 }); |
| 149 |
| 150 group("span()", () { |
| 151 test("returns a span between the given offsets", () { |
| 152 var span = file.span(5, 10); |
| 153 expect(span.start, equals(file.location(5))); |
| 154 expect(span.end, equals(file.location(10))); |
| 155 }); |
| 156 |
| 157 test("end defaults to the end of the file", () { |
| 158 var span = file.span(5); |
| 159 expect(span.start, equals(file.location(5))); |
| 160 expect(span.end, equals(file.location(file.length - 1))); |
| 161 }); |
| 162 }); |
| 163 |
| 164 group("getLine()", () { |
| 165 test("works for a middle character on the line", () { |
| 166 expect(file.getLine(15), equals(1)); |
| 167 }); |
| 168 |
| 169 test("works for the first character of a line", () { |
| 170 expect(file.getLine(12), equals(1)); |
| 171 }); |
| 172 |
| 173 test("works for a newline character", () { |
| 174 expect(file.getLine(11), equals(0)); |
| 175 }); |
| 176 |
| 177 test("works for the last offset", () { |
| 178 expect(file.getLine(file.length), equals(2)); |
| 179 }); |
| 180 }); |
| 181 |
| 182 group("getColumn()", () { |
| 183 test("works for a middle character on the line", () { |
| 184 expect(file.getColumn(15), equals(3)); |
| 185 }); |
| 186 |
| 187 test("works for the first character of a line", () { |
| 188 expect(file.getColumn(12), equals(0)); |
| 189 }); |
| 190 |
| 191 test("works for a newline character", () { |
| 192 expect(file.getColumn(11), equals(11)); |
| 193 }); |
| 194 |
| 195 test("works when line is passed as well", () { |
| 196 expect(file.getColumn(12, line: 1), equals(0)); |
| 197 }); |
| 198 |
| 199 test("works for the last offset", () { |
| 200 expect(file.getColumn(file.length), equals(11)); |
| 201 }); |
| 202 }); |
| 203 |
| 204 group("getOffset()", () { |
| 205 test("works for a middle character on the line", () { |
| 206 expect(file.getOffset(1, 3), equals(15)); |
| 207 }); |
| 208 |
| 209 test("works for the first character of a line", () { |
| 210 expect(file.getOffset(1), equals(12)); |
| 211 }); |
| 212 |
| 213 test("works for a newline character", () { |
| 214 expect(file.getOffset(0, 11), equals(11)); |
| 215 }); |
| 216 |
| 217 test("works for the last offset", () { |
| 218 expect(file.getOffset(2, 11), equals(file.length)); |
| 219 }); |
| 220 }); |
| 221 |
| 222 group("getText()", () { |
| 223 test("returns a substring of the source", () { |
| 224 expect(file.getText(8, 15), equals("baz\nwhi")); |
| 225 }); |
| 226 |
| 227 test("end defaults to the end of the file", () { |
| 228 expect(file.getText(20), equals("g boom\nzip zap zop")); |
| 229 }); |
| 230 }); |
| 231 |
| 232 group("FileLocation", () { |
| 233 test("reports the correct line number", () { |
| 234 expect(file.location(15).line, equals(1)); |
| 235 }); |
| 236 |
| 237 test("reports the correct column number", () { |
| 238 expect(file.location(15).column, equals(3)); |
| 239 }); |
| 240 |
| 241 test("pointSpan() returns a FileSpan", () { |
| 242 var location = file.location(15); |
| 243 var span = location.pointSpan(); |
| 244 expect(span, new isInstanceOf<FileSpan>()); |
| 245 expect(span.start, equals(location)); |
| 246 expect(span.end, equals(location)); |
| 247 expect(span.text, isEmpty); |
| 248 }); |
| 249 }); |
| 250 |
| 251 group("FileSpan", () { |
| 252 test("text returns a substring of the source", () { |
| 253 expect(file.span(8, 15).text, equals("baz\nwhi")); |
| 254 }); |
| 255 |
| 256 test("context contains the span's text", () { |
| 257 var span = file.span(8, 15); |
| 258 expect(span.context.contains(span.text), isTrue); |
| 259 expect(span.context, equals('foo bar baz\nwhiz bang boom\n')); |
| 260 }); |
| 261 |
| 262 group("union()", () { |
| 263 var span; |
| 264 setUp(() { |
| 265 span = file.span(5, 12); |
| 266 }); |
| 267 |
| 268 test("works with a preceding adjacent span", () { |
| 269 var other = file.span(0, 5); |
| 270 var result = span.union(other); |
| 271 expect(result.start, equals(other.start)); |
| 272 expect(result.end, equals(span.end)); |
| 273 expect(result.text, equals("foo bar baz\n")); |
| 274 }); |
| 275 |
| 276 test("works with a preceding overlapping span", () { |
| 277 var other = file.span(0, 8); |
| 278 var result = span.union(other); |
| 279 expect(result.start, equals(other.start)); |
| 280 expect(result.end, equals(span.end)); |
| 281 expect(result.text, equals("foo bar baz\n")); |
| 282 }); |
| 283 |
| 284 test("works with a following adjacent span", () { |
| 285 var other = file.span(12, 16); |
| 286 var result = span.union(other); |
| 287 expect(result.start, equals(span.start)); |
| 288 expect(result.end, equals(other.end)); |
| 289 expect(result.text, equals("ar baz\nwhiz")); |
| 290 }); |
| 291 |
| 292 test("works with a following overlapping span", () { |
| 293 var other = file.span(9, 16); |
| 294 var result = span.union(other); |
| 295 expect(result.start, equals(span.start)); |
| 296 expect(result.end, equals(other.end)); |
| 297 expect(result.text, equals("ar baz\nwhiz")); |
| 298 }); |
| 299 |
| 300 test("works with an internal overlapping span", () { |
| 301 var other = file.span(7, 10); |
| 302 expect(span.union(other), equals(span)); |
| 303 }); |
| 304 |
| 305 test("works with an external overlapping span", () { |
| 306 var other = file.span(0, 16); |
| 307 expect(span.union(other), equals(other)); |
| 308 }); |
| 309 |
| 310 test("returns a FileSpan for a FileSpan input", () { |
| 311 expect(span.union(file.span(0, 5)), new isInstanceOf<FileSpan>()); |
| 312 }); |
| 313 |
| 314 test("returns a base SourceSpan for a SourceSpan input", () { |
| 315 var other = new SourceSpan( |
| 316 new SourceLocation(0, sourceUrl: "foo.dart"), |
| 317 new SourceLocation(5, sourceUrl: "foo.dart"), |
| 318 "hey, "); |
| 319 var result = span.union(other); |
| 320 expect(result, isNot(new isInstanceOf<FileSpan>())); |
| 321 expect(result.start, equals(other.start)); |
| 322 expect(result.end, equals(span.end)); |
| 323 expect(result.text, equals("hey, ar baz\n")); |
| 324 }); |
| 325 }); |
| 326 |
| 327 group("expand()", () { |
| 328 var span; |
| 329 setUp(() { |
| 330 span = file.span(5, 12); |
| 331 }); |
| 332 |
| 333 test("works with a preceding nonadjacent span", () { |
| 334 var other = file.span(0, 3); |
| 335 var result = span.expand(other); |
| 336 expect(result.start, equals(other.start)); |
| 337 expect(result.end, equals(span.end)); |
| 338 expect(result.text, equals("foo bar baz\n")); |
| 339 }); |
| 340 |
| 341 test("works with a preceding overlapping span", () { |
| 342 var other = file.span(0, 8); |
| 343 var result = span.expand(other); |
| 344 expect(result.start, equals(other.start)); |
| 345 expect(result.end, equals(span.end)); |
| 346 expect(result.text, equals("foo bar baz\n")); |
| 347 }); |
| 348 |
| 349 test("works with a following nonadjacent span", () { |
| 350 var other = file.span(14, 16); |
| 351 var result = span.expand(other); |
| 352 expect(result.start, equals(span.start)); |
| 353 expect(result.end, equals(other.end)); |
| 354 expect(result.text, equals("ar baz\nwhiz")); |
| 355 }); |
| 356 |
| 357 test("works with a following overlapping span", () { |
| 358 var other = file.span(9, 16); |
| 359 var result = span.expand(other); |
| 360 expect(result.start, equals(span.start)); |
| 361 expect(result.end, equals(other.end)); |
| 362 expect(result.text, equals("ar baz\nwhiz")); |
| 363 }); |
| 364 |
| 365 test("works with an internal overlapping span", () { |
| 366 var other = file.span(7, 10); |
| 367 expect(span.expand(other), equals(span)); |
| 368 }); |
| 369 |
| 370 test("works with an external overlapping span", () { |
| 371 var other = file.span(0, 16); |
| 372 expect(span.expand(other), equals(other)); |
| 373 }); |
| 374 }); |
| 375 }); |
| 376 } |
OLD | NEW |