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 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 new SourceSpanWithContext()', () { |
| 40 test('context must contain text', () { |
| 41 var start = new SourceLocation(2); |
| 42 var end = new SourceLocation(5); |
| 43 expect(() => new SourceSpanWithContext( |
| 44 start, end, "abc", "--axc--"), throwsArgumentError); |
| 45 }); |
| 46 |
| 47 test('text starts at start.column in context', () { |
| 48 var start = new SourceLocation(3); |
| 49 var end = new SourceLocation(5); |
| 50 expect(() => new SourceSpanWithContext( |
| 51 start, end, "abc", "--abc--"), throwsArgumentError); |
| 52 }); |
| 53 |
| 54 test('text starts at start.column of line in multi-line context', () { |
| 55 var start = new SourceLocation(4, line: 55, column: 3); |
| 56 var end = new SourceLocation(7, line: 55, column: 6); |
| 57 expect(() => new SourceSpanWithContext( |
| 58 start, end, "abc", "\n--abc--"), throwsArgumentError); |
| 59 expect(() => new SourceSpanWithContext( |
| 60 start, end, "abc", "\n----abc--"), throwsArgumentError); |
| 61 expect(() => new SourceSpanWithContext( |
| 62 start, end, "abc", "\n\n--abc--"), throwsArgumentError); |
| 63 |
| 64 // However, these are valid: |
| 65 new SourceSpanWithContext(start, end, "abc", "\n---abc--"); |
| 66 new SourceSpanWithContext(start, end, "abc", "\n\n---abc--"); |
| 67 }); |
| 68 |
| 69 test('text can occur multiple times in context', () { |
| 70 var start1 = new SourceLocation(4, line: 55, column: 2); |
| 71 var end1 = new SourceLocation(7, line: 55, column: 5); |
| 72 var start2 = new SourceLocation(4, line: 55, column: 8); |
| 73 var end2 = new SourceLocation(7, line: 55, column: 11); |
| 74 new SourceSpanWithContext(start1, end1, "abc", "--abc---abc--\n"); |
| 75 new SourceSpanWithContext(start1, end1, "abc", "--abc--abc--\n"); |
| 76 new SourceSpanWithContext(start2, end2, "abc", "--abc---abc--\n"); |
| 77 new SourceSpanWithContext(start2, end2, "abc", "---abc--abc--\n"); |
| 78 expect(() => new SourceSpanWithContext( |
| 79 start1, end1, "abc", "---abc--abc--\n"), throwsArgumentError); |
| 80 expect(() => new SourceSpanWithContext( |
| 81 start2, end2, "abc", "--abc--abc--\n"), throwsArgumentError); |
| 82 }); |
| 83 }); |
| 84 |
| 85 group('for union()', () { |
| 86 test('source URLs must match', () { |
| 87 var other = new SourceSpan( |
| 88 new SourceLocation(12, sourceUrl: "bar.dart"), |
| 89 new SourceLocation(13, sourceUrl: "bar.dart"), |
| 90 "_"); |
| 91 |
| 92 expect(() => span.union(other), throwsArgumentError); |
| 93 }); |
| 94 |
| 95 test('spans may not be disjoint', () { |
| 96 var other = new SourceSpan( |
| 97 new SourceLocation(13, sourceUrl: 'foo.dart'), |
| 98 new SourceLocation(14, sourceUrl: 'foo.dart'), |
| 99 "_"); |
| 100 |
| 101 expect(() => span.union(other), throwsArgumentError); |
| 102 }); |
| 103 }); |
| 104 |
| 105 test('for compareTo() source URLs must match', () { |
| 106 var other = new SourceSpan( |
| 107 new SourceLocation(12, sourceUrl: "bar.dart"), |
| 108 new SourceLocation(13, sourceUrl: "bar.dart"), |
| 109 "_"); |
| 110 |
| 111 expect(() => span.compareTo(other), throwsArgumentError); |
| 112 }); |
| 113 }); |
| 114 |
| 115 test('fields work correctly', () { |
| 116 expect(span.start, equals(new SourceLocation(5, sourceUrl: "foo.dart"))); |
| 117 expect(span.end, equals(new SourceLocation(12, sourceUrl: "foo.dart"))); |
| 118 expect(span.sourceUrl, equals(Uri.parse("foo.dart"))); |
| 119 expect(span.length, equals(7)); |
| 120 }); |
| 121 |
| 122 group("union()", () { |
| 123 test("works with a preceding adjacent span", () { |
| 124 var other = new SourceSpan( |
| 125 new SourceLocation(0, sourceUrl: "foo.dart"), |
| 126 new SourceLocation(5, sourceUrl: "foo.dart"), |
| 127 "hey, "); |
| 128 |
| 129 var result = span.union(other); |
| 130 expect(result.start, equals(other.start)); |
| 131 expect(result.end, equals(span.end)); |
| 132 expect(result.text, equals("hey, foo bar")); |
| 133 }); |
| 134 |
| 135 test("works with a preceding overlapping span", () { |
| 136 var other = new SourceSpan( |
| 137 new SourceLocation(0, sourceUrl: "foo.dart"), |
| 138 new SourceLocation(8, sourceUrl: "foo.dart"), |
| 139 "hey, foo"); |
| 140 |
| 141 var result = span.union(other); |
| 142 expect(result.start, equals(other.start)); |
| 143 expect(result.end, equals(span.end)); |
| 144 expect(result.text, equals("hey, foo bar")); |
| 145 }); |
| 146 |
| 147 test("works with a following adjacent span", () { |
| 148 var other = new SourceSpan( |
| 149 new SourceLocation(12, sourceUrl: "foo.dart"), |
| 150 new SourceLocation(16, sourceUrl: "foo.dart"), |
| 151 " baz"); |
| 152 |
| 153 var result = span.union(other); |
| 154 expect(result.start, equals(span.start)); |
| 155 expect(result.end, equals(other.end)); |
| 156 expect(result.text, equals("foo bar baz")); |
| 157 }); |
| 158 |
| 159 test("works with a following overlapping span", () { |
| 160 var other = new SourceSpan( |
| 161 new SourceLocation(9, sourceUrl: "foo.dart"), |
| 162 new SourceLocation(16, sourceUrl: "foo.dart"), |
| 163 "bar baz"); |
| 164 |
| 165 var result = span.union(other); |
| 166 expect(result.start, equals(span.start)); |
| 167 expect(result.end, equals(other.end)); |
| 168 expect(result.text, equals("foo bar baz")); |
| 169 }); |
| 170 |
| 171 test("works with an internal overlapping span", () { |
| 172 var other = new SourceSpan( |
| 173 new SourceLocation(7, sourceUrl: "foo.dart"), |
| 174 new SourceLocation(10, sourceUrl: "foo.dart"), |
| 175 "o b"); |
| 176 |
| 177 expect(span.union(other), equals(span)); |
| 178 }); |
| 179 |
| 180 test("works with an external overlapping span", () { |
| 181 var other = new SourceSpan( |
| 182 new SourceLocation(0, sourceUrl: "foo.dart"), |
| 183 new SourceLocation(16, sourceUrl: "foo.dart"), |
| 184 "hey, foo bar baz"); |
| 185 |
| 186 expect(span.union(other), equals(other)); |
| 187 }); |
| 188 }); |
| 189 |
| 190 group("message()", () { |
| 191 test("prints the text being described", () { |
| 192 expect(span.message("oh no"), equals(""" |
| 193 line 1, column 6 of foo.dart: oh no |
| 194 foo bar |
| 195 ^^^^^^^""")); |
| 196 }); |
| 197 |
| 198 test("gracefully handles a missing source URL", () { |
| 199 var span = new SourceSpan( |
| 200 new SourceLocation(5), new SourceLocation(12), "foo bar"); |
| 201 |
| 202 expect(span.message("oh no"), equalsIgnoringWhitespace(""" |
| 203 line 1, column 6: oh no |
| 204 foo bar |
| 205 ^^^^^^^""")); |
| 206 }); |
| 207 |
| 208 test("gracefully handles empty text", () { |
| 209 var span = new SourceSpan( |
| 210 new SourceLocation(5), new SourceLocation(5), ""); |
| 211 |
| 212 expect(span.message("oh no"), |
| 213 equals("line 1, column 6: oh no")); |
| 214 }); |
| 215 |
| 216 test("doesn't colorize if color is false", () { |
| 217 expect(span.message("oh no", color: false), equals(""" |
| 218 line 1, column 6 of foo.dart: oh no |
| 219 foo bar |
| 220 ^^^^^^^""")); |
| 221 }); |
| 222 |
| 223 test("colorizes if color is true", () { |
| 224 expect(span.message("oh no", color: true), |
| 225 equals(""" |
| 226 line 1, column 6 of foo.dart: oh no |
| 227 ${colors.RED}foo bar${colors.NONE} |
| 228 ${colors.RED}^^^^^^^${colors.NONE}""")); |
| 229 }); |
| 230 |
| 231 test("uses the given color if it's passed", () { |
| 232 expect(span.message("oh no", color: colors.YELLOW), equals(""" |
| 233 line 1, column 6 of foo.dart: oh no |
| 234 ${colors.YELLOW}foo bar${colors.NONE} |
| 235 ${colors.YELLOW}^^^^^^^${colors.NONE}""")); |
| 236 }); |
| 237 }); |
| 238 |
| 239 group("message() with context", () { |
| 240 var spanWithContext; |
| 241 setUp(() { |
| 242 spanWithContext = new SourceSpanWithContext( |
| 243 new SourceLocation(5, sourceUrl: "foo.dart"), |
| 244 new SourceLocation(12, sourceUrl: "foo.dart"), |
| 245 "foo bar", |
| 246 "-----foo bar-----"); |
| 247 }); |
| 248 |
| 249 test("underlines under the right column", () { |
| 250 expect(spanWithContext.message("oh no", color: colors.YELLOW), equals(""" |
| 251 line 1, column 6 of foo.dart: oh no |
| 252 -----${colors.YELLOW}foo bar${colors.NONE}----- |
| 253 ${colors.YELLOW}^^^^^^^${colors.NONE}""")); |
| 254 }); |
| 255 |
| 256 test("underlines correctly when text appears twice", () { |
| 257 var span = new SourceSpanWithContext( |
| 258 new SourceLocation(9, column: 9, sourceUrl: "foo.dart"), |
| 259 new SourceLocation(12, column: 12, sourceUrl: "foo.dart"), |
| 260 "foo", |
| 261 "-----foo foo-----"); |
| 262 expect(span.message("oh no", color: colors.YELLOW), equals(""" |
| 263 line 1, column 10 of foo.dart: oh no |
| 264 -----foo ${colors.YELLOW}foo${colors.NONE}----- |
| 265 ${colors.YELLOW}^^^${colors.NONE}""")); |
| 266 }); |
| 267 |
| 268 test("supports lines of preceeding context", () { |
| 269 var span = new SourceSpanWithContext( |
| 270 new SourceLocation(5, line: 3, column: 5, sourceUrl: "foo.dart"), |
| 271 new SourceLocation(12, line: 3, column: 12, sourceUrl: "foo.dart"), |
| 272 "foo bar", |
| 273 "previous\nlines\n-----foo bar-----\nfollowing line\n"); |
| 274 |
| 275 expect(span.message("oh no", color: colors.YELLOW), equals(""" |
| 276 line 4, column 6 of foo.dart: oh no |
| 277 previous |
| 278 lines |
| 279 -----${colors.YELLOW}foo bar${colors.NONE}----- |
| 280 ${colors.YELLOW}^^^^^^^${colors.NONE}""")); |
| 281 }); |
| 282 }); |
| 283 |
| 284 group("compareTo()", () { |
| 285 test("sorts by start location first", () { |
| 286 var other = new SourceSpan( |
| 287 new SourceLocation(6, sourceUrl: "foo.dart"), |
| 288 new SourceLocation(14, sourceUrl: "foo.dart"), |
| 289 "oo bar b"); |
| 290 |
| 291 expect(span.compareTo(other), lessThan(0)); |
| 292 expect(other.compareTo(span), greaterThan(0)); |
| 293 }); |
| 294 |
| 295 test("sorts by length second", () { |
| 296 var other = new SourceSpan( |
| 297 new SourceLocation(5, sourceUrl: "foo.dart"), |
| 298 new SourceLocation(14, sourceUrl: "foo.dart"), |
| 299 "foo bar b"); |
| 300 |
| 301 expect(span.compareTo(other), lessThan(0)); |
| 302 expect(other.compareTo(span), greaterThan(0)); |
| 303 }); |
| 304 |
| 305 test("considers equal spans equal", () { |
| 306 expect(span.compareTo(span), equals(0)); |
| 307 }); |
| 308 }); |
| 309 |
| 310 group("equality", () { |
| 311 test("two spans with the same locations are equal", () { |
| 312 var other = new SourceSpan( |
| 313 new SourceLocation(5, sourceUrl: "foo.dart"), |
| 314 new SourceLocation(12, sourceUrl: "foo.dart"), |
| 315 "foo bar"); |
| 316 |
| 317 expect(span, equals(other)); |
| 318 }); |
| 319 |
| 320 test("a different start isn't equal", () { |
| 321 var other = new SourceSpan( |
| 322 new SourceLocation(0, sourceUrl: "foo.dart"), |
| 323 new SourceLocation(12, sourceUrl: "foo.dart"), |
| 324 "hey, foo bar"); |
| 325 |
| 326 expect(span, isNot(equals(other))); |
| 327 }); |
| 328 |
| 329 test("a different end isn't equal", () { |
| 330 var other = new SourceSpan( |
| 331 new SourceLocation(5, sourceUrl: "foo.dart"), |
| 332 new SourceLocation(16, sourceUrl: "foo.dart"), |
| 333 "foo bar baz"); |
| 334 |
| 335 expect(span, isNot(equals(other))); |
| 336 }); |
| 337 |
| 338 test("a different source URL isn't equal", () { |
| 339 var other = new SourceSpan( |
| 340 new SourceLocation(5, sourceUrl: "bar.dart"), |
| 341 new SourceLocation(12, sourceUrl: "bar.dart"), |
| 342 "foo bar"); |
| 343 |
| 344 expect(span, isNot(equals(other))); |
| 345 }); |
| 346 }); |
| 347 } |
OLD | NEW |