| 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 com.google.common.collect.Lists; |
| 20 |
import com.google.javascript.rhino.Node; |
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
public class RuntimeTypeCheckTest extends CompilerTestCase { |
| 27 |
|
| 28 |
public RuntimeTypeCheckTest() { |
| 29 |
super("/** @const */ var undefined;"); |
| 30 |
enableTypeCheck(CheckLevel.WARNING); |
| 31 |
} |
| 32 |
|
| 33 |
@Override |
| 34 |
protected void setUp() { |
| 35 |
super.enableLineNumberCheck(false); |
| 36 |
enableNormalize(); |
| 37 |
} |
| 38 |
|
| 39 |
public void testValue() { |
| 40 |
testChecks("/** @param {number} i */ function f(i) {}", |
| 41 |
"function f(i) {" + |
| 42 |
" $jscomp.typecheck.checkType(i, " + |
| 43 |
" [$jscomp.typecheck.valueChecker('number')]);" + |
| 44 |
"}"); |
| 45 |
} |
| 46 |
|
| 47 |
public void testConstValue() { |
| 48 |
|
| 49 |
|
| 50 |
testChecks("/** @param {number} CONST */ function f(CONST) {}", |
| 51 |
"function f(CONST) {" + |
| 52 |
" $jscomp.typecheck.checkType(CONST, " + |
| 53 |
" [$jscomp.typecheck.valueChecker('number')]);" + |
| 54 |
"}"); |
| 55 |
} |
| 56 |
|
| 57 |
public void testValueWithInnerFn() { |
| 58 |
testChecks("/** @param {number} i */ function f(i) { function g() {} }", |
| 59 |
"function f(i) {" + |
| 60 |
" function g() {}" + |
| 61 |
" $jscomp.typecheck.checkType(i, " + |
| 62 |
" [$jscomp.typecheck.valueChecker('number')]);" + |
| 63 |
"}"); |
| 64 |
} |
| 65 |
|
| 66 |
public void testNullValue() { |
| 67 |
testChecks("/** @param {null} i */ function f(i) {}", |
| 68 |
"function f(i) {" + |
| 69 |
" $jscomp.typecheck.checkType(i, [$jscomp.typecheck.nullChecker]);" + |
| 70 |
"}"); |
| 71 |
} |
| 72 |
|
| 73 |
public void testValues() { |
| 74 |
testChecks("/** @param {number} i\n@param {string} j*/ function f(i, j) {}", |
| 75 |
"function f(i, j) {" + |
| 76 |
" $jscomp.typecheck.checkType(i, " + |
| 77 |
" [$jscomp.typecheck.valueChecker('number')]);" + |
| 78 |
" $jscomp.typecheck.checkType(j, " + |
| 79 |
" [$jscomp.typecheck.valueChecker('string')]);" + |
| 80 |
"}"); |
| 81 |
} |
| 82 |
|
| 83 |
public void testSkipParamOK() { |
| 84 |
testChecks("/** @param {*} i\n@param {string} j*/ function f(i, j) {}", |
| 85 |
"function f(i, j) {" + |
| 86 |
" $jscomp.typecheck.checkType(j, " + |
| 87 |
" [$jscomp.typecheck.valueChecker('string')]);" + |
| 88 |
"}"); |
| 89 |
} |
| 90 |
|
| 91 |
public void testUnion() { |
| 92 |
testChecks("/** @param {number|string} x */ function f(x) {}", |
| 93 |
"function f(x) {" + |
| 94 |
" $jscomp.typecheck.checkType(x, [" + |
| 95 |
" $jscomp.typecheck.valueChecker('number'), " + |
| 96 |
" $jscomp.typecheck.valueChecker('string')" + |
| 97 |
"]);" + |
| 98 |
"}"); |
| 99 |
} |
| 100 |
|
| 101 |
public void testUntypedParam() { |
| 102 |
testChecks("/** ... */ function f(x) {}", "function f(x) {}"); |
| 103 |
} |
| 104 |
|
| 105 |
public void testReturn() { |
| 106 |
testChecks("/** @return {string} */ function f() { return 'x'; }", |
| 107 |
"function f() {" + |
| 108 |
" return $jscomp.typecheck.checkType('x', " + |
| 109 |
" [$jscomp.typecheck.valueChecker('string')]);" + |
| 110 |
"}"); |
| 111 |
} |
| 112 |
|
| 113 |
public void testNativeClass() { |
| 114 |
testChecks("/** @param {!String} x */ function f(x) {}", |
| 115 |
"function f(x) {" + |
| 116 |
" $jscomp.typecheck.checkType(x, " + |
| 117 |
" [$jscomp.typecheck.externClassChecker('String')]);" + |
| 118 |
"}"); |
| 119 |
} |
| 120 |
|
| 121 |
public void testQualifiedClass() { |
| 122 |
testChecks("var goog = {}; /** @constructor */goog.Foo = function() {};" + |
| 123 |
"/** @param {!goog.Foo} x */ function f(x) {}", |
| 124 |
"var goog = {}; goog.Foo = function() {};" + |
| 125 |
"goog.Foo.prototype['instance_of__goog.Foo'] = true;" + |
| 126 |
"function f(x) {" + |
| 127 |
" $jscomp.typecheck.checkType(x, " + |
| 128 |
" [$jscomp.typecheck.classChecker('goog.Foo')]);" + |
| 129 |
"}"); |
| 130 |
} |
| 131 |
|
| 132 |
public void testInnerClasses() { |
| 133 |
testChecks( |
| 134 |
"function f() { /** @constructor */ function inner() {} }" + |
| 135 |
"function g() { /** @constructor */ function inner() {} }", |
| 136 |
"function f() {" + |
| 137 |
" /** @constructor */ function inner() {}" + |
| 138 |
" inner.prototype['instance_of__inner'] = true;" + |
| 139 |
"}" + |
| 140 |
"function g() {" + |
| 141 |
" /** @constructor */ function inner$$1() {}" + |
| 142 |
" inner$$1.prototype['instance_of__inner$$1'] = true;" + |
| 143 |
"}"); |
| 144 |
} |
| 145 |
|
| 146 |
public void testInterface() { |
| 147 |
testChecks("/** @interface */function I() {}" + |
| 148 |
"/** @param {!I} i */function f(i) {}", |
| 149 |
"function I() {}" + |
| 150 |
"function f(i) {" + |
| 151 |
" $jscomp.typecheck.checkType(i, " + |
| 152 |
" [$jscomp.typecheck.interfaceChecker('I')])" + |
| 153 |
"}"); |
| 154 |
} |
| 155 |
|
| 156 |
public void testImplementedInterface() { |
| 157 |
testChecks("/** @interface */function I() {}" + |
| 158 |
"/** @param {!I} i */function f(i) {}" + |
| 159 |
"/** @constructor\n@implements {I} */function C() {}", |
| 160 |
"function I() {}" + |
| 161 |
"function f(i) {" + |
| 162 |
" $jscomp.typecheck.checkType(i, " + |
| 163 |
" [$jscomp.typecheck.interfaceChecker('I')])" + |
| 164 |
"}" + |
| 165 |
"function C() {}" + |
| 166 |
"C.prototype['instance_of__C'] = true;" + |
| 167 |
"C.prototype['implements__I'] = true;"); |
| 168 |
} |
| 169 |
|
| 170 |
public void testExtendedInterface() { |
| 171 |
testChecks("/** @interface */function I() {}" + |
| 172 |
"/** @interface\n@extends {I} */function J() {}" + |
| 173 |
"/** @param {!I} i */function f(i) {}" + |
| 174 |
"/** @constructor\n@implements {J} */function C() {}", |
| 175 |
"function I() {}" + |
| 176 |
"function J() {}" + |
| 177 |
"function f(i) {" + |
| 178 |
" $jscomp.typecheck.checkType(i, " + |
| 179 |
" [$jscomp.typecheck.interfaceChecker('I')])" + |
| 180 |
"}" + |
| 181 |
"function C() {}" + |
| 182 |
"C.prototype['instance_of__C'] = true;" + |
| 183 |
"C.prototype['implements__I'] = true;" + |
| 184 |
"C.prototype['implements__J'] = true;"); |
| 185 |
} |
| 186 |
|
| 187 |
public void testImplementedInterfaceOrdering() { |
| 188 |
testChecks("/** @interface */function I() {}" + |
| 189 |
"/** @param {!I} i */function f(i) {}" + |
| 190 |
"/** @constructor\n@implements {I} */function C() {}" + |
| 191 |
"C.prototype.f = function() {};", |
| 192 |
"function I() {}" + |
| 193 |
"function f(i) {" + |
| 194 |
" $jscomp.typecheck.checkType(i, " + |
| 195 |
" [$jscomp.typecheck.interfaceChecker('I')])" + |
| 196 |
"}" + |
| 197 |
"function C() {}" + |
| 198 |
"C.prototype['instance_of__C'] = true;" + |
| 199 |
"C.prototype['implements__I'] = true;" + |
| 200 |
"C.prototype.f = function() {};"); |
| 201 |
} |
| 202 |
|
| 203 |
public void testImplementedInterfaceOrderingGoogInherits() { |
| 204 |
testChecks("var goog = {}; goog.inherits = function(x, y) {};" + |
| 205 |
"/** @interface */function I() {}" + |
| 206 |
"/** @param {!I} i */function f(i) {}" + |
| 207 |
"/** @constructor */function B() {}" + |
| 208 |
"/** @constructor\n@extends {B}\n@implements {I} */function C() {}" + |
| 209 |
"goog.inherits(C, B);" + |
| 210 |
"C.prototype.f = function() {};", |
| 211 |
"var goog = {}; goog.inherits = function(x, y) {};" + |
| 212 |
"function I() {}" + |
| 213 |
"function f(i) {" + |
| 214 |
" $jscomp.typecheck.checkType(i, " + |
| 215 |
" [$jscomp.typecheck.interfaceChecker('I')])" + |
| 216 |
"}" + |
| 217 |
"function B() {}" + |
| 218 |
"B.prototype['instance_of__B'] = true;" + |
| 219 |
"function C() {}" + |
| 220 |
"goog.inherits(C, B);" + |
| 221 |
"C.prototype['instance_of__C'] = true;" + |
| 222 |
"C.prototype['implements__I'] = true;" + |
| 223 |
"C.prototype.f = function() {};"); |
| 224 |
} |
| 225 |
|
| 226 |
public void testInnerConstructor() { |
| 227 |
testChecks("(function() { /** @constructor */ function C() {} })()", |
| 228 |
"(function() {" + |
| 229 |
" function C() {} C.prototype['instance_of__C'] = true;" + |
| 230 |
"})()"); |
| 231 |
} |
| 232 |
|
| 233 |
public void testReturnNothing() { |
| 234 |
testChecks("function f() { return; }", "function f() { return; }"); |
| 235 |
} |
| 236 |
|
| 237 |
public void testFunctionType() { |
| 238 |
testChecks("/** @type {!Function} */function f() {}", "function f() {}"); |
| 239 |
} |
| 240 |
|
| 241 |
private void testChecks(String js, String expected) { |
| 242 |
Compiler compiler = new Compiler(); |
| 243 |
compiler.init(Lists.<SourceFile>newArrayList(), |
| 244 |
Lists.<SourceFile>newArrayList(), |
| 245 |
new CompilerOptions()); |
| 246 |
Node base = compiler.loadLibraryCode("base"); |
| 247 |
Node typeCheck = compiler.loadLibraryCode("runtime_type_check"); |
| 248 |
test(js, |
| 249 |
compiler.toSource(base) + ";" |
| 250 |
+ compiler.toSource(typeCheck) + ";" |
| 251 |
+ expected); |
| 252 |
} |
| 253 |
|
| 254 |
@Override |
| 255 |
protected CompilerPass getProcessor(final Compiler compiler) { |
| 256 |
return new RuntimeTypeCheck(compiler, null); |
| 257 |
} |
| 258 |
|
| 259 |
@Override |
| 260 |
protected int getNumRepetitions() { |
| 261 |
return 1; |
| 262 |
} |
| 263 |
} |