| 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:unittest/unittest.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 group("union()", () { | |
| 257 var span; | |
| 258 setUp(() { | |
| 259 span = file.span(5, 12); | |
| 260 }); | |
| 261 | |
| 262 test("works with a preceding adjacent span", () { | |
| 263 var other = file.span(0, 5); | |
| 264 var result = span.union(other); | |
| 265 expect(result.start, equals(other.start)); | |
| 266 expect(result.end, equals(span.end)); | |
| 267 expect(result.text, equals("foo bar baz\n")); | |
| 268 }); | |
| 269 | |
| 270 test("works with a preceding overlapping span", () { | |
| 271 var other = file.span(0, 8); | |
| 272 var result = span.union(other); | |
| 273 expect(result.start, equals(other.start)); | |
| 274 expect(result.end, equals(span.end)); | |
| 275 expect(result.text, equals("foo bar baz\n")); | |
| 276 }); | |
| 277 | |
| 278 test("works with a following adjacent span", () { | |
| 279 var other = file.span(12, 16); | |
| 280 var result = span.union(other); | |
| 281 expect(result.start, equals(span.start)); | |
| 282 expect(result.end, equals(other.end)); | |
| 283 expect(result.text, equals("ar baz\nwhiz")); | |
| 284 }); | |
| 285 | |
| 286 test("works with a following overlapping span", () { | |
| 287 var other = file.span(9, 16); | |
| 288 var result = span.union(other); | |
| 289 expect(result.start, equals(span.start)); | |
| 290 expect(result.end, equals(other.end)); | |
| 291 expect(result.text, equals("ar baz\nwhiz")); | |
| 292 }); | |
| 293 | |
| 294 test("works with an internal overlapping span", () { | |
| 295 var other = file.span(7, 10); | |
| 296 expect(span.union(other), equals(span)); | |
| 297 }); | |
| 298 | |
| 299 test("works with an external overlapping span", () { | |
| 300 var other = file.span(0, 16); | |
| 301 expect(span.union(other), equals(other)); | |
| 302 }); | |
| 303 | |
| 304 test("returns a FileSpan for a FileSpan input", () { | |
| 305 expect(span.union(file.span(0, 5)), new isInstanceOf<FileSpan>()); | |
| 306 }); | |
| 307 | |
| 308 test("returns a base SourceSpan for a SourceSpan input", () { | |
| 309 var other = new SourceSpan( | |
| 310 new SourceLocation(0, sourceUrl: "foo.dart"), | |
| 311 new SourceLocation(5, sourceUrl: "foo.dart"), | |
| 312 "hey, "); | |
| 313 var result = span.union(other); | |
| 314 expect(result, isNot(new isInstanceOf<FileSpan>())); | |
| 315 expect(result.start, equals(other.start)); | |
| 316 expect(result.end, equals(span.end)); | |
| 317 expect(result.text, equals("hey, ar baz\n")); | |
| 318 }); | |
| 319 }); | |
| 320 | |
| 321 group("expand()", () { | |
| 322 var span; | |
| 323 setUp(() { | |
| 324 span = file.span(5, 12); | |
| 325 }); | |
| 326 | |
| 327 test("works with a preceding nonadjacent span", () { | |
| 328 var other = file.span(0, 3); | |
| 329 var result = span.expand(other); | |
| 330 expect(result.start, equals(other.start)); | |
| 331 expect(result.end, equals(span.end)); | |
| 332 expect(result.text, equals("foo bar baz\n")); | |
| 333 }); | |
| 334 | |
| 335 test("works with a preceding overlapping span", () { | |
| 336 var other = file.span(0, 8); | |
| 337 var result = span.expand(other); | |
| 338 expect(result.start, equals(other.start)); | |
| 339 expect(result.end, equals(span.end)); | |
| 340 expect(result.text, equals("foo bar baz\n")); | |
| 341 }); | |
| 342 | |
| 343 test("works with a following nonadjacent span", () { | |
| 344 var other = file.span(14, 16); | |
| 345 var result = span.expand(other); | |
| 346 expect(result.start, equals(span.start)); | |
| 347 expect(result.end, equals(other.end)); | |
| 348 expect(result.text, equals("ar baz\nwhiz")); | |
| 349 }); | |
| 350 | |
| 351 test("works with a following overlapping span", () { | |
| 352 var other = file.span(9, 16); | |
| 353 var result = span.expand(other); | |
| 354 expect(result.start, equals(span.start)); | |
| 355 expect(result.end, equals(other.end)); | |
| 356 expect(result.text, equals("ar baz\nwhiz")); | |
| 357 }); | |
| 358 | |
| 359 test("works with an internal overlapping span", () { | |
| 360 var other = file.span(7, 10); | |
| 361 expect(span.expand(other), equals(span)); | |
| 362 }); | |
| 363 | |
| 364 test("works with an external overlapping span", () { | |
| 365 var other = file.span(0, 16); | |
| 366 expect(span.expand(other), equals(other)); | |
| 367 }); | |
| 368 }); | |
| 369 }); | |
| 370 } | |
| OLD | NEW |