| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
package com.google.javascript.jscomp; |
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
public class ChainCallsTest extends CompilerTestCase { |
| 24 |
@Override |
| 25 |
protected CompilerPass getProcessor(final Compiler compiler) { |
| 26 |
return new ChainCalls(compiler); |
| 27 |
} |
| 28 |
|
| 29 |
@Override |
| 30 |
protected int getNumRepetitions() { |
| 31 |
return 1; |
| 32 |
} |
| 33 |
|
| 34 |
public void testUnchainedCalls() { |
| 35 |
test( |
| 36 |
"" |
| 37 |
+ "/** @constructor */ function Foo() {}\n" |
| 38 |
+ "Foo.prototype.bar = function() { return this; };\n" |
| 39 |
+ "var f = new Foo();\n" |
| 40 |
+ "f.bar();\n" |
| 41 |
+ "f.bar();\n", |
| 42 |
"" |
| 43 |
+ "/** @constructor */ function Foo() {}\n" |
| 44 |
+ "Foo.prototype.bar = function() { return this; };\n" |
| 45 |
+ "var f = new Foo();\n" |
| 46 |
+ "f.bar().bar();\n"); |
| 47 |
|
| 48 |
} |
| 49 |
|
| 50 |
public void testSecondCallReturnNotThis() { |
| 51 |
test( |
| 52 |
"" |
| 53 |
+ "/** @constructor */ function Foo() {}\n" |
| 54 |
+ "Foo.prototype.bar = function() { return this; };\n" |
| 55 |
+ "Foo.prototype.baz = function() {};\n" |
| 56 |
+ "var f = new Foo();\n" |
| 57 |
+ "f.bar();\n" |
| 58 |
+ "f.baz();\n", |
| 59 |
"" |
| 60 |
+ "/** @constructor */ function Foo() {}\n" |
| 61 |
+ "Foo.prototype.bar = function() { return this; };\n" |
| 62 |
+ "Foo.prototype.baz = function() {};\n" |
| 63 |
+ "var f = new Foo();\n" |
| 64 |
+ "f.bar().baz();\n"); |
| 65 |
} |
| 66 |
|
| 67 |
public void testDifferentInstance() { |
| 68 |
testSame( |
| 69 |
"" |
| 70 |
+ "/** @constructor */ function Foo() {}\n" |
| 71 |
+ "Foo.prototype.bar = function() { return this; };\n" |
| 72 |
+ "new Foo().bar();\n" |
| 73 |
+ "new Foo().bar();\n"); |
| 74 |
} |
| 75 |
|
| 76 |
public void testSubclass() { |
| 77 |
testSame( |
| 78 |
"" |
| 79 |
+ "/** @constructor */ function Foo() {}\n" |
| 80 |
+ "Foo.prototype.bar = function() { return this; };\n" |
| 81 |
+ "/** @constructor\n@extends {Foo} */ function Baz() {}\n" |
| 82 |
+ "Baz.prototype.bar = function() {};\n" |
| 83 |
+ "(/** @type {Foo} */ new Baz()).bar();\n" |
| 84 |
+ "(/** @type {Foo} */ new Baz()).bar();\n"); |
| 85 |
} |
| 86 |
|
| 87 |
public void testSimpleDefinitionFinder() { |
| 88 |
String defs = |
| 89 |
"/** @constructor */ function Foo() {}\n" + |
| 90 |
"Foo.prototype.a = function() { return this; };" + |
| 91 |
"/** @constructor */ function Bar() {}\n" + |
| 92 |
"Bar.prototype.a = function() {};"; |
| 93 |
testSame( |
| 94 |
defs + |
| 95 |
"var o = new Foo; o.a(); o.a();"); |
| 96 |
testSame( |
| 97 |
defs + |
| 98 |
"var o = new Bar; o.a(); o.a();"); |
| 99 |
} |
| 100 |
|
| 101 |
public void testSimpleDefinitionFinder2() { |
| 102 |
String defs = |
| 103 |
"/** @constructor */ function Foo() {}\n" + |
| 104 |
"Foo.prototype.a = function() { return this; };" + |
| 105 |
"/** @constructor */ function Bar() {}\n" + |
| 106 |
"Bar.prototype.a = function() { return this; };"; |
| 107 |
testSame( |
| 108 |
defs + |
| 109 |
"var o = new Foo; o.a().a();"); |
| 110 |
testSame( |
| 111 |
defs + |
| 112 |
"var o = new Bar; o.a().a();"); |
| 113 |
} |
| 114 |
} |