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()", () { | |
Siggi Cherem (dart-lang)
2014/07/16 21:26:10
Let's bring back some of the tests we have in the
nweiz
2014/07/17 20:22:08
I'm not a big fan of automating test generation li
Siggi Cherem (dart-lang)
2014/07/17 22:24:39
ok - one that just crossed my mind is to test `fil
nweiz
2014/07/17 23:18:15
Done.
| |
145 test("prints the text being described", () { | |
146 expect(span.message("oh no"), equalsIgnoringWhitespace(""" | |
Siggi Cherem (dart-lang)
2014/07/16 21:26:10
I'd remove the 'equalsIgnoringWhitespace and just
nweiz
2014/07/17 20:22:08
Done.
| |
147 line 1, column 6 of foo.dart: oh no | |
148 foo bar | |
149 ^^^^^^^ | |
150 """)); | |
151 }); | |
152 | |
153 test("gracefully handles a missing source URL", () { | |
154 var span = new SourceSpan( | |
155 new SourceLocation(5), new SourceLocation(12), "foo bar"); | |
156 | |
157 expect(span.message("oh no"), equalsIgnoringWhitespace(""" | |
158 line 1, column 6: oh no | |
159 foo bar | |
160 ^^^^^^^ | |
161 """)); | |
162 }); | |
163 | |
164 test("gracefully handles empty text", () { | |
165 var span = new SourceSpan( | |
166 new SourceLocation(5), new SourceLocation(5), ""); | |
167 | |
168 expect(span.message("oh no"), | |
169 equals("line 1, column 6: oh no")); | |
170 }); | |
171 | |
172 test("doesn't colorize if color is false", () { | |
173 expect(span.message("oh no", color: false), | |
174 equalsIgnoringWhitespace(""" | |
175 line 1, column 6 of foo.dart: oh no | |
176 foo bar | |
177 ^^^^^^^ | |
178 """)); | |
179 }); | |
180 | |
181 test("colorizes if color is true", () { | |
182 expect(span.message("oh no", color: true), | |
183 equalsIgnoringWhitespace(""" | |
184 line 1, column 6 of foo.dart: oh no | |
185 ${colors.RED}foo bar | |
186 ^^^^^^^${colors.NONE} | |
187 """)); | |
188 }); | |
189 | |
190 test("uses the given color if it's passed", () { | |
191 expect(span.message("oh no", color: colors.YELLOW), | |
192 equalsIgnoringWhitespace(""" | |
193 line 1, column 6 of foo.dart: oh no | |
194 ${colors.YELLOW}foo bar | |
195 ^^^^^^^${colors.NONE} | |
196 """)); | |
197 }); | |
198 }); | |
199 | |
200 group("compareTo()", () { | |
201 test("sorts by start location first", () { | |
202 var other = new SourceSpan( | |
203 new SourceLocation(6, sourceUrl: "foo.dart"), | |
204 new SourceLocation(14, sourceUrl: "foo.dart"), | |
205 "oo bar b"); | |
206 | |
207 expect(span.compareTo(other), lessThan(0)); | |
208 expect(other.compareTo(span), greaterThan(0)); | |
209 }); | |
210 | |
211 test("sorts by length second", () { | |
212 var other = new SourceSpan( | |
213 new SourceLocation(5, sourceUrl: "foo.dart"), | |
214 new SourceLocation(14, sourceUrl: "foo.dart"), | |
215 "foo bar b"); | |
216 | |
217 expect(span.compareTo(other), lessThan(0)); | |
218 expect(other.compareTo(span), greaterThan(0)); | |
219 }); | |
220 | |
221 test("considers equal spans equal", () { | |
222 expect(span.compareTo(span), equals(0)); | |
223 }); | |
224 }); | |
225 | |
226 group("equality", () { | |
227 test("two spans with the same locations are equal", () { | |
228 var other = new SourceSpan( | |
229 new SourceLocation(5, sourceUrl: "foo.dart"), | |
230 new SourceLocation(12, sourceUrl: "foo.dart"), | |
231 "foo bar"); | |
232 | |
233 expect(span, equals(other)); | |
234 }); | |
235 | |
236 test("a different start isn't equal", () { | |
237 var other = new SourceSpan( | |
238 new SourceLocation(0, sourceUrl: "foo.dart"), | |
239 new SourceLocation(12, sourceUrl: "foo.dart"), | |
240 "hey, foo bar"); | |
241 | |
242 expect(span, isNot(equals(other))); | |
243 }); | |
244 | |
245 test("a different end isn't equal", () { | |
246 var other = new SourceSpan( | |
247 new SourceLocation(5, sourceUrl: "foo.dart"), | |
248 new SourceLocation(16, sourceUrl: "foo.dart"), | |
249 "foo bar baz"); | |
250 | |
251 expect(span, isNot(equals(other))); | |
252 }); | |
253 | |
254 test("a different source URL isn't equal", () { | |
255 var other = new SourceSpan( | |
256 new SourceLocation(5, sourceUrl: "bar.dart"), | |
257 new SourceLocation(12, sourceUrl: "bar.dart"), | |
258 "foo bar"); | |
259 | |
260 expect(span, isNot(equals(other))); | |
261 }); | |
262 }); | |
263 } | |
OLD | NEW |