| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package com.google.javascript.jscomp; |
| 18 |
|
| 19 |
import static com.google.javascript.rhino.testing.BaseJSTypeTestCase.ALL_NATIVE_EXTERN_TYPES; |
| 20 |
|
| 21 |
import com.google.javascript.jscomp.CheckLevel; |
| 22 |
import com.google.javascript.rhino.Node; |
| 23 |
import com.google.javascript.rhino.jstype.FunctionType; |
| 24 |
import com.google.javascript.rhino.jstype.ObjectType; |
| 25 |
|
| 26 |
import java.util.List; |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
public class FunctionTypeBuilderTest extends CompilerTestCase { |
| 33 |
|
| 34 |
public FunctionTypeBuilderTest() { |
| 35 |
parseTypeInfo = true; |
| 36 |
enableTypeCheck(CheckLevel.WARNING); |
| 37 |
} |
| 38 |
|
| 39 |
@Override |
| 40 |
public CompilerPass getProcessor(Compiler compiler) { |
| 41 |
|
| 42 |
return new CompilerPass() { |
| 43 |
@Override |
| 44 |
public void process(Node externs, Node js) {} |
| 45 |
}; |
| 46 |
} |
| 47 |
|
| 48 |
@Override |
| 49 |
public int getNumRepetitions() { |
| 50 |
return 1; |
| 51 |
} |
| 52 |
|
| 53 |
public void testValidBuiltInTypeRedefinition() throws Exception { |
| 54 |
testSame(ALL_NATIVE_EXTERN_TYPES, "", null); |
| 55 |
} |
| 56 |
|
| 57 |
public void testBuiltInTypeDifferentReturnType() throws Exception { |
| 58 |
testSame( |
| 59 |
"/**\n" |
| 60 |
+ " * @constructor\n" |
| 61 |
+ " * @param {*} opt_str\n" |
| 62 |
+ " * @return {number}\n" |
| 63 |
+ " */\n" |
| 64 |
+ "function String(opt_str) {}\n", |
| 65 |
"", FunctionTypeBuilder.TYPE_REDEFINITION, |
| 66 |
"attempted re-definition of type String\n" |
| 67 |
+ "found : function (new:String, *=): number\n" |
| 68 |
+ "expected: function (new:String, *=): string"); |
| 69 |
} |
| 70 |
|
| 71 |
public void testBuiltInTypeDifferentNumParams() throws Exception { |
| 72 |
testSame( |
| 73 |
"/**\n" |
| 74 |
+ " * @constructor\n" |
| 75 |
+ " * @return {string}\n" |
| 76 |
+ " */\n" |
| 77 |
+ "function String() {}\n", |
| 78 |
"", FunctionTypeBuilder.TYPE_REDEFINITION, |
| 79 |
"attempted re-definition of type String\n" |
| 80 |
+ "found : function (new:String): string\n" |
| 81 |
+ "expected: function (new:String, *=): string"); |
| 82 |
} |
| 83 |
|
| 84 |
public void testBuiltInTypeDifferentNumParams2() throws Exception { |
| 85 |
testSame( |
| 86 |
"/**\n" |
| 87 |
+ " * @constructor\n" |
| 88 |
+ " * @return {string}\n" |
| 89 |
+ " */\n" |
| 90 |
+ "function String(opt_str, opt_nothing) {}\n", |
| 91 |
"", FunctionTypeBuilder.TYPE_REDEFINITION, |
| 92 |
"attempted re-definition of type String\n" |
| 93 |
+ "found : function (new:String, ?=, ?=): string\n" |
| 94 |
+ "expected: function (new:String, *=): string"); |
| 95 |
} |
| 96 |
|
| 97 |
public void testBuiltInTypeDifferentParamType() throws Exception { |
| 98 |
testSame( |
| 99 |
"/**\n" |
| 100 |
+ " * @constructor\n" |
| 101 |
+ " * @return {string}\n" |
| 102 |
+ " */\n" |
| 103 |
+ "function String(opt_str) {}\n", |
| 104 |
"", FunctionTypeBuilder.TYPE_REDEFINITION, |
| 105 |
"attempted re-definition of type String\n" |
| 106 |
+ "found : function (new:String, ?=): string\n" |
| 107 |
+ "expected: function (new:String, *=): string"); |
| 108 |
} |
| 109 |
|
| 110 |
public void testBadFunctionTypeDefinition() throws Exception { |
| 111 |
testSame( |
| 112 |
"/** @constructor */function Function(opt_str) {}\n", |
| 113 |
"", FunctionTypeBuilder.TYPE_REDEFINITION, |
| 114 |
"attempted re-definition of type Function\n" |
| 115 |
+ "found : function (new:Function, ?=): ?\n" |
| 116 |
+ "expected: function (new:Function, ...[*]): ?"); |
| 117 |
} |
| 118 |
|
| 119 |
public void testInlineJsDoc() throws Exception { |
| 120 |
testSame( |
| 121 |
"/** @return {number} */ function f(/** string */ x) { return x; }", "", |
| 122 |
TypeValidator.TYPE_MISMATCH_WARNING, |
| 123 |
"inconsistent return type\n" + |
| 124 |
"found : string\n" + |
| 125 |
"required: number"); |
| 126 |
} |
| 127 |
|
| 128 |
public void testInlineJsDoc2() throws Exception { |
| 129 |
testSame( |
| 130 |
"/** @return {T} \n @template T */ " + |
| 131 |
"function f(/** T */ x) { return x; }" + |
| 132 |
"/** @type {string} */ var x = f(1);", "", |
| 133 |
TypeValidator.TYPE_MISMATCH_WARNING, |
| 134 |
"initializing variable\n" + |
| 135 |
"found : number\n" + |
| 136 |
"required: string"); |
| 137 |
} |
| 138 |
|
| 139 |
public void testExternSubTypes() throws Exception { |
| 140 |
testSame(ALL_NATIVE_EXTERN_TYPES, "", null); |
| 141 |
|
| 142 |
List<FunctionType> subtypes = ((ObjectType) getLastCompiler() |
| 143 |
.getTypeRegistry().getType("Error")).getConstructor().getSubTypes(); |
| 144 |
for (FunctionType type : subtypes) { |
| 145 |
String typeName = type.getInstanceType().toString(); |
| 146 |
FunctionType typeInRegistry = ((ObjectType) getLastCompiler() |
| 147 |
.getTypeRegistry().getType(typeName)).getConstructor(); |
| 148 |
assertTrue(typeInRegistry == type); |
| 149 |
} |
| 150 |
} |
| 151 |
} |