File: ClosureReverseAbstractInterpreterTest.java

package info (click to toggle)
closure-compiler 20130227%2Bdfsg1-10.1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 12,792 kB
  • sloc: java: 175,338; javascript: 20,728; xml: 371; makefile: 19; sh: 6
file content (299 lines) | stat: -rw-r--r-- 8,974 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
/*
 * Copyright 2007 The Closure Compiler Authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.google.javascript.jscomp;

import com.google.javascript.jscomp.type.ClosureReverseAbstractInterpreter;
import com.google.javascript.jscomp.type.FlowScope;
import com.google.javascript.rhino.Node;
import com.google.javascript.rhino.Token;
import com.google.javascript.rhino.jstype.JSType;
import com.google.javascript.rhino.testing.Asserts;

public class ClosureReverseAbstractInterpreterTest extends
    CompilerTypeTestCase {

  public void testGoogIsDef1() throws Exception {
    testClosureFunction("goog.isDef",
        createOptionalType(NUMBER_TYPE),
        NUMBER_TYPE,
        VOID_TYPE);
  }

  public void testGoogIsDef2() throws Exception {
    testClosureFunction("goog.isDef",
        createNullableType(NUMBER_TYPE),
        createNullableType(NUMBER_TYPE),
        NO_TYPE);
  }

  public void testGoogIsDef3() throws Exception {
    testClosureFunction("goog.isDef",
        ALL_TYPE,
        createUnionType(OBJECT_NUMBER_STRING_BOOLEAN,NULL_TYPE),
        VOID_TYPE);
  }

  public void testGoogIsDef4() throws Exception {
    testClosureFunction("goog.isDef",
        UNKNOWN_TYPE,
        UNKNOWN_TYPE,  // TODO(johnlenz): should be CHECKED_UNKNOWN_TYPE
        UNKNOWN_TYPE);
  }

  public void testGoogIsNull1() throws Exception {
    testClosureFunction("goog.isNull",
        createOptionalType(NUMBER_TYPE),
        NO_TYPE,
        createOptionalType(NUMBER_TYPE));
  }

  public void testGoogIsNull2() throws Exception {
    testClosureFunction("goog.isNull",
        createNullableType(NUMBER_TYPE),
        NULL_TYPE,
        NUMBER_TYPE);
  }

  public void testGoogIsNull3() throws Exception {
    testClosureFunction("goog.isNull",
        ALL_TYPE,
        NULL_TYPE,
        createUnionType(OBJECT_NUMBER_STRING_BOOLEAN, VOID_TYPE));
  }

  public void testGoogIsNull4() throws Exception {
    testClosureFunction("goog.isNull",
        UNKNOWN_TYPE,
        UNKNOWN_TYPE,
        UNKNOWN_TYPE); // TODO(johnlenz): this should be CHECK_UNKNOWN
  }

  public void testGoogIsDefAndNotNull1() throws Exception {
    testClosureFunction("goog.isDefAndNotNull",
        createOptionalType(NUMBER_TYPE),
        NUMBER_TYPE,
        VOID_TYPE);
  }

  public void testGoogIsDefAndNotNull2() throws Exception {
    testClosureFunction("goog.isDefAndNotNull",
        createNullableType(NUMBER_TYPE),
        NUMBER_TYPE,
        NULL_TYPE);
  }

  public void testGoogIsDefAndNotNull3() throws Exception {
    testClosureFunction("goog.isDefAndNotNull",
        createOptionalType(createNullableType(NUMBER_TYPE)),
        NUMBER_TYPE,
        NULL_VOID);
  }

  public void testGoogIsDefAndNotNull4() throws Exception {
    testClosureFunction("goog.isDefAndNotNull",
        ALL_TYPE,
        OBJECT_NUMBER_STRING_BOOLEAN,
        NULL_VOID);
  }

  public void testGoogIsDefAndNotNull5() throws Exception {
    testClosureFunction("goog.isDefAndNotNull",
        UNKNOWN_TYPE,
        UNKNOWN_TYPE,  // TODO(johnlenz): this should be "CHECKED_UNKNOWN"
        UNKNOWN_TYPE);
  }

  public void testGoogIsString1() throws Exception {
    testClosureFunction("goog.isString",
        createNullableType(STRING_TYPE),
        STRING_TYPE,
        NULL_TYPE);
  }

  public void testGoogIsString2() throws Exception {
    testClosureFunction("goog.isString",
        createNullableType(NUMBER_TYPE),
        createNullableType(NUMBER_TYPE),
        createNullableType(NUMBER_TYPE));
  }

  public void testGoogIsBoolean1() throws Exception {
    testClosureFunction("goog.isBoolean",
        createNullableType(BOOLEAN_TYPE),
        BOOLEAN_TYPE,
        NULL_TYPE);
  }

  public void testGoogIsBoolean2() throws Exception {
    testClosureFunction("goog.isBoolean",
        createUnionType(BOOLEAN_TYPE, STRING_TYPE, NO_OBJECT_TYPE),
        BOOLEAN_TYPE,
        createUnionType(STRING_TYPE, NO_OBJECT_TYPE));
  }

  public void testGoogIsBoolean3() throws Exception {
    testClosureFunction("goog.isBoolean",
        ALL_TYPE,
        BOOLEAN_TYPE,
        ALL_TYPE); // TODO(johnlenz): this should be:
                   //   {Object|number|string|null|void}
  }

  public void testGoogIsBoolean4() throws Exception {
    testClosureFunction("goog.isBoolean",
        UNKNOWN_TYPE,
        BOOLEAN_TYPE,
        CHECKED_UNKNOWN_TYPE);
  }

  public void testGoogIsNumber() throws Exception {
    testClosureFunction("goog.isNumber",
        createNullableType(NUMBER_TYPE),
        NUMBER_TYPE,
        NULL_TYPE);
  }

  public void testGoogIsFunction() throws Exception {
    testClosureFunction("goog.isFunction",
        createNullableType(OBJECT_FUNCTION_TYPE),
        OBJECT_FUNCTION_TYPE,
        NULL_TYPE);
  }

  public void testGoogIsFunction2() throws Exception {
    testClosureFunction("goog.isFunction",
        OBJECT_NUMBER_STRING_BOOLEAN,
        U2U_CONSTRUCTOR_TYPE,
        OBJECT_NUMBER_STRING_BOOLEAN);
  }

  public void testGoogIsFunction3() throws Exception {
    testClosureFunction("goog.isFunction",
        createUnionType(U2U_CONSTRUCTOR_TYPE, NUMBER_STRING_BOOLEAN),
        U2U_CONSTRUCTOR_TYPE,
        NUMBER_STRING_BOOLEAN);
  }

  public void testGoogIsFunctionOnNull() throws Exception {
    testClosureFunction("goog.isFunction",
        null,
        U2U_CONSTRUCTOR_TYPE,
        null);
  }

  public void testGoogIsArray1() throws Exception {
    testClosureFunction("goog.isArray",
        OBJECT_TYPE,
        ARRAY_TYPE,
        OBJECT_TYPE);
  }

  public void testGoogIsArray2() throws Exception {
    testClosureFunction("goog.isArray",
        ALL_TYPE,
        ALL_TYPE, // TODO(johnlenz): should be ARRAY_TYPE?
        ALL_TYPE);
  }

  public void testGoogIsArray3() throws Exception {
    testClosureFunction("goog.isArray",
        UNKNOWN_TYPE,
        CHECKED_UNKNOWN_TYPE,
        CHECKED_UNKNOWN_TYPE);
  }

  public void testGoogIsArray4() throws Exception {
    testClosureFunction("goog.isArray",
        createUnionType(ARRAY_TYPE, NULL_TYPE),
        ARRAY_TYPE,
        NULL_TYPE);
  }

  public void testGoogIsArrayOnNull() throws Exception {
    testClosureFunction("goog.isArray",
        null,
        ARRAY_TYPE,
        null);
  }

  public void testGoogIsObjectOnNull() throws Exception {
    testClosureFunction("goog.isObject",
        null,
        OBJECT_TYPE,
        null);
  }

  public void testGoogIsObject1() throws Exception {
    testClosureFunction("goog.isObject",
        ALL_TYPE,
        NO_OBJECT_TYPE,
        createUnionType(NUMBER_STRING_BOOLEAN, NULL_TYPE, VOID_TYPE));
  }

  public void testGoogIsObject2() throws Exception {
    testClosureFunction("goog.isObject",
          createUnionType(OBJECT_TYPE, NUMBER_STRING_BOOLEAN),
          OBJECT_TYPE,
          NUMBER_STRING_BOOLEAN);
  }

  public void testGoogIsObject3() throws Exception {
    testClosureFunction("goog.isObject",
          createUnionType(
              OBJECT_TYPE, NUMBER_STRING_BOOLEAN, NULL_TYPE, VOID_TYPE),
          OBJECT_TYPE,
          createUnionType(NUMBER_STRING_BOOLEAN, NULL_TYPE, VOID_TYPE));
  }

  public void testGoogIsObject4() throws Exception {
    testClosureFunction("goog.isObject",
        UNKNOWN_TYPE,
        NO_OBJECT_TYPE,  // ? Should this be CHECKED_UNKNOWN?
        CHECKED_UNKNOWN_TYPE);
  }

  private void testClosureFunction(String function, JSType type,
      JSType trueType, JSType falseType) {
    // function(a) where a : type
    Node n = compiler.parseTestCode("var a; " + function + "(a)");
    Node call = n.getLastChild().getLastChild();
    Node name = call.getLastChild();

    Scope scope = new SyntacticScopeCreator(compiler).createScope(n, null);
    FlowScope flowScope = LinkedFlowScope.createEntryLattice(scope);

    assertEquals(Token.CALL, call.getType());
    assertEquals(Token.NAME, name.getType());

    GoogleCodingConvention convention = new GoogleCodingConvention();
    flowScope.inferSlotType("a", type);
    ClosureReverseAbstractInterpreter rai =
        new ClosureReverseAbstractInterpreter(convention, registry);

    // trueScope
    Asserts.assertTypeEquals(
        trueType,
        rai.getPreciserScopeKnowingConditionOutcome(call, flowScope, true)
        .getSlot("a").getType());

    // falseScope
    Asserts.assertTypeEquals(
        falseType,
        rai.getPreciserScopeKnowingConditionOutcome(call, flowScope, false)
        .getSlot("a").getType());
  }
}