Debian Codegraph @

test/com/google/javascript/jscomp/TightenTypesTest.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 package com.google.javascript.jscomp;
17
18 import com.google.javascript.jscomp.ConcreteType.ConcreteFunctionType;
19 import com.google.javascript.jscomp.ConcreteType.ConcreteInstanceType;
20 import com.google.javascript.jscomp.CheckLevel;
21 import com.google.javascript.jscomp.TightenTypes.ConcreteSlot;
22
23 import com.google.javascript.rhino.testing.BaseJSTypeTestCase;
24
25 /**
26 * Unit test for the TightenTypes pass.
27 *
28 */
29 public class TightenTypesTest extends CompilerTestCase {
30 private TightenTypes tt;
31
32 public TightenTypesTest() {
33 parseTypeInfo = true;
34 enableTypeCheck(CheckLevel.WARNING);
35 enableNormalize(true);
36 }
37
38 @Override
39 public CompilerPass getProcessor(Compiler compiler) {
40 return (tt = new TightenTypes(compiler));
41 }
42
43 @Override
44 protected int getNumRepetitions() {
45 return 1;
46 }
47
48 @Override
49 protected CompilerOptions getOptions() {
50 return new CompilerOptions(); // no missing properties check
51 }
52
53 public void testTopLevelVariables() {
54 testSame("/** @constructor */ function Foo() {}\n"
55 + "var a = new Foo();\n"
56 + "var b = a;\n");
57
58 assertTrue(getType("Foo").isFunction());
59 assertTrue(getType("a").isInstance());
60 assertType("function (this:Foo): ()", getType("Foo"));
61 assertType("Foo", getType("a"));
62 assertType("Foo", getType("b"));
63
64 testSame("/** @constructor */ function Foo() {}\n"
65 + "/** @constructor */ function Bar() {}\n"
66 + "var a = new Foo();\n"
67 + "a = new Bar();\n"
68 + "var b = a;\n");
69
70 assertTrue(getType("a").isUnion());
71 assertType("(Bar,Foo)", getType("a"));
72 assertType("Bar", getType("b"));
73 }
74
75 public void testNamespacedVariables() {
76 testSame("var goog = goog || {}; goog.foo = {};\n"
77 + "/** @constructor */ goog.foo.Foo = function() {};\n"
78 + "goog.foo.Foo.prototype.blah = function() {};\n"
79 + "/** @constructor */ goog.foo.Bar = function() {};\n"
80 + "goog.foo.Bar.prototype.blah = function() {};\n"
81 + "function bar(a) { a.blah(); }\n"
82 + "var baz = bar;\n"
83 + "bar(new goog.foo.Foo);\n"
84 + "baz(new goog.foo.Bar);\n");
85
86 assertType("(goog.foo.Bar,goog.foo.Foo)", getParamType(getType("bar"), 0));
87 assertType("(goog.foo.Bar,goog.foo.Foo)", getParamType(getType("baz"), 0));
88 }
89
90 public void testReturnSlot() {
91 testSame("/** @constructor */ function Foo() {}\n"
92 + "function bar() {\n"
93 + " var a = new Foo();\n"
94 + " return a;\n"
95 + "}\n"
96 + "var b = bar();\n");
97
98 assertType("Foo", getType("b"));
99 }
100
101 public void testParameterSlots() {
102 testSame("/** @constructor */ function Foo() {}\n"
103 + "/** @constructor */ function Bar() {}\n"
104 + "function bar(a, b) {}\n"
105 + "bar(new Foo, new Foo);\n"
106 + "bar(new Bar, null);\n");
107
108 assertType("(Bar,Foo)", getParamType(getType("bar"), 0));
109 assertType("Foo", getParamType(getType("bar"), 1));
110 assertNull(getParamVar(getType("bar"), 2));
111 }
112
113 public void testAliasedFunction() {
114 testSame("/** @constructor */ function Foo() {}\n"
115 + "/** @constructor */ function Bar() {}\n"
116 + "function bar(a) {}\n"
117 + "var baz = bar;\n"
118 + "bar(new Foo);\n"
119 + "baz(new Bar);\n");
120
121 assertType("(Bar,Foo)", getParamType(getType("bar"), 0));
122 assertType("(Bar,Foo)", getParamType(getType("baz"), 0));
123 }
124
125 public void testCatchStatement() {
126 testSame(BaseJSTypeTestCase.ALL_NATIVE_EXTERN_TYPES,
127 "/** @constructor */ function Bar() {}\n"
128 + "function bar() { try { } catch (e) { return e; } }\n"
129 + "/** @constructor\n@extends{Error}*/ function ID10TError() {}\n"
130 + "var a = bar(); throw new ID10TError();\n", null, null);
131
132 assertType("(Error,EvalError,ID10TError,RangeError,ReferenceError,"
133 + "SyntaxError,TypeError,URIError)", getType("a"));
134 }
135
136 public void testConstructorParameterSlots() {
137 testSame("/** @constructor */ function Foo() {}\n"
138 + "/** @constructor */ function Bar() {}\n"
139 + "/** @constructor */ function Baz(a) {}\n"
140 + "new Baz(new Foo);\n"
141 + "new Baz(new Bar);\n");
142
143 assertType("(Bar,Foo)", getParamType(getType("Baz"), 0));
144 }
145
146 public void testCallSlot() {
147 testSame("function foo() {}\n"
148 + "function bar() {}\n"
149 + "function baz() {}\n"
150 + "var a = foo;\n"
151 + "a = bar;\n"
152 + "a();\n");
153
154 assertTrue(isCalled(getType("foo")));
155 assertTrue(isCalled(getType("bar")));
156 assertFalse(isCalled(getType("baz")));
157 }
158
159 public void testObjectLiteralTraversal() {
160 testSame("var foo = function() {}\n"
161 + "function bar() { return { 'a': foo()} };\n"
162 + "bar();");
163 assertTrue(isCalled(getType("foo")));
164 }
165
166 public void testThis() {
167 testSame("/** @constructor */ function Foo() {}\n"
168 + "Foo.prototype.foo = function() { return this; }\n"
169 + "var a = new Foo();\n"
170 + "var b = a.foo();\n");
171
172 assertType("Foo", getType("a"));
173 assertType("Foo", getType("b"));
174 }
175
176 public void testAssign() {
177 testSame("/** @constructor */ function Foo() {}\n"
178 + "/** @constructor */ function Bar() {}\n"
179 + "var a = new Foo();\n"
180 + "var b = a = new Bar();\n");
181
182 assertType("(Bar,Foo)", getType("a"));
183 assertType("Bar", getType("b"));
184 }
185
186 public void testComma() {
187 testSame("/** @constructor */ function Foo() {b=new Foo()}\n"
188 + "var b;"
189 + "/** @constructor */ function Bar() {}\n"
190 + "var a = (new Foo, new Bar);\n");
191
192 assertType("Bar", getType("a"));
193 assertType("Foo", getType("b"));
194 }
195
196 public void testAnd() {
197 testSame("/** @constructor */ function Foo() {}\n"
198 + "/** @constructor */ function Bar() {}\n"
199 + "var a = (new Foo && new Bar);\n");
200
201 assertType("Bar", getType("a"));
202 }
203
204 public void testOr() {
205 testSame("/** @constructor */ function Foo() {}\n"
206 + "/** @constructor */ function Bar() {}\n"
207 + "/** @type {Foo} */ var f = new Foo();\n"
208 + "/** @type {Bar} */ var b = new Bar();\n"
209 + "var a = (f || b);\n");
210
211 assertType("(Bar,Foo)", getType("a"));
212 }
213
214 public void testHook() {
215 testSame("/** @constructor */ function Foo() {}\n"
216 + "/** @constructor */ function Bar() {}\n"
217 + "var a = (1+1 == 2) ? new Foo : new Bar;\n");
218
219 assertType("(Bar,Foo)", getType("a"));
220 }
221
222 public void testFunctionLiteral() {
223 testSame("/** @constructor */ function Foo() {}\n"
224 + "var a = (function() { return new Foo; })();\n");
225
226 assertType("Foo", getType("a"));
227 }
228
229 public void testNameLookup() {
230 testSame("/** @constructor */ function Foo() {}\n"
231 + "var a = new Foo;\n"
232 + "var b = (function() { return a; })();\n");
233
234 assertType("Foo", getType("a"));
235 assertType("Foo", getType("b"));
236 }
237
238 public void testGetProp() {
239 testSame("/** @constructor */ function Foo() {\n"
240 + " this.foo = new A();\n"
241 + "}\n"
242 + "/** @constructor */ function Bar() {\n"
243 + " this.foo = new B();\n"
244 + "}\n"
245 + "/** @constructor */ function Baz() {}\n"
246 + "/** @constructor */ function A() {}\n"
247 + "/** @constructor */ function B() {}\n"
248 // add the casts to make the JSType a union with null
249 + "/** @type {Foo} */ var foo = new Foo();\n"
250 + "/** @type {Bar} */ var bar = new Bar();\n"
251 + "/** @type {Baz} */ var baz = new Baz();\n" // has no 'foo'
252 + "var a = foo || bar || baz\n"
253 + "var b = a.foo;\n");
254
255 assertType("(A,B)", getType("b"));
256 }
257
258 public void testGetPrototypeProperty() {
259 testSame("/** @constructor */ function Foo() {};\n"
260 + "/** @constructor */ function Bar() {};\n"
261 + "Bar.prototype.a = new Foo();\n"
262 + "var a = Bar.prototype.a;\n");
263
264 assertType("Foo", getType("a"));
265 }
266
267 public void testGetElem() {
268 testSame(
269 "/**\n"
270 + " * @constructor\n"
271 + " * @extends {Object}\n"
272 + " * @param {...*} var_args\n"
273 + " * @return {!Array}\n"
274 + " */\n"
275 + "function Array(var_args) {}\n",
276 "/** @constructor */ function Foo() {}\n"
277 + "/** @constructor */ function Bar() {}\n"
278 + "var a = [];\n"
279 + "a[0] = new Foo;\n"
280 + "a[1] = new Bar;\n"
281 + "var b = a[0];\n"
282 + "var c = [new Foo, new Bar];\n", null);
283
284 assertType("Array", getType("a"));
285 assertType("(Array,Bar,Foo)", getType("b"));
286 assertType("Array", getType("c"));
287
288 testSame("/** @constructor */ function Foo() {}\n"
289 + "/** @constructor */ function Bar() {}\n"
290 + "/** @constructor */ function Baz() {\n"
291 + " this.arr = [];\n"
292 + "}\n"
293 + "var b = new Baz;\n"
294 + "b.arr[0] = new Foo;\n"
295 + "b.arr[1] = new Bar;\n"
296 + "var c = b.arr;\n");
297
298 assertType("Array", getType("c"));
299 }
300
301 public void testGetElem3() {
302 testSame(BaseJSTypeTestCase.ALL_NATIVE_EXTERN_TYPES,
303 "/** @constructor */ function Foo() {}\n"
304 + "/** @constructor */ function Bar() {}\n"
305 + "/** @constructor */ function Baz() {\n"
306 + " this.arr = [];\n"
307 + "}\n"
308 + "function foo(anarr) {"
309 + "}\n"
310 + "var ar = [];\n"
311 + "foo(ar);\n", null);
312
313 assertType("Array", getType("ar"));
314 }
315
316 public void testScopeDiscovery() {
317 testSame("function spam() {}\n"
318 + "function foo() {}\n"
319 + "function bar() {\n"
320 + " return function() { foo(); };\n"
321 + "}"
322 + "function baz() {\n"
323 + " return function() { bar()(); };\n"
324 + "}"
325 + "baz()()();\n");
326
327 assertFalse(isCalled(getType("spam")));
328 assertTrue(isCalled(getType("foo")));
329 }
330
331 public void testSheqDiscovery() {
332 testSame("function spam() {}\n"
333 + "/** @constructor */\n"
334 + "function Foo() {}\n"
335 + "Foo.prototype.foo1 = function() { f1(); }\n"
336 + "Foo.prototype.foo2 = function() { f2(); }\n"
337 + "Foo.prototype.foo3 = function() { f3(); }\n"
338 + "function baz(a) {\n"
339 + " a === null || a instanceof Foo ?\n"
340 + " Foo.prototype.foo1.call(this) :\n"
341 + " Foo.prototype.foo2.call(this);\n"
342 + "}\n"
343 + "function f1() {}\n"
344 + "function f2() {}\n"
345 + "function f3() {}\n"
346 + "baz(3);\n");
347
348 assertFalse(isCalled(getType("spam")));
349 assertFalse(isCalled(getType("f3")));
350 assertTrue(isCalled(getType("f1")));
351 assertTrue(isCalled(getType("f2")));
352 }
353
354 public void testSubclass() {
355 testSame("/** @constructor */\n"
356 + "function Foo() {}\n"
357 + "Foo.prototype.foo = function() { return this.bar; };\n"
358 + "Foo.prototype.bar = function() { return new A(); };\n"
359 + "/**\n"
360 + " * @constructor\n"
361 + " * @extends Foo\n"
362 + " */\n"
363 + "function Bar() {}\n"
364 + "/** @override */\n"
365 + "Bar.prototype.bar = function() { return new B(); };\n"
366 + "/** @constructor */ function A() {}\n"
367 + "/** @constructor */ function B() {}\n"
368 + "var a = (new Foo()).foo()();\n"
369 + "a = (new Bar()).foo()();\n");
370
371 ConcreteType fooType =
372 getPropertyType(getFunctionPrototype(getType("Foo")), "foo");
373 assertType("(Bar,Foo)", getThisType(fooType));
374 assertType("(A,B)", getType("a"));
375
376 testSame("/** @constructor */\n"
377 + "function Foo() {}\n"
378 + "Foo.prototype.foo = function() { return this.bar; };\n"
379 + "Foo.prototype.bar = function() { return new A(); };\n"
380 + "/**\n"
381 + " * @constructor\n"
382 + " * @extends Foo\n"
383 + " */\n"
384 + "function Bar() {}\n"
385 + "/** @override */\n"
386 + "Bar.prototype.bar = function() { return new B(); };\n"
387 + "/** @constructor */ function A() {}\n"
388 + "/** @constructor */ function B() {}\n"
389 + "var a = (new Bar()).foo()();\n");
390
391 fooType = getPropertyType(getFunctionPrototype(getType("Foo")), "foo");
392 assertType("Bar", getThisType(fooType));
393 assertType("B", getType("a"));
394 }
395
396 public void testArrayAssignments() {
397 testSame("/** @constructor */ function Foo() {}\n"
398 + "var a = [];\n"
399 + "function foo() { return []; }\n"
400 + "(a.length == 0 ? a : foo())[0] = new Foo;\n"
401 + "var b = a[0];\n"
402 + "var c = foo()[0];\n");
403
404 assertType("(Array,Foo)", getType("b"));
405 assertType("(Array,Foo)", getType("c"));
406 }
407
408 public void testAllPropertyReference() {
409 testSame("/** @constructor */ function Foo() {}\n"
410 + "Foo.prototype.prop = function() { this.prop2(); }\n"
411 + "Foo.prototype.prop2 = function() { b = new Foo; }\n"
412 + "var a = new Foo;\n"
413 + "a = [][0];\n"
414 + "function fun(a) {\n"
415 + " return a.prop();\n"
416 + "}\n"
417 + "var b;\n"
418 + "fun(a);\n"
419 );
420
421 assertType("Foo", getType("a"));
422 assertType("Foo", getType("b"));
423 }
424
425 public void testCallFunction() {
426 testSame("/** @constructor */ function Foo() { this.a = new A; }\n"
427 + "/** @constructor \n @extends Foo */ function Bar() {\n"
428 + " Foo.call(this);\n"
429 + "}\n"
430 + "/** @constructor */ function A() {};\n"
431 + "new Bar;");
432
433 assertTrue(isCalled(getType("Foo")));
434 assertTrue(isCalled(getType("A")));
435 ConcreteType fooType = getThisType(getType("Foo"));
436 assertType("A", getPropertyType(fooType, "a"));
437
438 ConcreteType barType = getThisType(getType("Bar"));
439 assertType("A", getPropertyType(barType, "a"));
440 }
441
442 public void testCallFunctionWithArgs() {
443 testSame("/** @constructor */ function Foo(o) { this.a = o; }\n"
444 + "/** @constructor \n @extends Foo */ function Bar() {\n"
445 + " Foo.call(this, new A());\n"
446 + "}\n"
447 + "/** @constructor */ function A() {};\n"
448 + "var b = new Bar;");
449
450 assertTrue(isCalled(getType("Foo")));
451 assertTrue(isCalled(getType("A")));
452
453 ConcreteType barType = getThisType(getType("Bar"));
454 assertType("A", getPropertyType(barType, "a"));
455
456 ConcreteType fooType = getThisType(getType("Foo"));
457 assertType("A", getPropertyType(fooType, "a"));
458 }
459
460 public void testCallPrototypeFunction() {
461 testSame("/** @constructor */ function Foo() {}\n"
462 + "Foo.prototype.a = function() { return new A; }\n"
463 + "Foo.prototype.a = function() { return new A; };\n"
464 + "/** @constructor \n @extends Foo */ function Bar() {}\n"
465 + "/** @override */"
466 + "Bar.prototype.a = function() { return new B; };\n"
467 + "/** @constructor */ function A() {};\n"
468 + "/** @constructor */ function B() {};\n"
469 + "var ret = Foo.prototype.a.call(new Bar);");
470
471 assertType("A", getType("ret"));
472 }
473
474 public void testCallPrototypeFunctionWithArgs() {
475 testSame("/** @constructor */ function Foo() { this.p = null }\n"
476 + "Foo.prototype.set = function(arg) { this.p = arg; };\n"
477 + "Foo.prototype.get = function() { return this.p; };\n"
478 + "/** @constructor */ function A() {};\n"
479 + "Foo.prototype.set.call(new Foo, new A);\n"
480 + "var ret = Foo.prototype.get.call(new Foo);");
481
482 ConcreteType fooP = getFunctionPrototype(getType("Foo"));
483 ConcreteFunctionType gFun = getPropertyType(fooP, "get").toFunction();
484 ConcreteFunctionType sFun = getPropertyType(fooP, "set").toFunction();
485
486 assertTrue(isCalled(sFun));
487 assertTrue(isCalled(gFun));
488 assertTrue(isCalled(getType("A")));
489 assertType("A", getType("ret"));
490 }
491
492 public void testSetTimeout() {
493 testSame("/** @constructor */ function Window() {};\n"
494 + "Window.prototype.setTimeout = function(f, t) {};\n"
495 + "/** @type Window */ var window;",
496 "/** @constructor*/ function A() {}\n"
497 + "A.prototype.handle = function() { foo(); };\n"
498 + "function foo() {}\n"
499 + "window.setTimeout((new A).handle, 3);", null);
500
501 assertTrue(isCalled(getType("foo")));
502 }
503
504 public void testExternType() {
505 testSame("/** @constructor */ function T() {};\n"
506 + "/** @constructor */ function Ext() {};\n"
507 + "/** @return {T} */\n"
508 + "Ext.prototype.getT = function() {};\n"
509 + "/** @type T */ Ext.prototype.prop;\n"
510 + "/** @type Ext */ var ext;",
511 "var b = ext.getT();\n"
512 + "var p = ext.prop;", null);
513
514 assertType("Ext", getType("ext"));
515 assertType("T", getType("b"));
516 assertType("T", getType("p"));
517 }
518
519 public void testExternSubTypes() {
520 testSame("/** @constructor */ function A() {};\n"
521 + "/** @constructor \n@extends A */ function B() {};\n"
522 + "/** @constructor \n@extends A */ function C() {};\n"
523 + "/** @constructor \n@extends B */ function D() {};\n"
524 + "/** @constructor */ function Ext() {};\n"
525 + "/** @type A */ Ext.prototype.a;\n"
526 + "/** @type B */ Ext.prototype.b;\n"
527 + "/** @type D */ Ext.prototype.d;\n"
528 + "/** @return {A} */ Ext.prototype.getA = function() {};\n"
529 + "/** @return {B} */ Ext.prototype.getB = function() {};\n",
530 "var a = (new Ext).a;\n"
531 + "var a2 = (new Ext).getA();\n"
532 + "var b = (new Ext).b;\n"
533 + "var b2 = (new Ext).getB();\n"
534 + "var d = (new Ext).d;\n", null);
535
536 assertType("(A,B,C,D)", getType("a"));
537 assertType("(A,B,C,D)", getType("a2"));
538 assertType("(B,D)", getType("b"));
539 assertType("(B,D)", getType("b2"));
540 assertType("D", getType("d"));
541 }
542
543 public void testExternSubTypesForObject() {
544 testSame(BaseJSTypeTestCase.ALL_NATIVE_EXTERN_TYPES
545 + "/** @constructor */ function A() {};\n"
546 + "/** @constructor \n@extends A */ function B() {};\n"
547 + "/** @return {Object} */ "
548 + "Object.prototype.eval = function(code) {};\n"
549 + "/** @type {Object} */\n"
550 + "A.prototype.a;\n"
551 + "/** @return {Object} */\n"
552 + "A.prototype.b = function(){};\n",
553 "var a = (new A).b()", null, null);
554 assertType("(A,ActiveXObject,Array,B,Boolean,Date,Error,EvalError,"
555 + "Function,Number,Object,"
556 + "RangeError,ReferenceError,RegExp,String,SyntaxError,"
557 + "TypeError,URIError)", getType("a"));
558 }
559
560 public void testImplicitPropCall() {
561 testSame("/** @constructor */ function Window() {};\n"
562 + "/** @param {function()} f \n@param {number} d */\n"
563 + "Window.prototype.setTimeout = function(f, d) {};",
564 "function foo() {};\n"
565 + "(new Window).setTimeout(foo, 20);", null);
566
567 assertTrue(isCalled(getType("foo")));
568 }
569
570 public void testImplicitPropCallWithArgs() {
571 testSame("/** @constructor */ function Window() {};\n"
572 + "/** @constructor */ function EventListener() {};\n"
573 + "/** @param {string} t\n"
574 + " * @param {EventListener|function(Event)} f */\n"
575 + "Window.prototype.addEventListener = function(t, f) {};\n"
576 + "/** @constructor */ function Event() {};",
577 "function foo(evt) {};\n"
578 + "(new Window).addEventListener('click', foo);", null);
579
580 assertTrue(isCalled(getType("foo")));
581 assertType("Event", getParamType(getType("foo"), 0));
582 }
583
584 public void testUntypedImplicitCallFromProperty() {
585 testSame("/** @constructor */ function Element() {};\n"
586 + "/** @type {?function(Event)} */Element.prototype.onclick;\n"
587 + "/** @constructor */ function Event() {};"
588 + "/** @return {Event} */ Event.prototype.erv;",
589 " function foo(evt) { return bar(evt); };\n"
590 + "function bar(a) { return a.type() }\n"
591 + "/** @type Object */ var ar = new Element;\n"
592 + "ar.onclick = foo;", null);
593
594 assertTrue(isCalled(getType("foo")));
595 assertTrue(isCalled(getType("bar")));
596 assertType("Event", getParamType(getType("foo"), 0));
597 assertType("Event", getParamType(getType("bar"), 0));
598 assertType("Element", getThisType(getType("foo").toFunction()));
599 }
600
601 public void testImplicitCallFromProperty() {
602 testSame("/** @constructor */ function Element() {};\n"
603 + "/** @type {function(this:Element,Event)} */\n"
604 + "Element.prototype.onclick;\n"
605 + "/** @constructor */ function Event() {};",
606 "function foo(evt) {};\n"
607 + "(new Element).onclick = foo;", null);
608
609 assertTrue(isCalled(getType("foo")));
610 assertType("Event", getParamType(getType("foo"), 0));
611 assertType("Element", getThisType(getType("foo").toFunction()));
612 }
613
614 public void testImplicitCallFromPropertyOfUnion() {
615 testSame("/** @constructor */ function Element() {};\n"
616 + "/** @type {function(this:Element,Event)} */\n"
617 + "Element.prototype.onclick;\n"
618 + "/** @constructor */ function Event() {};",
619 "function foo(evt) {};\n"
620 + "(new Element).onclick = foo;", null);
621
622 assertTrue(isCalled(getType("foo")));
623 assertType("Event", getParamType(getType("foo"), 0));
624 assertType("Element", getThisType(getType("foo").toFunction()));
625 }
626
627 public void testImplicitCallFromPropertyOfAllType() {
628 testSame("/** @constructor */ function Element() {};\n"
629 + "/** @type {function(this:Element,Event)} */\n"
630 + "Element.prototype.onclick;\n"
631 + "/** @constructor */ function Event() {};",
632 "function foo(evt) {};\n"
633 + "var elems = [];\n"
634 + "var elem = elems[0];\n" // assign it the all type
635 + "elem.onclick = foo;", null);
636
637 assertTrue(isCalled(getType("foo")));
638 assertType("Event", getParamType(getType("foo"), 0));
639 assertType("Element", getThisType(getType("foo").toFunction()));
640 }
641
642 public void testRestrictToCast() {
643 testSame("/** @constructor */ function Foo() {};\n"
644 + "var a = [];\n"
645 + "var foo = (/** @type {Foo} */ a[0]);\n"
646 + "var u = a[0];\n"
647 + "new Foo");
648
649 assertType("Foo", getType("foo"));
650 assertType("(Array,Foo)", getType("u"));
651 }
652
653 public void testRestrictToInterfaceCast() {
654 testSame("/** @constructor \n @implements Int */ function Foo() {};\n"
655 + "/** @interface */ function Int() {};\n"
656 + "var a = [];\n"
657 + "var foo = (/** @type {Int} */ a[0]);\n"
658 + "new Foo");
659
660 assertType("Foo", getType("foo"));
661 }
662
663 public void testRestrictToCastWithNonInstantiatedTypes() {
664 testSame(
665 "/** @constructor */ function Super() {}\n"
666 + "/** @constructor \n @extends {Super} */ function Foo() {};\n"
667 + "Foo.prototype.blah = function() { foofunc() };\n"
668 + "/** @constructor \n @extends {Super} */ function Bar() {};\n"
669 + "Bar.prototype.blah = function() { barfunc() };\n"
670 + "function barfunc() {}\n"
671 + "function foofunc() {}\n"
672 + "var a = [];\n"
673 + "var u = /** @type {Super} */ (a[0]);\n"
674 + "u.blah()\n"
675 + "new Foo");
676
677 assertTrue(isCalled(getType("foofunc")));
678 assertFalse(isCalled(getType("barfunc")));
679 assertType("Array", getType("a"));
680 }
681
682 public void testFunctionToString() {
683 testSame("/** @constructor */ function Foo() {}\n"
684 + "/** @constructor \n * @extends Foo */\n"
685 + "function Bar() { Foo.call(this); }\n"
686 + "var a = function(a) { return new Foo; };\n;"
687 + "a(new Foo);\n"
688 + "a(new Bar);\n"
689 + "new Bar;");
690
691 assertType("function ((Bar,Foo)): Foo", getType("a"));
692 assertType("function (this:(Bar,Foo)): ()", getType("Foo"));
693 assertType("function (this:Bar): ()", getType("Bar"));
694 }
695
696 private void assertType(String expected, ConcreteType type) {
697 assertEquals(expected, type.toString());
698 }
699
700 /** Returns the type of the given variable in the top-most scope. */
701 private ConcreteType getType(String var) {
702 assertNotNull(tt.getTopScope().getSlot(var));
703 return tt.getTopScope().getSlot(var).getType();
704 }
705
706 /** Returns the variable for the given parameter of the given function. */
707 private ConcreteSlot getParamVar(ConcreteType funType, int param) {
708 assertTrue(funType.isFunction());
709 return (ConcreteSlot)
710 ((ConcreteFunctionType) funType).getParameterSlot(param);
711 }
712
713 /** Returns the type of the given parameter of the given function. */
714 private ConcreteType getParamType(ConcreteType funType, int param) {
715 ConcreteSlot paramVar = getParamVar(funType, param);
716 return (paramVar != null) ? paramVar.getType() : ConcreteType.NONE;
717 }
718
719 /** Returns the variable for the this variable of the given function. */
720 private ConcreteSlot getThisSlot(ConcreteType funType) {
721 assertTrue(funType.isFunction());
722 return (ConcreteSlot) ((ConcreteFunctionType) funType).getThisSlot();
723 }
724
725 /** Returns the type of the this variable of the given function. */
726 private ConcreteType getThisType(ConcreteType funType) {
727 return getThisSlot(funType).getType();
728 }
729
730 /** Returns the prototype type of the given function. */
731 private ConcreteType getFunctionPrototype(ConcreteType funType) {
732 assertTrue(funType.isFunction());
733 return ((ConcreteFunctionType) funType).getPrototypeType();
734 }
735
736 /**
737 * Returns the variable for the property with the give name on the given
738 * instance type.
739 */
740 private ConcreteSlot getPropertyVar(ConcreteType instType, String name) {
741 assertTrue(instType.isInstance());
742 return (ConcreteSlot)
743 ((ConcreteInstanceType) instType).getPropertySlot(name);
744 }
745
746 /** Returns the type of the property with the give name on the given type. */
747 private ConcreteType getPropertyType(ConcreteType instType, String name) {
748 return getPropertyVar(instType, name).getType();
749 }
750
751 /** Returns whether the given function is called. */
752 private boolean isCalled(ConcreteType funType) {
753 assertTrue(funType.isFunction());
754 ConcreteSlot callVar = (ConcreteSlot)
755 ((ConcreteFunctionType) funType).getCallSlot();
756 return !callVar.getType().isNone();
757 }
758 }