| 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.jscomp.TypeCheck.WRONG_ARGUMENT_COUNT; |
| 20 |
import static com.google.javascript.jscomp.FunctionTypeBuilder.OPTIONAL_ARG_AT_END; |
| 21 |
import static com.google.javascript.jscomp.FunctionTypeBuilder.VAR_ARGS_MUST_BE_LAST; |
| 22 |
|
| 23 |
import com.google.javascript.jscomp.CheckLevel; |
| 24 |
import com.google.javascript.rhino.Node; |
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
public class TypeCheckFunctionCheckTest extends CompilerTestCase { |
| 31 |
|
| 32 |
private CodingConvention convention = null; |
| 33 |
|
| 34 |
public TypeCheckFunctionCheckTest() { |
| 35 |
parseTypeInfo = true; |
| 36 |
enableTypeCheck(CheckLevel.ERROR); |
| 37 |
} |
| 38 |
|
| 39 |
@Override protected CompilerPass getProcessor(Compiler compiler) { |
| 40 |
return new CompilerPass() { |
| 41 |
@Override |
| 42 |
public void process(Node externs, Node root) {} |
| 43 |
}; |
| 44 |
} |
| 45 |
|
| 46 |
@Override |
| 47 |
protected CodingConvention getCodingConvention() { |
| 48 |
return convention; |
| 49 |
} |
| 50 |
|
| 51 |
@Override |
| 52 |
protected int getNumRepetitions() { |
| 53 |
|
| 54 |
|
| 55 |
return 1; |
| 56 |
} |
| 57 |
|
| 58 |
@Override |
| 59 |
public void setUp() throws Exception { |
| 60 |
super.setUp(); |
| 61 |
convention = new GoogleCodingConvention(); |
| 62 |
} |
| 63 |
|
| 64 |
public void testFunctionAritySimple() { |
| 65 |
assertOk("", ""); |
| 66 |
assertOk("a", "'a'"); |
| 67 |
assertOk("a,b", "10, 20"); |
| 68 |
} |
| 69 |
|
| 70 |
public void testFunctionArityWithOptionalArgs() { |
| 71 |
assertOk("a,b,opt_c", "1,2"); |
| 72 |
assertOk("a,b,opt_c", "1,2,3"); |
| 73 |
assertOk("a,opt_b,opt_c", "1"); |
| 74 |
} |
| 75 |
|
| 76 |
public void testFunctionArityWithVarArgs() { |
| 77 |
assertOk("var_args", ""); |
| 78 |
assertOk("var_args", "1,2"); |
| 79 |
assertOk("a,b,var_args", "1,2"); |
| 80 |
assertOk("a,b,var_args", "1,2,3"); |
| 81 |
assertOk("a,b,var_args", "1,2,3,4,5"); |
| 82 |
assertOk("a,opt_b,var_args", "1"); |
| 83 |
assertOk("a,opt_b,var_args", "1,2"); |
| 84 |
assertOk("a,opt_b,var_args", "1,2,3"); |
| 85 |
assertOk("a,opt_b,var_args", "1,2,3,4,5"); |
| 86 |
} |
| 87 |
|
| 88 |
public void testWrongNumberOfArgs() { |
| 89 |
assertWarning("a,b,opt_c", "1", |
| 90 |
WRONG_ARGUMENT_COUNT); |
| 91 |
assertWarning("a,b,opt_c", "1,2,3,4", |
| 92 |
WRONG_ARGUMENT_COUNT); |
| 93 |
assertWarning("a,b", "1, 2, 3", |
| 94 |
WRONG_ARGUMENT_COUNT); |
| 95 |
assertWarning("", "1, 2, 3", |
| 96 |
WRONG_ARGUMENT_COUNT); |
| 97 |
assertWarning("a,b,c,d", "1, 2, 3", |
| 98 |
WRONG_ARGUMENT_COUNT); |
| 99 |
assertWarning("a,b,var_args", "1", |
| 100 |
WRONG_ARGUMENT_COUNT); |
| 101 |
assertWarning("a,b,opt_c,var_args", "1", |
| 102 |
WRONG_ARGUMENT_COUNT); |
| 103 |
} |
| 104 |
|
| 105 |
public void testVarArgsLast() { |
| 106 |
assertWarning("a,b,var_args,c", "1,2,3,4", |
| 107 |
VAR_ARGS_MUST_BE_LAST); |
| 108 |
} |
| 109 |
|
| 110 |
public void testOptArgsLast() { |
| 111 |
assertWarning("a,b,opt_d,c", "1, 2, 3", |
| 112 |
OPTIONAL_ARG_AT_END); |
| 113 |
assertWarning("a,b,opt_d,c", "1, 2", |
| 114 |
OPTIONAL_ARG_AT_END); |
| 115 |
} |
| 116 |
|
| 117 |
public void testFunctionsWithJsDoc1() { |
| 118 |
testSame("/** @param {*=} c */ function foo(a,b,c) {} foo(1,2);"); |
| 119 |
} |
| 120 |
|
| 121 |
public void testFunctionsWithJsDoc2() { |
| 122 |
testSame("/** @param {*=} c */ function foo(a,b,c) {} foo(1,2,3);"); |
| 123 |
} |
| 124 |
|
| 125 |
public void testFunctionsWithJsDoc3() { |
| 126 |
testSame("/** @param {*=} c \n * @param {*=} b */ " + |
| 127 |
"function foo(a,b,c) {} foo(1);"); |
| 128 |
} |
| 129 |
|
| 130 |
public void testFunctionsWithJsDoc4() { |
| 131 |
testSame("/** @param {...*} a */ var foo = function(a) {}; foo();"); |
| 132 |
} |
| 133 |
|
| 134 |
public void testFunctionsWithJsDoc5() { |
| 135 |
testSame("/** @param {...*} a */ var foo = function(a) {}; foo(1,2);"); |
| 136 |
} |
| 137 |
|
| 138 |
public void testFunctionsWithJsDoc6() { |
| 139 |
testSame("/** @param {...*} b */ var foo = function(a, b) {}; foo();", |
| 140 |
WRONG_ARGUMENT_COUNT); |
| 141 |
} |
| 142 |
|
| 143 |
public void testFunctionsWithJsDoc7() { |
| 144 |
String fooDfn = "/** @param {*} [b] */ var foo = function(b) {};"; |
| 145 |
testSame(fooDfn + "foo();"); |
| 146 |
testSame(fooDfn + "foo(1);"); |
| 147 |
testSame(fooDfn + "foo(1, 2);", WRONG_ARGUMENT_COUNT); |
| 148 |
} |
| 149 |
|
| 150 |
public void testFunctionWithDefaultCodingConvention() { |
| 151 |
convention = CodingConventions.getDefault(); |
| 152 |
testSame("var foo = function(x) {}; foo(1, 2);", WRONG_ARGUMENT_COUNT); |
| 153 |
testSame("var foo = function(opt_x) {}; foo(1, 2);", WRONG_ARGUMENT_COUNT); |
| 154 |
testSame("var foo = function(var_args) {}; foo(1, 2);", |
| 155 |
WRONG_ARGUMENT_COUNT); |
| 156 |
} |
| 157 |
|
| 158 |
public void testMethodCalls() { |
| 159 |
final String METHOD_DEFS = |
| 160 |
"/** @constructor */\n" + |
| 161 |
"function Foo() {}" + |
| 162 |
|
| 163 |
"function twoArg(arg1, arg2) {};" + |
| 164 |
"Foo.prototype.prototypeMethod = twoArg;" + |
| 165 |
"Foo.staticMethod = twoArg;" + |
| 166 |
|
| 167 |
"/**\n * @constructor\n * @return {Bar}\n */\n" + |
| 168 |
"function Bar() {}"; |
| 169 |
|
| 170 |
|
| 171 |
testSame(METHOD_DEFS + |
| 172 |
"var f = new Foo();f.prototypeMethod(1, 2, 3);", |
| 173 |
TypeCheck.WRONG_ARGUMENT_COUNT); |
| 174 |
|
| 175 |
testSame(METHOD_DEFS + |
| 176 |
"var f = new Foo();f.prototypeMethod(1);", |
| 177 |
TypeCheck.WRONG_ARGUMENT_COUNT); |
| 178 |
|
| 179 |
|
| 180 |
testSame(METHOD_DEFS + |
| 181 |
"Foo.staticMethod(1, 2, 3);", |
| 182 |
TypeCheck.WRONG_ARGUMENT_COUNT); |
| 183 |
|
| 184 |
testSame(METHOD_DEFS + |
| 185 |
"Foo.staticMethod(1);", |
| 186 |
TypeCheck.WRONG_ARGUMENT_COUNT); |
| 187 |
|
| 188 |
|
| 189 |
testSame(METHOD_DEFS + "Bar();", TypeCheck.CONSTRUCTOR_NOT_CALLABLE); |
| 190 |
|
| 191 |
|
| 192 |
testSame(METHOD_DEFS, "Foo();", TypeCheck.CONSTRUCTOR_NOT_CALLABLE); |
| 193 |
|
| 194 |
|
| 195 |
testSame(METHOD_DEFS, "Bar();", null); |
| 196 |
} |
| 197 |
|
| 198 |
public void assertOk(String params, String arguments) { |
| 199 |
assertWarning(params, arguments, null); |
| 200 |
} |
| 201 |
|
| 202 |
public void assertWarning(String params, String arguments, |
| 203 |
DiagnosticType type) { |
| 204 |
testSame("function foo(" + params + ") {} foo(" + arguments + ");", |
| 205 |
type); |
| 206 |
} |
| 207 |
} |