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