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 location; |
| 10 setUp(() { |
| 11 location = new SourceLocation(15, |
| 12 line: 2, column: 6, sourceUrl: "foo.dart"); |
| 13 }); |
| 14 |
| 15 group('errors', () { |
| 16 group('for new SourceLocation()', () { |
| 17 test('offset may not be negative', () { |
| 18 expect(() => new SourceLocation(-1), throwsRangeError); |
| 19 }); |
| 20 |
| 21 test('line may not be negative', () { |
| 22 expect(() => new SourceLocation(0, line: -1), throwsRangeError); |
| 23 }); |
| 24 |
| 25 test('column may not be negative', () { |
| 26 expect(() => new SourceLocation(0, column: -1), throwsRangeError); |
| 27 }); |
| 28 }); |
| 29 |
| 30 test('for distance() source URLs must match', () { |
| 31 expect(() => location.distance(new SourceLocation(0)), |
| 32 throwsArgumentError); |
| 33 }); |
| 34 |
| 35 test('for compareTo() source URLs must match', () { |
| 36 expect(() => location.compareTo(new SourceLocation(0)), |
| 37 throwsArgumentError); |
| 38 }); |
| 39 }); |
| 40 |
| 41 test('fields work correctly', () { |
| 42 expect(location.sourceUrl, equals(Uri.parse("foo.dart"))); |
| 43 expect(location.offset, equals(15)); |
| 44 expect(location.line, equals(2)); |
| 45 expect(location.column, equals(6)); |
| 46 }); |
| 47 |
| 48 group('toolString', () { |
| 49 test('returns a computer-readable representation', () { |
| 50 expect(location.toolString, equals('foo.dart:3:7')); |
| 51 }); |
| 52 |
| 53 test('gracefully handles a missing source URL', () { |
| 54 var location = new SourceLocation(15, line: 2, column: 6); |
| 55 expect(location.toolString, equals('unknown source:3:7')); |
| 56 }); |
| 57 }); |
| 58 |
| 59 test("distance returns the absolute distance between locations", () { |
| 60 var other = new SourceLocation(10, sourceUrl: "foo.dart"); |
| 61 expect(location.distance(other), equals(5)); |
| 62 expect(other.distance(location), equals(5)); |
| 63 }); |
| 64 |
| 65 test("pointSpan returns an empty span at location", () { |
| 66 var span = location.pointSpan(); |
| 67 expect(span.start, equals(location)); |
| 68 expect(span.end, equals(location)); |
| 69 expect(span.text, isEmpty); |
| 70 }); |
| 71 |
| 72 group("compareTo()", () { |
| 73 test("sorts by offset", () { |
| 74 var other = new SourceLocation(20, sourceUrl: "foo.dart"); |
| 75 expect(location.compareTo(other), lessThan(0)); |
| 76 expect(other.compareTo(location), greaterThan(0)); |
| 77 }); |
| 78 |
| 79 test("considers equal locations equal", () { |
| 80 expect(location.compareTo(location), equals(0)); |
| 81 }); |
| 82 }); |
| 83 |
| 84 |
| 85 group("equality", () { |
| 86 test("two locations with the same offset and source are equal", () { |
| 87 var other = new SourceLocation(15, sourceUrl: "foo.dart"); |
| 88 expect(location, equals(other)); |
| 89 }); |
| 90 |
| 91 test("a different offset isn't equal", () { |
| 92 var other = new SourceLocation(10, sourceUrl: "foo.dart"); |
| 93 expect(location, isNot(equals(other))); |
| 94 }); |
| 95 |
| 96 test("a different source isn't equal", () { |
| 97 var other = new SourceLocation(15, sourceUrl: "bar.dart"); |
| 98 expect(location, isNot(equals(other))); |
| 99 }); |
| 100 }); |
| 101 } |
OLD | NEW |