Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1760)

Side by Side Diff: pkg/source_span/test/source_file_test.dart

Issue 381363002: Extract out a source_span package from source_maps. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 6 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(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
8 main() {
9 var file;
10 setUp(() {
11 file = new SourceFile("""
12 foo bar baz
13 whiz bang boom
14 zip zap zop""", url: "foo.dart");
15 });
16
17 group("errors", () {
18 group("for span()", () {
19 test("end must come after start", () {
20 expect(() => file.span(10, 5), throwsArgumentError);
21 });
22
23 test("start may not be negative", () {
24 expect(() => file.span(-1, 5), throwsRangeError);
25 });
26
27 test("end may not be outside the file", () {
28 expect(() => file.span(10, 100), throwsRangeError);
29 });
30 });
31
32 group("for location()", () {
33 test("offset may not be negative", () {
34 expect(() => file.location(-1), throwsRangeError);
35 });
36
37 test("offset may not be outside the file", () {
38 expect(() => file.location(100), throwsRangeError);
39 });
40 });
41
42 group("for getLine()", () {
43 test("offset may not be negative", () {
44 expect(() => file.getLine(-1), throwsRangeError);
45 });
46
47 test("offset may not be outside the file", () {
48 expect(() => file.getLine(100), throwsRangeError);
49 });
50 });
51
52 group("for getColumn()", () {
53 test("offset may not be negative", () {
54 expect(() => file.getColumn(-1), throwsRangeError);
55 });
56
57 test("offset may not be outside the file", () {
58 expect(() => file.getColumn(100), throwsRangeError);
59 });
60
61 test("line may not be negative", () {
62 expect(() => file.getColumn(1, line: -1), throwsRangeError);
63 });
64
65 test("line may not be outside the file", () {
66 expect(() => file.getColumn(1, line: 100), throwsRangeError);
67 });
68
69 test("line must be accurate", () {
70 expect(() => file.getColumn(1, line: 1), throwsRangeError);
71 });
72 });
73
74 group("getOffset()", () {
75 test("line may not be negative", () {
76 expect(() => file.getOffset(-1), throwsRangeError);
77 });
78
79 test("column may not be negative", () {
80 expect(() => file.getOffset(1, -1), throwsRangeError);
81 });
82
83 test("line may not be outside the file", () {
84 expect(() => file.getOffset(100), throwsRangeError);
85 });
86
87 test("column may not be outside the file", () {
88 expect(() => file.getOffset(2, 100), throwsRangeError);
89 });
90
91 test("column may not be outside the line", () {
92 expect(() => file.getOffset(1, 20), throwsRangeError);
93 });
94 });
95
96 group("for getText()", () {
97 test("end must come after start", () {
98 expect(() => file.getText(10, 5), throwsArgumentError);
99 });
100
101 test("start may not be negative", () {
102 expect(() => file.getText(-1, 5), throwsRangeError);
103 });
104
105 test("end may not be outside the file", () {
106 expect(() => file.getText(10, 100), throwsRangeError);
107 });
108 });
109
110 group("for span().union()", () {
111 test("source URLs must match", () {
112 var other = new SourceSpan(
113 new SourceLocation(10), new SourceLocation(11), "_");
114
115 expect(() => file.span(9, 10).union(other), throwsArgumentError);
116 });
117
118 test("spans may not be disjoint", () {
119 expect(() => file.span(9, 10).union(file.span(11, 12)),
120 throwsArgumentError);
121 });
122 });
123 });
124
125 test('fields work correctly', () {
126 expect(file.url, equals(Uri.parse("foo.dart")));
127 expect(file.lines, equals(3));
128 expect(file.length, equals(38));
129 });
130
131 group("new SourceFile()", () {
132 test("handles CRLF correctly", () {
133 expect(new SourceFile("foo\r\nbar").getLine(6), equals(1));
134 });
135
136 test("handles a lone CR correctly", () {
137 expect(new SourceFile("foo\rbar").getLine(5), equals(1));
138 });
139 });
140
141 group("span()", () {
142 test("returns a span between the given offsets", () {
143 var span = file.span(5, 10);
144 expect(span.start, equals(file.location(5)));
145 expect(span.end, equals(file.location(10)));
146 });
147
148 test("end defaults to the end of the file", () {
149 var span = file.span(5);
150 expect(span.start, equals(file.location(5)));
151 expect(span.end, equals(file.location(file.length - 1)));
152 });
153 });
154
155 group("getLine()", () {
156 test("works for a middle character on the line", () {
157 expect(file.getLine(15), equals(1));
158 });
159
160 test("works for the first character of a line", () {
161 expect(file.getLine(12), equals(1));
162 });
163
164 test("works for a newline character", () {
165 expect(file.getLine(11), equals(0));
166 });
167
168 test("works for the last offset", () {
169 expect(file.getLine(file.length), equals(2));
170 });
171 });
172
173 group("getColumn()", () {
174 test("works for a middle character on the line", () {
175 expect(file.getColumn(15), equals(3));
176 });
177
178 test("works for the first character of a line", () {
179 expect(file.getColumn(12), equals(0));
180 });
181
182 test("works for a newline character", () {
183 expect(file.getColumn(11), equals(11));
184 });
185
186 test("works when line is passed as well", () {
187 expect(file.getColumn(12, line: 1), equals(0));
188 });
189
190 test("works for the last offset", () {
191 expect(file.getColumn(file.length), equals(11));
192 });
193 });
194
195 group("getOffset()", () {
196 test("works for a middle character on the line", () {
197 expect(file.getOffset(1, 3), equals(15));
198 });
199
200 test("works for the first character of a line", () {
201 expect(file.getOffset(1), equals(12));
202 });
203
204 test("works for a newline character", () {
205 expect(file.getOffset(0, 11), equals(11));
206 });
207
208 test("works for the last offset", () {
209 expect(file.getOffset(2, 11), equals(file.length));
210 });
211 });
212
213 group("getText()", () {
214 test("returns a substring of the source", () {
215 expect(file.getText(8, 15), equals("baz\nwhi"));
216 });
217
218 test("end defaults to the end of the file", () {
219 expect(file.getText(20), equals("g boom\nzip zap zop"));
220 });
221 });
222
223 group("FileLocation", () {
224 test("reports the correct line number", () {
225 expect(file.location(15).line, equals(1));
226 });
227
228 test("reports the correct column number", () {
229 expect(file.location(15).column, equals(3));
230 });
231
232 test("pointSpan() returns a FileSpan", () {
233 var location = file.location(15);
234 var span = location.pointSpan();
235 expect(span, new isInstanceOf<FileSpan>());
236 expect(span.start, equals(location));
237 expect(span.end, equals(location));
238 expect(span.text, isEmpty);
239 });
240 });
241
242 group("FileSpan", () {
243 test("text returns a substring of the source", () {
244 expect(file.span(8, 15).text, equals("baz\nwhi"));
245 });
246
247 group("union()", () {
248 var span;
249 setUp(() {
250 span = file.span(5, 12);
251 });
252
253 test("works with a preceding adjacent span", () {
254 var other = file.span(0, 5);
255 var result = span.union(other);
256 expect(result.start, equals(other.start));
257 expect(result.end, equals(span.end));
258 expect(result.text, equals("foo bar baz\n"));
259 });
260
261 test("works with a preceding overlapping span", () {
262 var other = file.span(0, 8);
263 var result = span.union(other);
264 expect(result.start, equals(other.start));
265 expect(result.end, equals(span.end));
266 expect(result.text, equals("foo bar baz\n"));
267 });
268
269 test("works with a following adjacent span", () {
270 var other = file.span(12, 16);
271 var result = span.union(other);
272 expect(result.start, equals(span.start));
273 expect(result.end, equals(other.end));
274 expect(result.text, equals("ar baz\nwhiz"));
275 });
276
277 test("works with a following overlapping span", () {
278 var other = file.span(9, 16);
279 var result = span.union(other);
280 expect(result.start, equals(span.start));
281 expect(result.end, equals(other.end));
282 expect(result.text, equals("ar baz\nwhiz"));
283 });
284
285 test("works with an internal overlapping span", () {
286 var other = file.span(7, 10);
287 expect(span.union(other), equals(span));
288 });
289
290 test("works with an external overlapping span", () {
291 var other = file.span(0, 16);
292 expect(span.union(other), equals(other));
293 });
294
295 test("returns a FileSpan for a FileSpan input", () {
296 expect(span.union(file.span(0, 5)), new isInstanceOf<FileSpan>());
297 });
298
299 test("returns a base SourceSpan for a SourceSpan input", () {
300 var other = new SourceSpan(
301 new SourceLocation(0, sourceUrl: "foo.dart"),
302 new SourceLocation(5, sourceUrl: "foo.dart"),
303 "hey, ");
304 var result = span.union(other);
305 expect(result, isNot(new isInstanceOf<FileSpan>()));
306 expect(result.start, equals(other.start));
307 expect(result.end, equals(span.end));
308 expect(result.text, equals("hey, ar baz\n"));
309 });
310 });
311 });
312 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698