OLD | NEW |
1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | 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 | 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. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 import 'package:unittest/unittest.dart'; | 5 import 'package:unittest/unittest.dart'; |
6 import 'package:source_span/source_span.dart'; | 6 import 'package:source_span/source_span.dart'; |
7 import 'package:source_span/src/colors.dart' as colors; | 7 import 'package:source_span/src/colors.dart' as colors; |
8 | 8 |
9 main() { | 9 main() { |
10 var span; | 10 var span; |
(...skipping 18 matching lines...) Expand all Loading... |
29 expect(() => new SourceSpan(start, end, "_"), throwsArgumentError); | 29 expect(() => new SourceSpan(start, end, "_"), throwsArgumentError); |
30 }); | 30 }); |
31 | 31 |
32 test('text must be the right length', () { | 32 test('text must be the right length', () { |
33 var start = new SourceLocation(0); | 33 var start = new SourceLocation(0); |
34 var end = new SourceLocation(1); | 34 var end = new SourceLocation(1); |
35 expect(() => new SourceSpan(start, end, "abc"), throwsArgumentError); | 35 expect(() => new SourceSpan(start, end, "abc"), throwsArgumentError); |
36 }); | 36 }); |
37 }); | 37 }); |
38 | 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 |
39 group('for union()', () { | 70 group('for union()', () { |
40 test('source URLs must match', () { | 71 test('source URLs must match', () { |
41 var other = new SourceSpan( | 72 var other = new SourceSpan( |
42 new SourceLocation(12, sourceUrl: "bar.dart"), | 73 new SourceLocation(12, sourceUrl: "bar.dart"), |
43 new SourceLocation(13, sourceUrl: "bar.dart"), | 74 new SourceLocation(13, sourceUrl: "bar.dart"), |
44 "_"); | 75 "_"); |
45 | 76 |
46 expect(() => span.union(other), throwsArgumentError); | 77 expect(() => span.union(other), throwsArgumentError); |
47 }); | 78 }); |
48 | 79 |
(...skipping 122 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
171 expect(span.message("oh no", color: false), equals(""" | 202 expect(span.message("oh no", color: false), equals(""" |
172 line 1, column 6 of foo.dart: oh no | 203 line 1, column 6 of foo.dart: oh no |
173 foo bar | 204 foo bar |
174 ^^^^^^^""")); | 205 ^^^^^^^""")); |
175 }); | 206 }); |
176 | 207 |
177 test("colorizes if color is true", () { | 208 test("colorizes if color is true", () { |
178 expect(span.message("oh no", color: true), | 209 expect(span.message("oh no", color: true), |
179 equals(""" | 210 equals(""" |
180 line 1, column 6 of foo.dart: oh no | 211 line 1, column 6 of foo.dart: oh no |
181 ${colors.RED}foo bar | 212 ${colors.RED}foo bar${colors.NONE} |
182 ^^^^^^^${colors.NONE}""")); | 213 ${colors.RED}^^^^^^^${colors.NONE}""")); |
183 }); | 214 }); |
184 | 215 |
185 test("uses the given color if it's passed", () { | 216 test("uses the given color if it's passed", () { |
186 expect(span.message("oh no", color: colors.YELLOW), equals(""" | 217 expect(span.message("oh no", color: colors.YELLOW), equals(""" |
187 line 1, column 6 of foo.dart: oh no | 218 line 1, column 6 of foo.dart: oh no |
188 ${colors.YELLOW}foo bar | 219 ${colors.YELLOW}foo bar${colors.NONE} |
189 ^^^^^^^${colors.NONE}""")); | 220 ${colors.YELLOW}^^^^^^^${colors.NONE}""")); |
190 }); | 221 }); |
191 }); | 222 }); |
192 | 223 |
| 224 group("message() with context", () { |
| 225 var spanWithContext; |
| 226 setUp(() { |
| 227 spanWithContext = new SourceSpanWithContext( |
| 228 new SourceLocation(5, sourceUrl: "foo.dart"), |
| 229 new SourceLocation(12, sourceUrl: "foo.dart"), |
| 230 "foo bar", |
| 231 "-----foo bar-----"); |
| 232 }); |
| 233 |
| 234 test("underlines under the right column", () { |
| 235 expect(spanWithContext.message("oh no", color: colors.YELLOW), equals(""" |
| 236 line 1, column 6 of foo.dart: oh no |
| 237 -----${colors.YELLOW}foo bar${colors.NONE}----- |
| 238 ${colors.YELLOW}^^^^^^^${colors.NONE}""")); |
| 239 }); |
| 240 |
| 241 test("supports lines of preceeding context", () { |
| 242 var span = new SourceSpanWithContext( |
| 243 new SourceLocation(5, line: 3, column: 5, sourceUrl: "foo.dart"), |
| 244 new SourceLocation(12, line: 3, column: 12, sourceUrl: "foo.dart"), |
| 245 "foo bar", |
| 246 "previous\nlines\n-----foo bar-----\nfollowing line\n"); |
| 247 |
| 248 expect(span.message("oh no", color: colors.YELLOW), equals(""" |
| 249 line 4, column 6 of foo.dart: oh no |
| 250 previous |
| 251 lines |
| 252 -----${colors.YELLOW}foo bar${colors.NONE}----- |
| 253 ${colors.YELLOW}^^^^^^^${colors.NONE}""")); |
| 254 }); |
| 255 }); |
| 256 |
193 group("compareTo()", () { | 257 group("compareTo()", () { |
194 test("sorts by start location first", () { | 258 test("sorts by start location first", () { |
195 var other = new SourceSpan( | 259 var other = new SourceSpan( |
196 new SourceLocation(6, sourceUrl: "foo.dart"), | 260 new SourceLocation(6, sourceUrl: "foo.dart"), |
197 new SourceLocation(14, sourceUrl: "foo.dart"), | 261 new SourceLocation(14, sourceUrl: "foo.dart"), |
198 "oo bar b"); | 262 "oo bar b"); |
199 | 263 |
200 expect(span.compareTo(other), lessThan(0)); | 264 expect(span.compareTo(other), lessThan(0)); |
201 expect(other.compareTo(span), greaterThan(0)); | 265 expect(other.compareTo(span), greaterThan(0)); |
202 }); | 266 }); |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
247 test("a different source URL isn't equal", () { | 311 test("a different source URL isn't equal", () { |
248 var other = new SourceSpan( | 312 var other = new SourceSpan( |
249 new SourceLocation(5, sourceUrl: "bar.dart"), | 313 new SourceLocation(5, sourceUrl: "bar.dart"), |
250 new SourceLocation(12, sourceUrl: "bar.dart"), | 314 new SourceLocation(12, sourceUrl: "bar.dart"), |
251 "foo bar"); | 315 "foo bar"); |
252 | 316 |
253 expect(span, isNot(equals(other))); | 317 expect(span, isNot(equals(other))); |
254 }); | 318 }); |
255 }); | 319 }); |
256 } | 320 } |
OLD | NEW |