Debian Codegraph @

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

raw symbols SCIP
1 /*
2 * Copyright 2011 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 static com.google.javascript.jscomp.ReferenceCollectingCallback.Reference.createRefForTest;
20
21 import com.google.common.collect.Lists;
22 import com.google.common.collect.Maps;
23 import com.google.javascript.jscomp.ReferenceCollectingCallback.Reference;
24 import com.google.javascript.jscomp.ReferenceCollectingCallback.ReferenceCollection;
25 import com.google.javascript.jscomp.Scope.Var;
26 import com.google.javascript.rhino.Node;
27 import com.google.javascript.rhino.Token;
28
29 import junit.framework.TestCase;
30
31 import java.util.Map;
32
33 /**
34 * Unit-tests for the GlobalVarReferenceMap class.
35 *
36 * @author [email protected] (Bashir Sadjad)
37 */
38 public class GlobalVarReferenceMapTest extends TestCase {
39
40 private final CompilerInput INPUT1 =
41 new CompilerInput(SourceFile.fromCode("input1", ""), false);
42 private final CompilerInput INPUT2 =
43 new CompilerInput(SourceFile.fromCode("input2", ""), false);
44 private final CompilerInput INPUT3 =
45 new CompilerInput(SourceFile.fromCode("input3", ""), false);
46 private final CompilerInput EXTERN1 =
47 new CompilerInput(SourceFile.fromCode("extern1", ""), true);
48
49 private final GlobalVarReferenceMap map = new GlobalVarReferenceMap(
50 Lists.newArrayList(INPUT1, INPUT2, INPUT3), Lists.newArrayList(EXTERN1));
51 private final Map<Var, ReferenceCollection> globalMap = Maps.newHashMap();
52 private final Node root = new Node(Token.BLOCK);
53 private final Scope globalScope = Scope.createGlobalScope(root);
54 private Node scriptRoot = new Node(Token.SCRIPT);
55
56 // In the initial setUp we have 3 references to var1 (one in each input) and
57 // 2 references to var2 (in first and third inputs), and 2 references to var3
58 // (in second input and first extern)
59 private static final String VAR1 = "var1";
60 private static final String VAR2 = "var2";
61 private static final String VAR3 = "var3";
62 private final ReferenceCollection var1Refs = new ReferenceCollection();
63 private final ReferenceCollection var2Refs = new ReferenceCollection();
64 private final ReferenceCollection var3Refs = new ReferenceCollection();
65 private final Reference var1In1Ref = createRefForTest(INPUT1);
66 private final Reference var1In2Ref = createRefForTest(INPUT2);
67 private final Reference var1In3Ref = createRefForTest(INPUT3);
68 private final Reference var2In1Ref = createRefForTest(INPUT1);
69 private final Reference var2In3Ref = createRefForTest(INPUT3);
70 private final Reference var3In2Ref = createRefForTest(INPUT2);
71 private final Reference var3In1Ext = createRefForTest(EXTERN1);
72
73 @Override
74 protected void setUp() throws Exception {
75 super.setUp();
76 globalScope.declare(VAR1, new Node(Token.NAME), null, INPUT1);
77 var1Refs.references = Lists.newArrayList(var1In1Ref,
78 var1In2Ref, var1In3Ref);
79 globalScope.declare(VAR2, new Node(Token.NAME), null, INPUT1);
80 var2Refs.references = Lists.newArrayList(var2In1Ref, var2In3Ref);
81 globalScope.declare(VAR3, new Node(Token.NAME), null, EXTERN1);
82 var3Refs.references = Lists.newArrayList(var3In1Ext, var3In2Ref);
83
84 // We recreate these two ReferenceCollection to keep var1Refs and
85 // var2Refs intact in update operations for comparison in the tests.
86 ReferenceCollection var1TempRefs = new ReferenceCollection();
87 var1TempRefs.references = Lists.newArrayList(var1Refs.references);
88 ReferenceCollection var2TempRefs = new ReferenceCollection();
89 var2TempRefs.references = Lists.newArrayList(var2Refs.references);
90 ReferenceCollection var3TempRefs = new ReferenceCollection();
91 var3TempRefs.references = Lists.newArrayList(var3Refs.references);
92 globalMap.put(globalScope.getVar(VAR1), var1TempRefs);
93 globalMap.put(globalScope.getVar(VAR2), var2TempRefs);
94 globalMap.put(globalScope.getVar(VAR3), var3TempRefs);
95 map.updateGlobalVarReferences(globalMap, root);
96 scriptRoot.setInputId(INPUT2.getInputId());
97 scriptRoot.setSourceFileForTesting(INPUT2.getName());
98 }
99
100 /** Tests whether the global variable references are set/reset properly. */
101 public void testUpdateGlobalVarReferences_ResetReferences() {
102 // First we check the original setup then reset again.
103 for (int i = 0; i < 2; i++) {
104 assertEquals(var1Refs.references,
105 map.getReferences(globalScope.getVar(VAR1)).references);
106 assertEquals(var2Refs.references,
107 map.getReferences(globalScope.getVar(VAR2)).references);
108 assertEquals(var3Refs.references,
109 map.getReferences(globalScope.getVar(VAR3)).references);
110 map.updateGlobalVarReferences(globalMap, root);
111 }
112 }
113
114 /** Removes all variable references in second script. */
115 public void testUpdateGlobalVarReferences_UpdateScriptNoRef() {
116 Map<Var, ReferenceCollection> scriptMap = Maps.newHashMap();
117 map.updateGlobalVarReferences(scriptMap, scriptRoot);
118 ReferenceCollection refs = map.getReferences(globalScope.getVar(VAR2));
119 assertEquals(var2Refs.references, refs.references);
120 refs = map.getReferences(globalScope.getVar(VAR1));
121 assertEquals(2, refs.references.size());
122 assertEquals(var1Refs.references.get(0), refs.references.get(0));
123 assertEquals(var1Refs.references.get(2), refs.references.get(1));
124 refs = map.getReferences(globalScope.getVar(VAR3));
125 assertEquals(1, refs.references.size());
126 assertEquals(var3Refs.references.get(0), refs.references.get(0));
127 }
128
129 /** Changes variable references in second script. */
130 public void testUpdateGlobalVarReferences_UpdateScriptNewRefs() {
131 Map<Var, ReferenceCollection> scriptMap = Maps.newHashMap();
132
133 ReferenceCollection newVar1Refs = new ReferenceCollection();
134 Reference newVar1In2Ref = createRefForTest(INPUT2);
135 newVar1Refs.references = Lists.newArrayList(newVar1In2Ref);
136
137 ReferenceCollection newVar2Refs = new ReferenceCollection();
138 Reference newVar2In2Ref = createRefForTest(INPUT2);
139 newVar2Refs.references = Lists.newArrayList(newVar2In2Ref);
140
141 ReferenceCollection newVar3Refs = new ReferenceCollection();
142 Reference newVar3In2Ref = createRefForTest(INPUT2);
143 newVar3Refs.references = Lists.newArrayList(newVar3In2Ref);
144
145 scriptMap.put(globalScope.getVar(VAR1), newVar1Refs);
146 scriptMap.put(globalScope.getVar(VAR2), newVar2Refs);
147 scriptMap.put(globalScope.getVar(VAR3), newVar3Refs);
148 map.updateGlobalVarReferences(scriptMap, scriptRoot);
149 ReferenceCollection refs = map.getReferences(globalScope.getVar(VAR1));
150 assertEquals(3, refs.references.size());
151 assertEquals(var1Refs.references.get(0), refs.references.get(0));
152 assertEquals(newVar1In2Ref, refs.references.get(1));
153 assertEquals(var1Refs.references.get(2), refs.references.get(2));
154 refs = map.getReferences(globalScope.getVar(VAR2));
155 assertEquals(3, refs.references.size());
156 assertEquals(var2Refs.references.get(0), refs.references.get(0));
157 assertEquals(newVar2In2Ref, refs.references.get(1));
158 assertEquals(var2Refs.references.get(1), refs.references.get(2));
159 refs = map.getReferences(globalScope.getVar(VAR3));
160 assertEquals(2, refs.references.size());
161 assertEquals(var3Refs.references.get(0), refs.references.get(0));
162 assertEquals(newVar3In2Ref, refs.references.get(1));
163 }
164
165 /** Changes variable references in second script. */
166 public void testUpdateGlobalVarReferences_UpdateScriptNewVar() {
167 Map<Var, ReferenceCollection> scriptMap = Maps.newHashMap();
168 final String var4 = "var4";
169 globalScope.declare(var4, new Node(Token.NAME), null, INPUT2);
170 ReferenceCollection newVar3Refs = new ReferenceCollection();
171 Reference newVar3In2Ref = createRefForTest(INPUT2);
172 newVar3Refs.references = Lists.newArrayList(newVar3In2Ref);
173 scriptMap.put(globalScope.getVar(var4), newVar3Refs);
174 map.updateGlobalVarReferences(scriptMap, scriptRoot);
175 ReferenceCollection refs = map.getReferences(globalScope.getVar(var4));
176 assertEquals(1, refs.references.size());
177 assertEquals(newVar3In2Ref, refs.references.get(0));
178 }
179
180 public void testUpdateReferencesWithGlobalScope() {
181 Scope newGlobalScope = Scope.createGlobalScope(root);
182 map.updateReferencesWithGlobalScope(newGlobalScope);
183 ReferenceCollection references =
184 map.getReferences(globalScope.getVar(VAR1));
185 for (Reference ref : references) {
186 assertEquals(newGlobalScope, ref.getScope());
187 }
188 references = map.getReferences(globalScope.getVar(VAR2));
189 for (Reference ref : references) {
190 assertEquals(newGlobalScope, ref.getScope());
191 }
192 references = map.getReferences(globalScope.getVar(VAR3));
193 for (Reference ref : references) {
194 assertEquals(newGlobalScope, ref.getScope());
195 }
196 }
197 }