Debian Codegraph @

test/com/google/javascript/jscomp/RecordFunctionInformationTest.java

raw symbols SCIP
1 /*
2 * Copyright 2008 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.common.collect.ImmutableList;
20 import com.google.common.collect.Lists;
21 import com.google.javascript.rhino.Node;
22
23 import junit.framework.TestCase;
24
25 /**
26 * Tests for {@link RecordFunctionInformation}
27 *
28 */
29 public class RecordFunctionInformationTest extends TestCase {
30
31 public void testFunction() {
32 String g = "function g(){}";
33 String fAndG = "function f(){" + g + "}";
34 String js = "var h=" + fAndG + ";h()";
35
36 FunctionInformationMap.Builder expected =
37 FunctionInformationMap.newBuilder();
38 expected.addEntry(
39 FunctionInformationMap.Entry.newBuilder()
40 .setId(0)
41 .setSourceName("testcode")
42 .setLineNumber(1)
43 .setModuleName("")
44 .setSize(g.length())
45 .setName("f::g")
46 .setCompiledSource(g).build());
47 expected.addEntry(
48 FunctionInformationMap.Entry.newBuilder()
49 .setId(1)
50 .setSourceName("testcode")
51 .setLineNumber(1)
52 .setModuleName("")
53 .setSize(fAndG.length())
54 .setName("f")
55 .setCompiledSource(fAndG).build());
56 expected.addModule(
57 FunctionInformationMap.Module.newBuilder()
58 .setName("")
59 .setCompiledSource(js + ";").build());
60
61 test(js, expected.build());
62 }
63
64 public void testModule() {
65 String g = "function g(){}";
66 String fAndG = "function f(){" + g + "}";
67 String m0_js = "var h=" + fAndG + ";h()";
68 String sum = "function(a,b){return a+b}";
69 String m1_js = "var x=" + sum + "(1,2)";
70
71 FunctionInformationMap.Builder expected =
72 FunctionInformationMap.newBuilder();
73 expected.addEntry(
74 FunctionInformationMap.Entry.newBuilder()
75 .setId(0)
76 .setSourceName("i0")
77 .setLineNumber(1)
78 .setModuleName("m0")
79 .setSize(g.length())
80 .setName("f::g")
81 .setCompiledSource(g).build());
82 expected.addEntry(
83 FunctionInformationMap.Entry.newBuilder()
84 .setId(1)
85 .setSourceName("i0")
86 .setLineNumber(1)
87 .setModuleName("m0")
88 .setSize(fAndG.length())
89 .setName("f")
90 .setCompiledSource(fAndG).build());
91 expected.addEntry(
92 FunctionInformationMap.Entry.newBuilder()
93 .setId(2)
94 .setSourceName("i1")
95 .setLineNumber(1)
96 .setModuleName("m1")
97 .setSize(sum.length())
98 .setName("<anonymous>")
99 .setCompiledSource(sum).build());
100 expected.addModule(
101 FunctionInformationMap.Module.newBuilder()
102 .setName("m0")
103 .setCompiledSource(m0_js + ";").build());
104 expected.addModule(
105 FunctionInformationMap.Module.newBuilder()
106 .setName("m1")
107 .setCompiledSource(m1_js + ";").build());
108
109 test(CompilerTestCase.createModules(m0_js, m1_js), expected.build());
110 }
111
112 public void testMotionPreservesOriginalSourceName() {
113 String f = "function f(){}";
114 String g = "function g(){}";
115
116 String m0_before = f + g;
117 String m1_before = "";
118
119 JSModule[] modules = CompilerTestCase.createModules(m0_before, m1_before);
120 Compiler compiler = compilerFor(modules);
121 Node root = root(compiler);
122 Node externsRoot = externs(root);
123 Node mainRoot = main(root);
124
125 String m0_after = f;
126 String m1_after = g;
127 Node nodeG = mainRoot.getFirstChild().getLastChild();
128 mainRoot.getFirstChild().removeChild(nodeG);
129 mainRoot.getLastChild().addChildrenToBack(nodeG.cloneTree());
130
131 FunctionInformationMap.Builder expected =
132 FunctionInformationMap.newBuilder();
133 expected.addEntry(
134 FunctionInformationMap.Entry.newBuilder()
135 .setId(0)
136 .setSourceName("i0")
137 .setLineNumber(1)
138 .setModuleName("m0")
139 .setSize(g.length())
140 .setName("f")
141 .setCompiledSource(f).build());
142 expected.addEntry(
143 FunctionInformationMap.Entry.newBuilder()
144 .setId(1)
145 .setSourceName("i0")
146 .setLineNumber(1)
147 .setModuleName("m1")
148 .setSize(g.length())
149 .setName("g")
150 .setCompiledSource(g).build());
151 expected.addModule(
152 FunctionInformationMap.Module.newBuilder()
153 .setName("m0")
154 .setCompiledSource(m0_after + ";").build());
155 expected.addModule(
156 FunctionInformationMap.Module.newBuilder()
157 .setName("m1")
158 .setCompiledSource(m1_after + ";").build());
159
160 test(compiler, externsRoot, mainRoot, expected.build());
161 }
162
163
164 private void test(String js, FunctionInformationMap expected) {
165 Compiler compiler = new Compiler();
166 compiler.init(ImmutableList.of(SourceFile.fromCode("externs", "")),
167 ImmutableList.of(SourceFile.fromCode("testcode", js)),
168 new CompilerOptions());
169 test(compiler, expected);
170 }
171
172 private void test(JSModule[] modules, FunctionInformationMap expected) {
173 test(compilerFor(modules), expected);
174 }
175
176 private void test(Compiler compiler, FunctionInformationMap expected) {
177 Node root = root(compiler);
178 test(compiler, externs(root), main(root), expected);
179 }
180
181 private void test(Compiler compiler, Node externsRoot, Node mainRoot,
182 FunctionInformationMap expected) {
183 FunctionNames functionNames = new FunctionNames(compiler);
184 functionNames.process(externsRoot, mainRoot);
185
186 RecordFunctionInformation processor =
187 new RecordFunctionInformation(compiler, functionNames);
188 processor.process(externsRoot, mainRoot);
189 FunctionInformationMap result = processor.getMap();
190 assertEquals(expected, result);
191 }
192
193 private Compiler compilerFor(JSModule[] modules) {
194 Compiler compiler = new Compiler();
195 compiler.initModules(
196 ImmutableList.of(SourceFile.fromCode("externs", "")),
197 Lists.newArrayList(modules),
198 new CompilerOptions());
199 return compiler;
200 }
201
202 private Node root(Compiler compiler) {
203 return compiler.parseInputs();
204 }
205
206 private Node externs(Node root) {
207 return root.getFirstChild();
208 }
209
210 private Node main(Node root) {
211 return root.getFirstChild().getNext();
212 }
213 }