Debian Codegraph @

test/com/google/javascript/jscomp/ConcreteTypeTest.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 static com.google.javascript.jscomp.ConcreteType.ALL;
20 import static com.google.javascript.jscomp.ConcreteType.NONE;
21
22 import com.google.common.collect.Lists;
23 import com.google.common.collect.Maps;
24 import com.google.common.collect.Sets;
25 import com.google.javascript.jscomp.ConcreteType.ConcreteFunctionType;
26 import com.google.javascript.jscomp.ConcreteType.ConcreteInstanceType;
27 import com.google.javascript.jscomp.ConcreteType.ConcreteUnionType;
28 import com.google.javascript.jscomp.ConcreteType.Factory;
29 import com.google.javascript.rhino.JSDocInfo;
30 import com.google.javascript.rhino.Node;
31 import com.google.javascript.rhino.Token;
32 import com.google.javascript.rhino.jstype.FunctionType;
33 import com.google.javascript.rhino.jstype.JSType;
34 import com.google.javascript.rhino.jstype.JSTypeNative;
35 import com.google.javascript.rhino.jstype.JSTypeRegistry;
36 import com.google.javascript.rhino.jstype.ObjectType;
37 import com.google.javascript.rhino.jstype.StaticReference;
38 import com.google.javascript.rhino.jstype.StaticScope;
39 import com.google.javascript.rhino.jstype.StaticSlot;
40 import com.google.javascript.rhino.testing.AbstractStaticScope;
41 import com.google.javascript.rhino.testing.TestErrorReporter;
42
43 import junit.framework.TestCase;
44
45 import java.util.Arrays;
46 import java.util.Collection;
47 import java.util.List;
48 import java.util.Map;
49
50 /**
51 * Unit test for the the subclasses of ConcreteType.
52 *
53 */
54 public class ConcreteTypeTest extends TestCase {
55 private JSTypeRegistry typeRegistry;
56 private JSType unknownType;
57 private Factory factory;
58
59 @Override
60 public void setUp() {
61 typeRegistry = new JSTypeRegistry(new TestErrorReporter(null, null));
62 unknownType = typeRegistry.getNativeType(JSTypeNative.UNKNOWN_TYPE);
63 factory = new FakeFactory();
64 }
65
66 private void checkEquality(List<ConcreteType> types) {
67 for (int i = 0; i < types.size(); ++i) {
68 for (int j = 0; j < types.size(); ++j) {
69 if (i == j) {
70 assertEquals(types.get(i), types.get(j));
71 } else {
72 assertFalse(types.get(i).equals(types.get(j)));
73 }
74 }
75 }
76 }
77
78 public void testEquals() {
79 ConcreteFunctionType fun1 = createFunction("fun1");
80 ConcreteFunctionType fun2 = createFunction("fun2");
81 ConcreteType obj1 = fun1.getInstanceType();
82 ConcreteType obj2 = fun2.getInstanceType();
83 ConcreteType union1 = new ConcreteUnionType(fun1, fun2);
84 ConcreteType union2 = new ConcreteUnionType(fun1, obj1);
85 ConcreteType union3 = new ConcreteUnionType(fun1, obj1);
86
87 checkEquality(Lists.newArrayList(fun1, fun2, obj1, obj2,
88 union1, union2));
89
90 assertEquals(union2, union3);
91 }
92
93 public void testUnionWith() {
94 ConcreteFunctionType fun = createFunction("fun");
95 ConcreteType obj = fun.getInstanceType();
96 ConcreteType both = new ConcreteUnionType(fun, obj);
97
98 assertTrue(fun.isSingleton());
99 assertTrue(obj.isSingleton());
100 assertFalse(both.isSingleton());
101 assertFalse(NONE.isSingleton());
102 assertFalse(ALL.isSingleton());
103
104 checkUnionWith(fun, NONE, fun);
105 checkUnionWith(fun, ALL, ALL);
106
107 checkUnionWith(fun, obj, both);
108 checkUnionWith(both, NONE, both);
109 checkUnionWith(both, ALL, ALL);
110 }
111
112 private void checkUnionWith(ConcreteType a, ConcreteType b, ConcreteType c) {
113 assertEquals(a, a.unionWith(a));
114 assertEquals(b, b.unionWith(b));
115 assertEquals(c, a.unionWith(b));
116 assertEquals(c, b.unionWith(a));
117 }
118
119 public void testIntersectionWith() {
120 ConcreteFunctionType fun = createFunction("fun");
121 ConcreteFunctionType fun2 = createFunction("fun2");
122 ConcreteType obj = fun.getInstanceType();
123 ConcreteType both = new ConcreteUnionType(fun, obj);
124
125 assertEquals(NONE, fun.intersectWith(obj));
126 assertEquals(NONE, obj.intersectWith(fun));
127
128 assertEquals(fun, both.intersectWith(fun));
129 assertEquals(fun, fun.intersectWith(both));
130
131 assertEquals(NONE, NONE.intersectWith(both));
132 assertEquals(NONE, both.intersectWith(NONE));
133 assertEquals(NONE, fun.intersectWith(NONE));
134 assertEquals(NONE, NONE.intersectWith(fun));
135
136 assertEquals(NONE, both.intersectWith(fun2));
137
138 assertEquals(both, ALL.intersectWith(both));
139 assertEquals(both, both.intersectWith(ALL));
140 assertEquals(fun, ALL.intersectWith(fun));
141 assertEquals(fun, fun.intersectWith(ALL));
142 assertEquals(NONE, ALL.intersectWith(NONE));
143 assertEquals(NONE, NONE.intersectWith(ALL));
144 }
145
146 public void testFunction() {
147 ConcreteFunctionType fun = createFunction("fun", "a", "b");
148 assertTrue(fun.isFunction());
149 assertNotNull(fun.getCallSlot());
150 assertNotNull(fun.getReturnSlot());
151 assertNotNull(fun.getParameterSlot(0));
152 assertNotNull(fun.getParameterSlot(1));
153 assertNull(fun.getParameterSlot(2));
154 assertTrue(fun.getInstanceType().isInstance());
155 }
156
157 public void testInstance() {
158 ConcreteInstanceType obj = createInstance("MyObj", "a", "b");
159 assertTrue(obj.isInstance());
160 assertNotNull(obj.getPropertySlot("a"));
161 assertNotNull(obj.getPropertySlot("b"));
162 assertNull(obj.getPropertySlot("c"));
163
164 // The prototype chain should be: MyObj -> MyObj.prototype -> Object ->
165 // Object.prototype -> null.
166 for (int i = 0; i < 3; ++i) {
167 assertNotNull(obj = obj.getImplicitPrototype());
168 assertTrue(obj.isInstance());
169 }
170 assertNull(obj.getImplicitPrototype());
171 }
172
173 public void testGetX() {
174 ConcreteFunctionType fun1 = createFunction("fun1");
175 ConcreteFunctionType fun2 = createFunction("fun2");
176 ConcreteInstanceType obj1 = fun1.getInstanceType();
177 ConcreteInstanceType obj2 = fun2.getInstanceType();
178 ConcreteType union1 = fun1.unionWith(obj1);
179 ConcreteType union2 =
180 union1.unionWith(fun2).unionWith(obj2);
181
182 assertEqualSets(Lists.newArrayList(), NONE.getFunctions());
183 assertEqualSets(Lists.newArrayList(), NONE.getInstances());
184 assertEqualSets(Lists.newArrayList(fun1), fun1.getFunctions());
185 assertEqualSets(Lists.newArrayList(), fun1.getInstances());
186 assertEqualSets(Lists.newArrayList(), obj1.getFunctions());
187 assertEqualSets(Lists.newArrayList(obj1), obj1.getInstances());
188
189 assertEqualSets(Lists.newArrayList(fun1), union1.getFunctions());
190 assertEqualSets(Lists.newArrayList(obj1), union1.getInstances());
191
192 assertEqualSets(Lists.newArrayList(fun1, fun2), union2.getFunctions());
193 assertEqualSets(Lists.newArrayList(obj1, obj2), union2.getInstances());
194 }
195
196 /** Checks that the two collections are equal as sets. */
197 private void assertEqualSets(Collection<?> first, Collection<?> second) {
198 assertEquals(Sets.newHashSet(first), Sets.newHashSet(second));
199 }
200
201 /** Creates a fake function with the given description. */
202 private ConcreteFunctionType createFunction(
203 String name, String... paramNames) {
204 Node args = new Node(Token.PARAM_LIST);
205 for (int i = 0; i < paramNames.length; ++i) {
206 args.addChildToBack(Node.newString(Token.NAME, paramNames[i]));
207 }
208
209 Node decl = new Node(Token.FUNCTION,
210 Node.newString(Token.NAME, name),
211 args,
212 new Node(Token.BLOCK));
213
214 JSType[] paramTypes = new JSType[paramNames.length];
215 Arrays.fill(paramTypes, unknownType);
216 decl.setJSType(typeRegistry.createConstructorType(
217 name, decl, args, unknownType, null));
218
219 return new ConcreteFunctionType(factory, decl, null);
220 }
221
222 /** Creates a fake instance with the given description. */
223 private ConcreteInstanceType createInstance(
224 String name, String... propNames) {
225 ObjectType objType = typeRegistry.createObjectType(name, null,
226 typeRegistry.createObjectType(name + ".prototype", null, null));
227 for (int i = 0; i < propNames.length; ++i) {
228 objType.defineDeclaredProperty(propNames[i], unknownType, null);
229 }
230 return new ConcreteInstanceType(factory, objType);
231 }
232
233 private class FakeFactory implements Factory {
234 private final Map<Node, ConcreteFunctionType> functionByDeclaration =
235 Maps.newHashMap();
236 private final Map<FunctionType, ConcreteFunctionType> functionByJSType =
237 Maps.newHashMap();
238 private final Map<ObjectType, ConcreteInstanceType> instanceByJSType =
239 Maps.newHashMap();
240
241 private final JSTypeRegistry registry = new JSTypeRegistry(
242 new TestErrorReporter(null, null));
243
244 @Override
245 public JSTypeRegistry getTypeRegistry() {
246 return registry;
247 }
248
249 /** {@inheritDoc} */
250 @Override
251 public ConcreteFunctionType createConcreteFunction(
252 Node decl, StaticScope<ConcreteType> parent) {
253 ConcreteFunctionType funcType = functionByDeclaration.get(decl);
254 if (funcType == null) {
255 functionByDeclaration.put(decl, funcType =
256 new ConcreteFunctionType(this, decl, parent));
257 if (decl.getJSType() != null) {
258 functionByJSType.put((FunctionType) decl.getJSType(), funcType);
259 }
260 }
261 return funcType;
262 }
263
264 /** {@inheritDoc} */
265 @Override
266 public ConcreteInstanceType createConcreteInstance(
267 ObjectType instanceType) {
268 ConcreteInstanceType instType = instanceByJSType.get(instanceType);
269 if (instType == null) {
270 instanceByJSType.put(instanceType,
271 instType = new ConcreteInstanceType(this, instanceType));
272 }
273 return instType;
274 }
275
276 /** {@inheritDoc} */
277 @Override
278 public ConcreteFunctionType getConcreteFunction(FunctionType functionType) {
279 return functionByJSType.get(functionType);
280 }
281
282 /** {@inheritDoc} */
283 @Override
284 public ConcreteInstanceType getConcreteInstance(ObjectType instanceType) {
285 return instanceByJSType.get(instanceType);
286 }
287
288 /** {@inheritDoc} */
289 @Override
290 public StaticScope<ConcreteType> createFunctionScope(
291 Node decl, StaticScope<ConcreteType> parent) {
292 FakeScope scope = new FakeScope((FakeScope) parent);
293 scope.addSlot(ConcreteFunctionType.CALL_SLOT_NAME);
294 scope.addSlot(ConcreteFunctionType.THIS_SLOT_NAME);
295 scope.addSlot(ConcreteFunctionType.RETURN_SLOT_NAME);
296 for (Node n = decl.getFirstChild().getNext().getFirstChild();
297 n != null;
298 n = n.getNext()) {
299 scope.addSlot(n.getString());
300 }
301 return scope;
302 }
303
304 /** {@inheritDoc} */
305 @Override
306 public StaticScope<ConcreteType> createInstanceScope(
307 ObjectType instanceType) {
308 FakeScope parentScope = null;
309 if (instanceType.getImplicitPrototype() != null) {
310 ConcreteInstanceType prototype =
311 createConcreteInstance(instanceType.getImplicitPrototype());
312 parentScope = (FakeScope) prototype.getScope();
313 }
314
315 FakeScope scope = new FakeScope(parentScope);
316 for (String propName : instanceType.getOwnPropertyNames()) {
317 scope.addSlot(propName);
318 }
319 return scope;
320 }
321 }
322
323 // TODO(user): move to a common place if it can be used elsewhere
324 private class FakeScope extends AbstractStaticScope<ConcreteType> {
325 private final FakeScope parent;
326 private final Map<String, FakeSlot> slots = Maps.newHashMap();
327
328 FakeScope(FakeScope parent) {
329 this.parent = parent;
330 }
331
332 /** {@inheritDoc} */
333 @Override
334 public StaticScope<ConcreteType> getParentScope() { return parent; }
335
336 /** {@inheritDoc} */
337 @Override
338 public StaticSlot<ConcreteType> getOwnSlot(String name) {
339 return slots.get(name);
340 }
341
342 /** {@inheritDoc} */
343 @Override
344 public StaticSlot<ConcreteType> getSlot(String name) {
345 if (slots.containsKey(name)) {
346 return slots.get(name);
347 } else if (parent != null) {
348 return parent.getSlot(name);
349 } else {
350 return null;
351 }
352 }
353
354 /** {@inheritDoc} */
355 @Override
356 public ConcreteType getTypeOfThis() { return ConcreteType.ALL; }
357
358 void addSlot(String name) {
359 slots.put(name, new FakeSlot(name));
360 }
361 }
362
363 // TODO(user): move to a common place if it can be used elsewhere
364 private class FakeSlot implements StaticSlot<ConcreteType> {
365 private final String name;
366
367 FakeSlot(String name) {
368 this.name = name;
369 }
370
371 @Override
372 public String getName() { return name; }
373
374 @Override
375 public ConcreteType getType() { return ConcreteType.ALL; }
376
377 @Override
378 public boolean isTypeInferred() { return true; }
379
380 @Override
381 public StaticReference<ConcreteType> getDeclaration() { return null; }
382
383 @Override
384 public JSDocInfo getJSDocInfo() { return null; }
385 }
386 }