Debian Codegraph @

test/com/google/debugging/sourcemap/SourceMapConsumerV2Test.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.SourceMapConsumerV2;
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 SourceMapConsumerV2Test extends TestCase {
26
27 public SourceMapConsumerV2Test() {
28 }
29
30 public SourceMapConsumerV2Test(String name) {
31 super(name);
32 }
33
34 public void testEmptyMap() throws Exception {
35 StringBuilder sb = new StringBuilder();
36 sb.append("{\n");
37 sb.append("\"version\": 2,\n");
38 sb.append("\"file\": \"somefile.js\",\n");
39 sb.append("\"lineCount\": 0,\n");
40 sb.append("\"lineMaps\": [],\n");
41 sb.append("\"sources\": [],\n");
42 sb.append("\"mappings\": []\n");
43 sb.append("}\n");
44
45 SourceMapConsumerV2 sourceMap = new SourceMapConsumerV2();
46 sourceMap.parse(sb.toString());
47 }
48
49 public void testGetMappingForLine() throws Exception {
50 // Input Code: function f(foo, bar) { foo = foo + bar + 2; return foo; }
51 String mapData =
52 "{\n" +
53 "\"version\":2,\n" +
54 "\"file\":\"testcode\",\n" +
55 "\"lineCount\":1,\n" +
56 "\"lineMaps\":" +
57 "[\"cAEBABIBA/ICA+ADICA/ICA+IDA9AEYBMBA5\"],\n" +
58 "\"sources\":[\"testcode\"],\n" +
59 "\"mappings\":[[0,1,9,\"f\"],\n" +
60 "[0,1,9,\"f\"],\n" +
61 "[0,1,10],\n" +
62 "[0,1,11,\"foo\"],\n" +
63 "[0,1,16,\"bar\"],\n" +
64 "[0,1,21],\n" +
65 "[0,1,23],\n" +
66 "[0,1,23,\"foo\"],\n" +
67 "[0,1,29,\"foo\"],\n" +
68 "[0,1,35,\"bar\"],\n" +
69 "[0,1,41],\n" +
70 "[0,1,44],\n" +
71 "[0,1,51,\"foo\"],\n" +
72 "]\n" +
73 "}\n";
74
75 SourceMapConsumerV2 sourceMap = new SourceMapConsumerV2();
76 sourceMap.parse(mapData);
77
78 OriginalMapping mapping = sourceMap.getMappingForLine(1, 10);
79
80 assertNotNull(mapping);
81 assertEquals("testcode", mapping.getOriginalFile());
82 assertEquals(1, mapping.getLineNumber());
83 assertEquals(9, mapping.getColumnPosition());
84 assertEquals("f", mapping.getIdentifier());
85
86 mapping = sourceMap.getMappingForLine(1, 40);
87
88 assertNotNull(mapping);
89 assertEquals("testcode", mapping.getOriginalFile());
90 assertEquals(1, mapping.getLineNumber());
91 assertEquals(44, mapping.getColumnPosition());
92 assertEquals("", mapping.getIdentifier());
93
94 mapping = sourceMap.getMappingForLine(1, 42);
95 assertNotNull(mapping);
96 assertEquals("testcode", mapping.getOriginalFile());
97 assertEquals(1, mapping.getLineNumber());
98 assertEquals(51, mapping.getColumnPosition());
99 assertEquals("foo", mapping.getIdentifier());
100
101 assertNull(sourceMap.getMappingForLine(Integer.MAX_VALUE, 1));
102 assertNull(sourceMap.getMappingForLine(1, Integer.MAX_VALUE));
103 }
104
105 public void testGetMappingForLineWithNameIndex() throws Exception {
106 String mapData =
107 "{\n" +
108 "\"version\":2,\n" +
109 "\"file\":\"testcode\",\n" +
110 "\"lineCount\":1,\n" +
111 "\"lineMaps\":" +
112 "[\"cAEBABIBA/ICA+ADICA/ICA+IDA9AEYBMBA5\"],\n" +
113 "\"sources\":[\"testcode\"],\n" +
114 "\"names\": [\"f\"],\n" +
115 "\"mappings\":[[0,1,9,0],\n" +
116 "[0,1,9,0]\n" +
117 "]\n" +
118 "}\n";
119
120 SourceMapConsumerV2 sourceMap = new SourceMapConsumerV2();
121 sourceMap.parse(mapData);
122
123 OriginalMapping mapping = sourceMap.getMappingForLine(1, 10);
124
125 assertNotNull(mapping);
126 assertEquals("testcode", mapping.getOriginalFile());
127 assertEquals(1, mapping.getLineNumber());
128 assertEquals(9, mapping.getColumnPosition());
129 assertEquals("f", mapping.getIdentifier());
130 }
131
132 public void testInvalidJSONFailure() throws Exception {
133 StringBuilder sb = new StringBuilder();
134 sb.append("notjson");
135
136 assertExceptionStartsWith("JSON parse exception: org.json.JSONException: "
137 + "A JSONObject text must begin "
138 + "with '{' at character 1", sb);
139 }
140
141 public void testUnknownVersion() throws Exception {
142 StringBuilder sb = new StringBuilder();
143 sb.append("{\"version\": 3}");
144 assertException("Unknown version: 3", sb);
145 }
146
147 public void testMissingFile() throws Exception {
148 StringBuilder sb = new StringBuilder();
149 sb.append("{\"version\": 2, \"file\": \"\"}");
150 assertException("File entry is missing or empty", sb);
151 }
152
153 private void assertException(String exception, StringBuilder sb) {
154 boolean exceptionRaised = false;
155
156 try {
157 SourceMapConsumerV2 sourceMap = new SourceMapConsumerV2();
158 sourceMap.parse(sb.toString());
159
160 } catch (SourceMapParseException pe) {
161 assertEquals(exception, pe.getMessage());
162 exceptionRaised = true;
163 }
164
165 assertTrue(exceptionRaised);
166 }
167
168 private void assertExceptionStartsWith(String exception, StringBuilder sb) {
169 boolean exceptionRaised = false;
170
171 try {
172 SourceMapConsumerV2 sourceMap = new SourceMapConsumerV2();
173 sourceMap.parse(sb.toString());
174
175 } catch (SourceMapParseException pe) {
176 assertTrue(
177 "expected <" + exception +"> but was <"+ pe.getMessage() +">",
178 pe.getMessage().startsWith(exception));
179 exceptionRaised = true;
180 }
181
182 assertTrue(exceptionRaised);
183 }
184 }