Debian Codegraph @

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

raw symbols SCIP
1 /*
2 * Copyright 2007 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.javascript.jscomp.type.ClosureReverseAbstractInterpreter;
20 import com.google.javascript.jscomp.type.FlowScope;
21 import com.google.javascript.rhino.Node;
22 import com.google.javascript.rhino.Token;
23 import com.google.javascript.rhino.jstype.JSType;
24 import com.google.javascript.rhino.testing.Asserts;
25
26 public class ClosureReverseAbstractInterpreterTest extends
27 CompilerTypeTestCase {
28
29 public void testGoogIsDef1() throws Exception {
30 testClosureFunction("goog.isDef",
31 createOptionalType(NUMBER_TYPE),
32 NUMBER_TYPE,
33 VOID_TYPE);
34 }
35
36 public void testGoogIsDef2() throws Exception {
37 testClosureFunction("goog.isDef",
38 createNullableType(NUMBER_TYPE),
39 createNullableType(NUMBER_TYPE),
40 NO_TYPE);
41 }
42
43 public void testGoogIsDef3() throws Exception {
44 testClosureFunction("goog.isDef",
45 ALL_TYPE,
46 createUnionType(OBJECT_NUMBER_STRING_BOOLEAN,NULL_TYPE),
47 VOID_TYPE);
48 }
49
50 public void testGoogIsDef4() throws Exception {
51 testClosureFunction("goog.isDef",
52 UNKNOWN_TYPE,
53 UNKNOWN_TYPE, // TODO(johnlenz): should be CHECKED_UNKNOWN_TYPE
54 UNKNOWN_TYPE);
55 }
56
57 public void testGoogIsNull1() throws Exception {
58 testClosureFunction("goog.isNull",
59 createOptionalType(NUMBER_TYPE),
60 NO_TYPE,
61 createOptionalType(NUMBER_TYPE));
62 }
63
64 public void testGoogIsNull2() throws Exception {
65 testClosureFunction("goog.isNull",
66 createNullableType(NUMBER_TYPE),
67 NULL_TYPE,
68 NUMBER_TYPE);
69 }
70
71 public void testGoogIsNull3() throws Exception {
72 testClosureFunction("goog.isNull",
73 ALL_TYPE,
74 NULL_TYPE,
75 createUnionType(OBJECT_NUMBER_STRING_BOOLEAN, VOID_TYPE));
76 }
77
78 public void testGoogIsNull4() throws Exception {
79 testClosureFunction("goog.isNull",
80 UNKNOWN_TYPE,
81 UNKNOWN_TYPE,
82 UNKNOWN_TYPE); // TODO(johnlenz): this should be CHECK_UNKNOWN
83 }
84
85 public void testGoogIsDefAndNotNull1() throws Exception {
86 testClosureFunction("goog.isDefAndNotNull",
87 createOptionalType(NUMBER_TYPE),
88 NUMBER_TYPE,
89 VOID_TYPE);
90 }
91
92 public void testGoogIsDefAndNotNull2() throws Exception {
93 testClosureFunction("goog.isDefAndNotNull",
94 createNullableType(NUMBER_TYPE),
95 NUMBER_TYPE,
96 NULL_TYPE);
97 }
98
99 public void testGoogIsDefAndNotNull3() throws Exception {
100 testClosureFunction("goog.isDefAndNotNull",
101 createOptionalType(createNullableType(NUMBER_TYPE)),
102 NUMBER_TYPE,
103 NULL_VOID);
104 }
105
106 public void testGoogIsDefAndNotNull4() throws Exception {
107 testClosureFunction("goog.isDefAndNotNull",
108 ALL_TYPE,
109 OBJECT_NUMBER_STRING_BOOLEAN,
110 NULL_VOID);
111 }
112
113 public void testGoogIsDefAndNotNull5() throws Exception {
114 testClosureFunction("goog.isDefAndNotNull",
115 UNKNOWN_TYPE,
116 UNKNOWN_TYPE, // TODO(johnlenz): this should be "CHECKED_UNKNOWN"
117 UNKNOWN_TYPE);
118 }
119
120 public void testGoogIsString1() throws Exception {
121 testClosureFunction("goog.isString",
122 createNullableType(STRING_TYPE),
123 STRING_TYPE,
124 NULL_TYPE);
125 }
126
127 public void testGoogIsString2() throws Exception {
128 testClosureFunction("goog.isString",
129 createNullableType(NUMBER_TYPE),
130 createNullableType(NUMBER_TYPE),
131 createNullableType(NUMBER_TYPE));
132 }
133
134 public void testGoogIsBoolean1() throws Exception {
135 testClosureFunction("goog.isBoolean",
136 createNullableType(BOOLEAN_TYPE),
137 BOOLEAN_TYPE,
138 NULL_TYPE);
139 }
140
141 public void testGoogIsBoolean2() throws Exception {
142 testClosureFunction("goog.isBoolean",
143 createUnionType(BOOLEAN_TYPE, STRING_TYPE, NO_OBJECT_TYPE),
144 BOOLEAN_TYPE,
145 createUnionType(STRING_TYPE, NO_OBJECT_TYPE));
146 }
147
148 public void testGoogIsBoolean3() throws Exception {
149 testClosureFunction("goog.isBoolean",
150 ALL_TYPE,
151 BOOLEAN_TYPE,
152 ALL_TYPE); // TODO(johnlenz): this should be:
153 // {Object|number|string|null|void}
154 }
155
156 public void testGoogIsBoolean4() throws Exception {
157 testClosureFunction("goog.isBoolean",
158 UNKNOWN_TYPE,
159 BOOLEAN_TYPE,
160 CHECKED_UNKNOWN_TYPE);
161 }
162
163 public void testGoogIsNumber() throws Exception {
164 testClosureFunction("goog.isNumber",
165 createNullableType(NUMBER_TYPE),
166 NUMBER_TYPE,
167 NULL_TYPE);
168 }
169
170 public void testGoogIsFunction() throws Exception {
171 testClosureFunction("goog.isFunction",
172 createNullableType(OBJECT_FUNCTION_TYPE),
173 OBJECT_FUNCTION_TYPE,
174 NULL_TYPE);
175 }
176
177 public void testGoogIsFunction2() throws Exception {
178 testClosureFunction("goog.isFunction",
179 OBJECT_NUMBER_STRING_BOOLEAN,
180 U2U_CONSTRUCTOR_TYPE,
181 OBJECT_NUMBER_STRING_BOOLEAN);
182 }
183
184 public void testGoogIsFunction3() throws Exception {
185 testClosureFunction("goog.isFunction",
186 createUnionType(U2U_CONSTRUCTOR_TYPE, NUMBER_STRING_BOOLEAN),
187 U2U_CONSTRUCTOR_TYPE,
188 NUMBER_STRING_BOOLEAN);
189 }
190
191 public void testGoogIsFunctionOnNull() throws Exception {
192 testClosureFunction("goog.isFunction",
193 null,
194 U2U_CONSTRUCTOR_TYPE,
195 null);
196 }
197
198 public void testGoogIsArray1() throws Exception {
199 testClosureFunction("goog.isArray",
200 OBJECT_TYPE,
201 ARRAY_TYPE,
202 OBJECT_TYPE);
203 }
204
205 public void testGoogIsArray2() throws Exception {
206 testClosureFunction("goog.isArray",
207 ALL_TYPE,
208 ALL_TYPE, // TODO(johnlenz): should be ARRAY_TYPE?
209 ALL_TYPE);
210 }
211
212 public void testGoogIsArray3() throws Exception {
213 testClosureFunction("goog.isArray",
214 UNKNOWN_TYPE,
215 CHECKED_UNKNOWN_TYPE,
216 CHECKED_UNKNOWN_TYPE);
217 }
218
219 public void testGoogIsArray4() throws Exception {
220 testClosureFunction("goog.isArray",
221 createUnionType(ARRAY_TYPE, NULL_TYPE),
222 ARRAY_TYPE,
223 NULL_TYPE);
224 }
225
226 public void testGoogIsArrayOnNull() throws Exception {
227 testClosureFunction("goog.isArray",
228 null,
229 ARRAY_TYPE,
230 null);
231 }
232
233 public void testGoogIsObjectOnNull() throws Exception {
234 testClosureFunction("goog.isObject",
235 null,
236 OBJECT_TYPE,
237 null);
238 }
239
240 public void testGoogIsObject1() throws Exception {
241 testClosureFunction("goog.isObject",
242 ALL_TYPE,
243 NO_OBJECT_TYPE,
244 createUnionType(NUMBER_STRING_BOOLEAN, NULL_TYPE, VOID_TYPE));
245 }
246
247 public void testGoogIsObject2() throws Exception {
248 testClosureFunction("goog.isObject",
249 createUnionType(OBJECT_TYPE, NUMBER_STRING_BOOLEAN),
250 OBJECT_TYPE,
251 NUMBER_STRING_BOOLEAN);
252 }
253
254 public void testGoogIsObject3() throws Exception {
255 testClosureFunction("goog.isObject",
256 createUnionType(
257 OBJECT_TYPE, NUMBER_STRING_BOOLEAN, NULL_TYPE, VOID_TYPE),
258 OBJECT_TYPE,
259 createUnionType(NUMBER_STRING_BOOLEAN, NULL_TYPE, VOID_TYPE));
260 }
261
262 public void testGoogIsObject4() throws Exception {
263 testClosureFunction("goog.isObject",
264 UNKNOWN_TYPE,
265 NO_OBJECT_TYPE, // ? Should this be CHECKED_UNKNOWN?
266 CHECKED_UNKNOWN_TYPE);
267 }
268
269 private void testClosureFunction(String function, JSType type,
270 JSType trueType, JSType falseType) {
271 // function(a) where a : type
272 Node n = compiler.parseTestCode("var a; " + function + "(a)");
273 Node call = n.getLastChild().getLastChild();
274 Node name = call.getLastChild();
275
276 Scope scope = new SyntacticScopeCreator(compiler).createScope(n, null);
277 FlowScope flowScope = LinkedFlowScope.createEntryLattice(scope);
278
279 assertEquals(Token.CALL, call.getType());
280 assertEquals(Token.NAME, name.getType());
281
282 GoogleCodingConvention convention = new GoogleCodingConvention();
283 flowScope.inferSlotType("a", type);
284 ClosureReverseAbstractInterpreter rai =
285 new ClosureReverseAbstractInterpreter(convention, registry);
286
287 // trueScope
288 Asserts.assertTypeEquals(
289 trueType,
290 rai.getPreciserScopeKnowingConditionOutcome(call, flowScope, true)
291 .getSlot("a").getType());
292
293 // falseScope
294 Asserts.assertTypeEquals(
295 falseType,
296 rai.getPreciserScopeKnowingConditionOutcome(call, flowScope, false)
297 .getSlot("a").getType());
298 }
299 }