Debian Codegraph @

test/com/google/javascript/jscomp/SourceMapTest.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.javascript.jscomp;
18
19 import com.google.debugging.sourcemap.SourceMapConsumer;
20 import com.google.debugging.sourcemap.SourceMapConsumerV2;
21 import com.google.debugging.sourcemap.SourceMapTestCase;
22 import com.google.javascript.jscomp.SourceMap.Format;
23
24 import java.io.IOException;
25 import java.util.ArrayList;
26 import java.util.List;
27
28 /**
29 *
30 * @author [email protected] (John Lenz)
31 */
32 public class SourceMapTest extends SourceMapTestCase {
33
34 public SourceMapTest() {
35 }
36
37 private List<SourceMap.LocationMapping> mappings;
38
39 public void testPrefixReplacement1() throws IOException {
40 mappings = new ArrayList<SourceMap.LocationMapping>();
41 // mapping can be used to remove a prefix
42 mappings.add( new SourceMap.LocationMapping("pre/","") );
43 checkSourceMap2("", "pre/file1", "", "pre/file2" , "{\n" +
44 "\"version\":2,\n" +
45 "\"file\":\"testcode\",\n" +
46 "\"lineCount\":1,\n" +
47 "\"lineMaps\":[\"\"],\n" +
48 "\"mappings\":[],\n" +
49 "\"sources\":[\"file1\",\"file2\"],\n" +
50 "\"names\":[]\n" +
51 "}\n");
52 }
53
54 public void testPrefixReplacement2() throws IOException {
55 mappings = new ArrayList<SourceMap.LocationMapping>();
56 // mapping can be used to replace a prefix
57 mappings.add( new SourceMap.LocationMapping("pre/file","src") );
58 checkSourceMap2("", "pre/file1", "", "pre/file2" , "{\n" +
59 "\"version\":2,\n" +
60 "\"file\":\"testcode\",\n" +
61 "\"lineCount\":1,\n" +
62 "\"lineMaps\":[\"\"],\n" +
63 "\"mappings\":[],\n" +
64 "\"sources\":[\"src1\",\"src2\"],\n" +
65 "\"names\":[]\n" +
66 "}\n");
67 }
68
69 public void testPrefixReplacement3() throws IOException {
70 mappings = new ArrayList<SourceMap.LocationMapping>();
71 // multiple mappings can be applied
72 mappings.add( new SourceMap.LocationMapping("file1","x") );
73 mappings.add( new SourceMap.LocationMapping("file2","y") );
74 checkSourceMap2("", "file1", "", "file2" , "{\n" +
75 "\"version\":2,\n" +
76 "\"file\":\"testcode\",\n" +
77 "\"lineCount\":1,\n" +
78 "\"lineMaps\":[\"\"],\n" +
79 "\"mappings\":[],\n" +
80 "\"sources\":[\"x\",\"y\"],\n" +
81 "\"names\":[]\n" +
82 "}\n");
83 }
84
85 public void testPrefixReplacement4() throws IOException {
86 mappings = new ArrayList<SourceMap.LocationMapping>();
87 // first match wins
88 mappings.add( new SourceMap.LocationMapping("file1","x") );
89 mappings.add( new SourceMap.LocationMapping("file","y") );
90 checkSourceMap2("", "file1", "", "file2" , "{\n" +
91 "\"version\":2,\n" +
92 "\"file\":\"testcode\",\n" +
93 "\"lineCount\":1,\n" +
94 "\"lineMaps\":[\"\"],\n" +
95 "\"mappings\":[],\n" +
96 "\"sources\":[\"x\",\"y2\"],\n" +
97 "\"names\":[]\n" +
98 "}\n");
99 }
100
101 @Override
102 protected CompilerOptions getCompilerOptions() {
103 CompilerOptions options = super.getCompilerOptions();
104 if (mappings != null) {
105 options.sourceMapLocationMappings = mappings;
106 }
107 return options;
108 }
109
110 @Override
111 public void setUp() {
112 super.setUp();
113 }
114
115 private void checkSourceMap2(
116 String js1, String file1, String js2, String file2, String expectedMap)
117 throws IOException {
118 RunResult result = compile(js1, file1, js2, file2);
119 assertEquals(expectedMap, result.sourceMapFileContent);
120 assertEquals(result.sourceMapFileContent, getSourceMap(result));
121 }
122
123 @Override
124 protected Format getSourceMapFormat() {
125 return Format.V2;
126 }
127
128 @Override
129 protected SourceMapConsumer getSourceMapConsumer() {
130 return new SourceMapConsumerV2();
131 }
132 }