Debian Codegraph @

test/com/google/debugging/sourcemap/SourceMapConsumerV1Test.java

raw symbols SCIP
1 /*
2 * Copyright 2009 The Closure Compiler Authors.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 package com.google.debugging.sourcemap;
18
19 import com.google.debugging.sourcemap.SourceMapConsumerV1;
20 import com.google.debugging.sourcemap.SourceMapParseException;
21 import com.google.debugging.sourcemap.proto.Mapping.OriginalMapping;
22
23 import junit.framework.TestCase;
24
25 public class SourceMapConsumerV1Test extends TestCase {
26
27 public SourceMapConsumerV1Test() {
28 }
29
30 public SourceMapConsumerV1Test(String name) {
31 super(name);
32 }
33
34 public void testGetMappingForLine() throws Exception {
35 StringBuilder sb = new StringBuilder();
36 sb.append("/** Begin line maps. **/{ count : 2 }\n");
37 sb.append("[0,,,,1,,2]\n");
38 sb.append("[3,,,,,,,]\n");
39 sb.append("/** Begin file information. **/\n");
40 sb.append("['test.js']\n");
41 sb.append("['foo.js']\n");
42 sb.append("/** Begin mapping definitions. **/\n");
43 sb.append("['test.js', 0, 1]\n");
44 sb.append("['bleg.js', 5, 8, 'hello']\n");
45 sb.append("['bleg.js', 12, 78]\n");
46 sb.append("['foo.js', 15, 16, 'yo!']");
47
48 SourceMapConsumerV1 sourceMap = new SourceMapConsumerV1();
49 sourceMap.parse(sb.toString());
50
51 OriginalMapping mapping = sourceMap.getMappingForLine(1, 1);
52
53 assertNotNull(mapping);
54 assertEquals("test.js", mapping.getOriginalFile());
55 assertEquals(0, mapping.getLineNumber());
56 assertEquals(1, mapping.getColumnPosition());
57 assertEquals("", mapping.getIdentifier());
58
59 mapping = sourceMap.getMappingForLine(1, 6);
60 assertNotNull(mapping);
61 assertEquals("bleg.js", mapping.getOriginalFile());
62 assertEquals(5, mapping.getLineNumber());
63 assertEquals(8, mapping.getColumnPosition());
64 assertEquals("hello", mapping.getIdentifier());
65
66 mapping = sourceMap.getMappingForLine(2, 4);
67 assertNotNull(mapping);
68 assertEquals("foo.js", mapping.getOriginalFile());
69 assertEquals(15, mapping.getLineNumber());
70 assertEquals(16, mapping.getColumnPosition());
71 assertEquals("yo!", mapping.getIdentifier());
72
73 assertNull(sourceMap.getMappingForLine(Integer.MAX_VALUE, 1));
74 assertNotNull(sourceMap.getMappingForLine(1, Integer.MAX_VALUE));
75 }
76
77 public void testLineEdges() throws Exception {
78 StringBuilder sb = new StringBuilder();
79 sb.append("/** Begin line maps. **/{ count : 2 }\n");
80 sb.append("[100]\n");
81 sb.append("[200]\n");
82 sb.append("/** Begin file information. **/\n");
83 sb.append("['test.js']\n");
84 sb.append("['foo.js']\n");
85 sb.append("/** Begin mapping definitions. **/\n");
86 for (int i = 0; i <= 200; i++) {
87 sb.append("['foo.js', ").append(i).append(", 1]\n");
88 }
89
90 SourceMapConsumerV1 sourceMap = new SourceMapConsumerV1();
91 sourceMap.parse(sb.toString());
92
93 OriginalMapping mapping = sourceMap.getMappingForLine(-1, 1);
94 assertNull(mapping);
95
96 mapping = sourceMap.getMappingForLine(0, 1);
97 assertNull(mapping);
98
99 mapping = sourceMap.getMappingForLine(1, 1);
100 assertEquals(100, mapping.getLineNumber());
101
102 mapping = sourceMap.getMappingForLine(2, 1);
103 assertEquals(200, mapping.getLineNumber());
104
105 mapping = sourceMap.getMappingForLine(3, 1);
106 assertNull(mapping);
107 }
108
109 public void testColumnEdges() throws Exception {
110 StringBuilder sb = new StringBuilder();
111 sb.append("/** Begin line maps. **/{ count : 1 }\n");
112 sb.append("[100, 101]\n");
113 sb.append("/** Begin file information. **/\n");
114 sb.append("\n");
115 sb.append("/** Begin mapping definitions. **/\n");
116 for (int i = 0; i <= 200; i++) {
117 sb.append("['foo.js', ").append(i).append(", 1]\n");
118 }
119
120 SourceMapConsumerV1 sourceMap = new SourceMapConsumerV1();
121 sourceMap.parse(sb.toString());
122
123 OriginalMapping mapping = sourceMap.getMappingForLine(1, -1);
124 assertNull(mapping);
125
126 mapping = sourceMap.getMappingForLine(1, 0);
127 assertNull(mapping);
128
129 mapping = sourceMap.getMappingForLine(1, 1);
130 assertEquals(100, mapping.getLineNumber());
131
132 mapping = sourceMap.getMappingForLine(1, 2);
133 assertEquals(101, mapping.getLineNumber());
134
135 // Columns beyond the end of the line are treated the same
136 // as the last column.
137 mapping = sourceMap.getMappingForLine(1, 3);
138 assertEquals(101, mapping.getLineNumber());
139 }
140
141 public void testNegativeOneInLineMap() throws Exception {
142 StringBuilder sb = new StringBuilder();
143 sb.append("/** Begin line maps. **/{ count : 1 }\n");
144 sb.append("[200,-1,199,-1]\n");
145 sb.append("/** Begin file information. **/\n");
146 sb.append("\n");
147 sb.append("/** Begin mapping definitions. **/\n");
148 for (int i = 0; i <= 200; i++) {
149 sb.append("['foo.js', 1, ").append(i).append("]\n");
150 }
151
152 SourceMapConsumerV1 sourceMap = new SourceMapConsumerV1();
153 sourceMap.parse(sb.toString());
154
155 OriginalMapping mapping = sourceMap.getMappingForLine(1, 2);
156 assertNull(mapping);
157
158 mapping = sourceMap.getMappingForLine(1, 4);
159 assertNull(mapping);
160 }
161
162 public void testSimpleParse() throws Exception {
163 StringBuilder sb = new StringBuilder();
164 sb.append("/** Begin line maps. **/{ count : 1 }\n");
165 sb.append("[0,,,,1,2]\n");
166 sb.append("/** Begin file information. **/\n");
167 sb.append("['test.js']\n");
168 sb.append("/** Begin mapping definitions. **/\n");
169 sb.append("['test.js', 0, 1]\n");
170 sb.append("['test.js', 0, 1, 'hello']\n");
171 sb.append("['test.js', 0, 1]");
172
173 SourceMapConsumerV1 sourceMap = new SourceMapConsumerV1();
174 sourceMap.parse(sb.toString());
175 }
176
177 public void testBlankLine() throws Exception {
178 StringBuilder sb = new StringBuilder();
179 sb.append("/** Begin line maps. **/{ count : 1 }\n");
180 sb.append("\n");
181 sb.append("/** Begin file information. **/\n");
182 sb.append("\n");
183 sb.append("/** Begin mapping definitions. **/\n");
184
185 SourceMapConsumerV1 sourceMap = new SourceMapConsumerV1();
186 sourceMap.parse(sb.toString());
187 }
188
189 public void testCountFailure() throws Exception {
190 StringBuilder sb = new StringBuilder();
191 sb.append("/** Begin line maps. **/{ counter : 1 }\n");
192
193 assertException("Missing 'count'", sb);
194 }
195
196 public void testInvalidCountFailure() throws Exception {
197 StringBuilder sb = new StringBuilder();
198 sb.append("/** Begin line maps. **/{ count : 0 }\n");
199
200 assertException("Count must be >= 1", sb);
201 }
202
203 public void testInvalidJSONFailure() throws Exception {
204 StringBuilder sb = new StringBuilder();
205 sb.append("/** Begin line maps. **/{ count : 2 }\n");
206 sb.append("[0,,,,2\n");
207
208 assertExceptionStartsWith("JSON parse exception: org.json.JSONException: " +
209 "Expected a ',' or ']' at ", sb);
210 }
211
212 public void testInvalidHeaderFailure() throws Exception {
213 StringBuilder sb = new StringBuilder();
214 sb.append("/** Begin line maps. **/{ count : 1 }\n");
215 sb.append("[0,,,1]\n");
216 sb.append("[3,,,4]\n");
217
218 assertException(
219 "Expected /** Begin file information. **/ got [3,,,4]", sb);
220 }
221
222 public void testInvalidPostHeaderToken() throws Exception {
223 StringBuilder sb = new StringBuilder();
224 sb.append("/** Begin line maps. **/{ count : 1 }\n");
225 sb.append("[0,,,1]\n");
226 sb.append("/** Begin file information. **/f\n");
227
228 assertException("Expected /** Begin file information. **/"
229 + " got /** Begin file information. **/f", sb);
230 }
231
232 public void testInvalidMappingArrayFailure() throws Exception {
233 StringBuilder sb = new StringBuilder();
234 sb.append("/** Begin line maps. **/{ count : 1 }\n");
235 sb.append("[0,,,,1,2]\n");
236 sb.append("/** Begin file information. **/\n");
237 sb.append("['test.js']\n");
238 sb.append("/** Begin mapping definitions. **/\n");
239 sb.append("['test.js', 0]\n");
240
241 assertException("Invalid mapping array", sb);
242 }
243
244 public void testMultipleLineFragments() throws Exception {
245 StringBuilder sb = new StringBuilder();
246 sb.append("/** Begin line maps. **/{ count : 1 }\n");
247 sb.append("[0,1,2,3,260,261,262]\n");
248 sb.append("/** Begin file information. **/\n");
249 sb.append("\n");
250 sb.append("/** Begin mapping definitions. **/\n");
251 for (int i = 0; i < 262; i++) {
252 sb.append("['frog/test" + i + ".js', " + i + ", 1]\n");
253 }
254 sb.append("['frog/testigloo.js', 500, 1]");
255
256 SourceMapConsumerV1 sourceMap = new SourceMapConsumerV1();
257 sourceMap.parse(sb.toString());
258
259 OriginalMapping mapping = sourceMap.getMappingForLine(1, 1);
260
261 assertNotNull(mapping);
262 assertEquals("frog/test0.js", mapping.getOriginalFile());
263 assertEquals(0, mapping.getLineNumber());
264 assertEquals(1, mapping.getColumnPosition());
265 assertEquals("", mapping.getIdentifier());
266
267 mapping = sourceMap.getMappingForLine(1, 6);
268 assertNotNull(mapping);
269 assertEquals("frog/test261.js", mapping.getOriginalFile());
270 assertEquals(261, mapping.getLineNumber());
271 assertEquals(1, mapping.getColumnPosition());
272 }
273
274 public void testMultipleMappingFragments() throws Exception {
275 StringBuilder sb = new StringBuilder();
276 sb.append("/** Begin line maps. **/{ count : 1 }\n");
277 sb.append("[0,1,2,3,260,261,262,1023]\n");
278 sb.append("/** Begin file information. **/\n");
279 sb.append("\n");
280 sb.append("/** Begin mapping definitions. **/\n");
281 for (int i = 0; i < 2000; i++) {
282 sb.append("['frog/test" + (i / 100) + ".js', " + i + ", 1]\n");
283 }
284 sb.append("['frog/testigloo.js', 500, 1]");
285
286 SourceMapConsumerV1 sourceMap = new SourceMapConsumerV1();
287 sourceMap.parse(sb.toString());
288
289 OriginalMapping mapping = sourceMap.getMappingForLine(1, 1);
290
291 assertNotNull(mapping);
292 assertEquals("frog/test0.js", mapping.getOriginalFile());
293 assertEquals(0, mapping.getLineNumber());
294 assertEquals(1, mapping.getColumnPosition());
295 assertEquals("", mapping.getIdentifier());
296
297 mapping = sourceMap.getMappingForLine(1, 6);
298 assertNotNull(mapping);
299 assertEquals("frog/test2.js", mapping.getOriginalFile());
300 assertEquals(261, mapping.getLineNumber());
301 assertEquals(1, mapping.getColumnPosition());
302
303 mapping = sourceMap.getMappingForLine(1, 8);
304 assertNotNull(mapping);
305 assertEquals("frog/test10.js", mapping.getOriginalFile());
306 assertEquals(1023, mapping.getLineNumber());
307 assertEquals(1, mapping.getColumnPosition());
308 }
309
310 private void assertException(String exception, StringBuilder sb) {
311 boolean exceptionRaised = false;
312
313 try {
314 SourceMapConsumerV1 sourceMap = new SourceMapConsumerV1();
315 sourceMap.parse(sb.toString());
316
317 } catch (SourceMapParseException pe) {
318 assertEquals(exception, pe.getMessage());
319 exceptionRaised = true;
320 }
321
322 assertTrue(exceptionRaised);
323 }
324
325 private void assertExceptionStartsWith(String exception, StringBuilder sb) {
326 boolean exceptionRaised = false;
327
328 try {
329 SourceMapConsumerV1 sourceMap = new SourceMapConsumerV1();
330 sourceMap.parse(sb.toString());
331
332 } catch (SourceMapParseException pe) {
333 assertTrue(
334 "expected <" + exception +"> but was <"+ pe.getMessage() +">",
335 pe.getMessage().startsWith(exception));
336 exceptionRaised = true;
337 }
338
339 assertTrue(exceptionRaised);
340 }
341
342 }