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 import 'package:source_span/src/colors.dart' as colors; |
| 8 |
| 9 main() { |
| 10 var span; |
| 11 setUp(() { |
| 12 span = new SourceSpan( |
| 13 new SourceLocation(5, sourceUrl: "foo.dart"), |
| 14 new SourceLocation(12, sourceUrl: "foo.dart"), |
| 15 "foo bar"); |
| 16 }); |
| 17 |
| 18 group('errors', () { |
| 19 group('for new SourceSpan()', () { |
| 20 test('source URLs must match', () { |
| 21 var start = new SourceLocation(0, sourceUrl: "foo.dart"); |
| 22 var end = new SourceLocation(1, sourceUrl: "bar.dart"); |
| 23 expect(() => new SourceSpan(start, end, "_"), throwsArgumentError); |
| 24 }); |
| 25 |
| 26 test('end must come after start', () { |
| 27 var start = new SourceLocation(1); |
| 28 var end = new SourceLocation(0); |
| 29 expect(() => new SourceSpan(start, end, "_"), throwsArgumentError); |
| 30 }); |
| 31 |
| 32 test('text must be the right length', () { |
| 33 var start = new SourceLocation(0); |
| 34 var end = new SourceLocation(1); |
| 35 expect(() => new SourceSpan(start, end, "abc"), throwsArgumentError); |
| 36 }); |
| 37 }); |
| 38 |
| 39 group('for union()', () { |
| 40 test('source URLs must match', () { |
| 41 var other = new SourceSpan( |
| 42 new SourceLocation(12, sourceUrl: "bar.dart"), |
| 43 new SourceLocation(13, sourceUrl: "bar.dart"), |
| 44 "_"); |
| 45 |
| 46 expect(() => span.union(other), throwsArgumentError); |
| 47 }); |
| 48 |
| 49 test('spans may not be disjoint', () { |
| 50 var other = new SourceSpan( |
| 51 new SourceLocation(13, sourceUrl: 'foo.dart'), |
| 52 new SourceLocation(14, sourceUrl: 'foo.dart'), |
| 53 "_"); |
| 54 |
| 55 expect(() => span.union(other), throwsArgumentError); |
| 56 }); |
| 57 }); |
| 58 |
| 59 test('for compareTo() source URLs must match', () { |
| 60 var other = new SourceSpan( |
| 61 new SourceLocation(12, sourceUrl: "bar.dart"), |
| 62 new SourceLocation(13, sourceUrl: "bar.dart"), |
| 63 "_"); |
| 64 |
| 65 expect(() => span.compareTo(other), throwsArgumentError); |
| 66 }); |
| 67 }); |
| 68 |
| 69 test('fields work correctly', () { |
| 70 expect(span.start, equals(new SourceLocation(5, sourceUrl: "foo.dart"))); |
| 71 expect(span.end, equals(new SourceLocation(12, sourceUrl: "foo.dart"))); |
| 72 expect(span.sourceUrl, equals(Uri.parse("foo.dart"))); |
| 73 expect(span.length, equals(7)); |
| 74 }); |
| 75 |
| 76 group("union()", () { |
| 77 test("works with a preceding adjacent span", () { |
| 78 var other = new SourceSpan( |
| 79 new SourceLocation(0, sourceUrl: "foo.dart"), |
| 80 new SourceLocation(5, sourceUrl: "foo.dart"), |
| 81 "hey, "); |
| 82 |
| 83 var result = span.union(other); |
| 84 expect(result.start, equals(other.start)); |
| 85 expect(result.end, equals(span.end)); |
| 86 expect(result.text, equals("hey, foo bar")); |
| 87 }); |
| 88 |
| 89 test("works with a preceding overlapping span", () { |
| 90 var other = new SourceSpan( |
| 91 new SourceLocation(0, sourceUrl: "foo.dart"), |
| 92 new SourceLocation(8, sourceUrl: "foo.dart"), |
| 93 "hey, foo"); |
| 94 |
| 95 var result = span.union(other); |
| 96 expect(result.start, equals(other.start)); |
| 97 expect(result.end, equals(span.end)); |
| 98 expect(result.text, equals("hey, foo bar")); |
| 99 }); |
| 100 |
| 101 test("works with a following adjacent span", () { |
| 102 var other = new SourceSpan( |
| 103 new SourceLocation(12, sourceUrl: "foo.dart"), |
| 104 new SourceLocation(16, sourceUrl: "foo.dart"), |
| 105 " baz"); |
| 106 |
| 107 var result = span.union(other); |
| 108 expect(result.start, equals(span.start)); |
| 109 expect(result.end, equals(other.end)); |
| 110 expect(result.text, equals("foo bar baz")); |
| 111 }); |
| 112 |
| 113 test("works with a following overlapping span", () { |
| 114 var other = new SourceSpan( |
| 115 new SourceLocation(9, sourceUrl: "foo.dart"), |
| 116 new SourceLocation(16, sourceUrl: "foo.dart"), |
| 117 "bar baz"); |
| 118 |
| 119 var result = span.union(other); |
| 120 expect(result.start, equals(span.start)); |
| 121 expect(result.end, equals(other.end)); |
| 122 expect(result.text, equals("foo bar baz")); |
| 123 }); |
| 124 |
| 125 test("works with an internal overlapping span", () { |
| 126 var other = new SourceSpan( |
| 127 new SourceLocation(7, sourceUrl: "foo.dart"), |
| 128 new SourceLocation(10, sourceUrl: "foo.dart"), |
| 129 "o b"); |
| 130 |
| 131 expect(span.union(other), equals(span)); |
| 132 }); |
| 133 |
| 134 test("works with an external overlapping span", () { |
| 135 var other = new SourceSpan( |
| 136 new SourceLocation(0, sourceUrl: "foo.dart"), |
| 137 new SourceLocation(16, sourceUrl: "foo.dart"), |
| 138 "hey, foo bar baz"); |
| 139 |
| 140 expect(span.union(other), equals(other)); |
| 141 }); |
| 142 }); |
| 143 |
| 144 group("message()", () { |
| 145 test("prints the text being described", () { |
| 146 expect(span.message("oh no"), equals(""" |
| 147 line 1, column 6 of foo.dart: oh no |
| 148 foo bar |
| 149 ^^^^^^^""")); |
| 150 }); |
| 151 |
| 152 test("gracefully handles a missing source URL", () { |
| 153 var span = new SourceSpan( |
| 154 new SourceLocation(5), new SourceLocation(12), "foo bar"); |
| 155 |
| 156 expect(span.message("oh no"), equalsIgnoringWhitespace(""" |
| 157 line 1, column 6: oh no |
| 158 foo bar |
| 159 ^^^^^^^""")); |
| 160 }); |
| 161 |
| 162 test("gracefully handles empty text", () { |
| 163 var span = new SourceSpan( |
| 164 new SourceLocation(5), new SourceLocation(5), ""); |
| 165 |
| 166 expect(span.message("oh no"), |
| 167 equals("line 1, column 6: oh no")); |
| 168 }); |
| 169 |
| 170 test("doesn't colorize if color is false", () { |
| 171 expect(span.message("oh no", color: false), equals(""" |
| 172 line 1, column 6 of foo.dart: oh no |
| 173 foo bar |
| 174 ^^^^^^^""")); |
| 175 }); |
| 176 |
| 177 test("colorizes if color is true", () { |
| 178 expect(span.message("oh no", color: true), |
| 179 equals(""" |
| 180 line 1, column 6 of foo.dart: oh no |
| 181 ${colors.RED}foo bar |
| 182 ^^^^^^^${colors.NONE}""")); |
| 183 }); |
| 184 |
| 185 test("uses the given color if it's passed", () { |
| 186 expect(span.message("oh no", color: colors.YELLOW), equals(""" |
| 187 line 1, column 6 of foo.dart: oh no |
| 188 ${colors.YELLOW}foo bar |
| 189 ^^^^^^^${colors.NONE}""")); |
| 190 }); |
| 191 }); |
| 192 |
| 193 group("compareTo()", () { |
| 194 test("sorts by start location first", () { |
| 195 var other = new SourceSpan( |
| 196 new SourceLocation(6, sourceUrl: "foo.dart"), |
| 197 new SourceLocation(14, sourceUrl: "foo.dart"), |
| 198 "oo bar b"); |
| 199 |
| 200 expect(span.compareTo(other), lessThan(0)); |
| 201 expect(other.compareTo(span), greaterThan(0)); |
| 202 }); |
| 203 |
| 204 test("sorts by length second", () { |
| 205 var other = new SourceSpan( |
| 206 new SourceLocation(5, sourceUrl: "foo.dart"), |
| 207 new SourceLocation(14, sourceUrl: "foo.dart"), |
| 208 "foo bar b"); |
| 209 |
| 210 expect(span.compareTo(other), lessThan(0)); |
| 211 expect(other.compareTo(span), greaterThan(0)); |
| 212 }); |
| 213 |
| 214 test("considers equal spans equal", () { |
| 215 expect(span.compareTo(span), equals(0)); |
| 216 }); |
| 217 }); |
| 218 |
| 219 group("equality", () { |
| 220 test("two spans with the same locations are equal", () { |
| 221 var other = new SourceSpan( |
| 222 new SourceLocation(5, sourceUrl: "foo.dart"), |
| 223 new SourceLocation(12, sourceUrl: "foo.dart"), |
| 224 "foo bar"); |
| 225 |
| 226 expect(span, equals(other)); |
| 227 }); |
| 228 |
| 229 test("a different start isn't equal", () { |
| 230 var other = new SourceSpan( |
| 231 new SourceLocation(0, sourceUrl: "foo.dart"), |
| 232 new SourceLocation(12, sourceUrl: "foo.dart"), |
| 233 "hey, foo bar"); |
| 234 |
| 235 expect(span, isNot(equals(other))); |
| 236 }); |
| 237 |
| 238 test("a different end isn't equal", () { |
| 239 var other = new SourceSpan( |
| 240 new SourceLocation(5, sourceUrl: "foo.dart"), |
| 241 new SourceLocation(16, sourceUrl: "foo.dart"), |
| 242 "foo bar baz"); |
| 243 |
| 244 expect(span, isNot(equals(other))); |
| 245 }); |
| 246 |
| 247 test("a different source URL isn't equal", () { |
| 248 var other = new SourceSpan( |
| 249 new SourceLocation(5, sourceUrl: "bar.dart"), |
| 250 new SourceLocation(12, sourceUrl: "bar.dart"), |
| 251 "foo bar"); |
| 252 |
| 253 expect(span, isNot(equals(other))); |
| 254 }); |
| 255 }); |
| 256 } |
OLD | NEW |