| 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.Sets; |
| 20 |
import com.google.javascript.jscomp.type.FlowScope; |
| 21 |
import com.google.javascript.jscomp.type.ReverseAbstractInterpreter; |
| 22 |
import com.google.javascript.jscomp.type.SemanticReverseAbstractInterpreter; |
| 23 |
import com.google.javascript.rhino.Node; |
| 24 |
import com.google.javascript.rhino.Token; |
| 25 |
import com.google.javascript.rhino.jstype.JSType; |
| 26 |
|
| 27 |
import java.util.Arrays; |
| 28 |
import java.util.Collection; |
| 29 |
|
| 30 |
public class SemanticReverseAbstractInterpreterTest |
| 31 |
extends CompilerTypeTestCase { |
| 32 |
private CodingConvention codingConvention = new GoogleCodingConvention(); |
| 33 |
private ReverseAbstractInterpreter interpreter; |
| 34 |
private Scope functionScope; |
| 35 |
|
| 36 |
@Override |
| 37 |
protected void setUp() throws Exception { |
| 38 |
super.setUp(); |
| 39 |
|
| 40 |
interpreter = new SemanticReverseAbstractInterpreter( |
| 41 |
codingConvention, registry); |
| 42 |
} |
| 43 |
|
| 44 |
public FlowScope newScope() { |
| 45 |
Scope globalScope = Scope.createGlobalScope(new Node(Token.EMPTY)); |
| 46 |
functionScope = new Scope(globalScope, new Node(Token.EMPTY)); |
| 47 |
return LinkedFlowScope.createEntryLattice(functionScope); |
| 48 |
} |
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
public void testNameCondition() throws Exception { |
| 54 |
FlowScope blind = newScope(); |
| 55 |
Node condition = createVar(blind, "a", createNullableType(STRING_TYPE)); |
| 56 |
|
| 57 |
|
| 58 |
FlowScope informedTrue = interpreter. |
| 59 |
getPreciserScopeKnowingConditionOutcome(condition, blind, true); |
| 60 |
assertTypeEquals(STRING_TYPE, getVarType(informedTrue, "a")); |
| 61 |
|
| 62 |
|
| 63 |
FlowScope informedFalse = interpreter. |
| 64 |
getPreciserScopeKnowingConditionOutcome(condition, blind, false); |
| 65 |
assertTypeEquals(createNullableType(STRING_TYPE), |
| 66 |
getVarType(informedFalse, "a")); |
| 67 |
} |
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
public void testNegatedNameCondition() throws Exception { |
| 73 |
FlowScope blind = newScope(); |
| 74 |
Node a = createVar(blind, "a", createNullableType(STRING_TYPE)); |
| 75 |
Node condition = new Node(Token.NOT); |
| 76 |
condition.addChildToBack(a); |
| 77 |
|
| 78 |
|
| 79 |
FlowScope informedTrue = interpreter. |
| 80 |
getPreciserScopeKnowingConditionOutcome(condition, blind, true); |
| 81 |
assertTypeEquals(createNullableType(STRING_TYPE), |
| 82 |
getVarType(informedTrue, "a")); |
| 83 |
|
| 84 |
|
| 85 |
FlowScope informedFalse = interpreter. |
| 86 |
getPreciserScopeKnowingConditionOutcome(condition, blind, false); |
| 87 |
assertTypeEquals(STRING_TYPE, getVarType(informedFalse, "a")); |
| 88 |
} |
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
@SuppressWarnings("unchecked") |
| 94 |
public void testAssignCondition1() throws Exception { |
| 95 |
FlowScope blind = newScope(); |
| 96 |
testBinop(blind, |
| 97 |
Token.ASSIGN, |
| 98 |
createVar(blind, "a", createNullableType(OBJECT_TYPE)), |
| 99 |
createVar(blind, "b", createNullableType(OBJECT_TYPE)), |
| 100 |
Sets.newHashSet( |
| 101 |
new TypedName("a", OBJECT_TYPE), |
| 102 |
new TypedName("b", OBJECT_TYPE)), |
| 103 |
Sets.newHashSet( |
| 104 |
new TypedName("a", NULL_TYPE), |
| 105 |
new TypedName("b", NULL_TYPE))); |
| 106 |
} |
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
@SuppressWarnings("unchecked") |
| 112 |
public void testSheqCondition1() throws Exception { |
| 113 |
FlowScope blind = newScope(); |
| 114 |
testBinop(blind, |
| 115 |
Token.SHEQ, |
| 116 |
createVar(blind, "a", createUnionType(STRING_TYPE, NUMBER_TYPE)), |
| 117 |
createNumber(56), |
| 118 |
Sets.newHashSet(new TypedName("a", NUMBER_TYPE)), |
| 119 |
Sets.newHashSet(new TypedName("a", |
| 120 |
createUnionType(STRING_TYPE, NUMBER_TYPE)))); |
| 121 |
} |
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
@SuppressWarnings("unchecked") |
| 127 |
public void testSheqCondition2() throws Exception { |
| 128 |
FlowScope blind = newScope(); |
| 129 |
testBinop(blind, |
| 130 |
Token.SHEQ, |
| 131 |
createNumber(56), |
| 132 |
createVar(blind, "a", createUnionType(STRING_TYPE, NUMBER_TYPE)), |
| 133 |
Sets.newHashSet(new TypedName("a", NUMBER_TYPE)), |
| 134 |
Sets.newHashSet(new TypedName("a", |
| 135 |
createUnionType(STRING_TYPE, NUMBER_TYPE)))); |
| 136 |
} |
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
@SuppressWarnings("unchecked") |
| 142 |
public void testSheqCondition3() throws Exception { |
| 143 |
FlowScope blind = newScope(); |
| 144 |
testBinop(blind, |
| 145 |
Token.SHEQ, |
| 146 |
createVar(blind, "b", createUnionType(STRING_TYPE, BOOLEAN_TYPE)), |
| 147 |
createVar(blind, "a", createUnionType(STRING_TYPE, NUMBER_TYPE)), |
| 148 |
Sets.newHashSet(new TypedName("a", STRING_TYPE), |
| 149 |
new TypedName("b", STRING_TYPE)), |
| 150 |
Sets.newHashSet(new TypedName("a", |
| 151 |
createUnionType(STRING_TYPE, NUMBER_TYPE)), |
| 152 |
new TypedName("b", |
| 153 |
createUnionType(STRING_TYPE, BOOLEAN_TYPE)))); |
| 154 |
} |
| 155 |
|
| 156 |
@SuppressWarnings("unchecked") |
| 157 |
public void testSheqCondition4() throws Exception { |
| 158 |
FlowScope blind = newScope(); |
| 159 |
testBinop(blind, |
| 160 |
Token.SHEQ, |
| 161 |
createVar(blind, "a", createUnionType(STRING_TYPE, VOID_TYPE)), |
| 162 |
createVar(blind, "b", createUnionType(VOID_TYPE)), |
| 163 |
Sets.newHashSet(new TypedName("a", VOID_TYPE), |
| 164 |
new TypedName("b", VOID_TYPE)), |
| 165 |
Sets.newHashSet(new TypedName("a", STRING_TYPE), |
| 166 |
new TypedName("b", VOID_TYPE))); |
| 167 |
} |
| 168 |
|
| 169 |
@SuppressWarnings("unchecked") |
| 170 |
public void testSheqCondition5() throws Exception { |
| 171 |
FlowScope blind = newScope(); |
| 172 |
testBinop(blind, |
| 173 |
Token.SHEQ, |
| 174 |
createVar(blind, "a", createUnionType(NULL_TYPE, VOID_TYPE)), |
| 175 |
createVar(blind, "b", createUnionType(VOID_TYPE)), |
| 176 |
Sets.newHashSet(new TypedName("a", VOID_TYPE), |
| 177 |
new TypedName("b", VOID_TYPE)), |
| 178 |
Sets.newHashSet(new TypedName("a", NULL_TYPE), |
| 179 |
new TypedName("b", VOID_TYPE))); |
| 180 |
} |
| 181 |
|
| 182 |
@SuppressWarnings("unchecked") |
| 183 |
public void testSheqCondition6() throws Exception { |
| 184 |
FlowScope blind = newScope(); |
| 185 |
testBinop(blind, |
| 186 |
Token.SHEQ, |
| 187 |
createVar(blind, "a", createUnionType(STRING_TYPE, VOID_TYPE)), |
| 188 |
createVar(blind, "b", createUnionType(NUMBER_TYPE, VOID_TYPE)), |
| 189 |
Sets.newHashSet( |
| 190 |
new TypedName("a", VOID_TYPE), |
| 191 |
new TypedName("b", VOID_TYPE)), |
| 192 |
Sets.newHashSet( |
| 193 |
new TypedName("a", |
| 194 |
createUnionType(STRING_TYPE, VOID_TYPE)), |
| 195 |
new TypedName("b", |
| 196 |
createUnionType(NUMBER_TYPE, VOID_TYPE)))); |
| 197 |
} |
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
@SuppressWarnings("unchecked") |
| 203 |
public void testShneCondition1() throws Exception { |
| 204 |
FlowScope blind = newScope(); |
| 205 |
testBinop(blind, |
| 206 |
Token.SHNE, |
| 207 |
createVar(blind, "a", createUnionType(STRING_TYPE, NUMBER_TYPE)), |
| 208 |
createNumber(56), |
| 209 |
Sets.newHashSet(new TypedName("a", |
| 210 |
createUnionType(STRING_TYPE, NUMBER_TYPE))), |
| 211 |
Sets.newHashSet(new TypedName("a", NUMBER_TYPE))); |
| 212 |
} |
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
@SuppressWarnings("unchecked") |
| 218 |
public void testShneCondition2() throws Exception { |
| 219 |
FlowScope blind = newScope(); |
| 220 |
testBinop(blind, |
| 221 |
Token.SHNE, |
| 222 |
createNumber(56), |
| 223 |
createVar(blind, "a", createUnionType(STRING_TYPE, NUMBER_TYPE)), |
| 224 |
Sets.newHashSet(new TypedName("a", |
| 225 |
createUnionType(STRING_TYPE, NUMBER_TYPE))), |
| 226 |
Sets.newHashSet(new TypedName("a", NUMBER_TYPE))); |
| 227 |
} |
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
@SuppressWarnings("unchecked") |
| 233 |
public void testShneCondition3() throws Exception { |
| 234 |
FlowScope blind = newScope(); |
| 235 |
testBinop(blind, |
| 236 |
Token.SHNE, |
| 237 |
createVar(blind, "b", createUnionType(STRING_TYPE, BOOLEAN_TYPE)), |
| 238 |
createVar(blind, "a", createUnionType(STRING_TYPE, NUMBER_TYPE)), |
| 239 |
Sets.newHashSet(new TypedName("a", |
| 240 |
createUnionType(STRING_TYPE, NUMBER_TYPE)), |
| 241 |
new TypedName("b", |
| 242 |
createUnionType(STRING_TYPE, BOOLEAN_TYPE))), |
| 243 |
Sets.newHashSet(new TypedName("a", STRING_TYPE), |
| 244 |
new TypedName("b", STRING_TYPE))); |
| 245 |
} |
| 246 |
|
| 247 |
@SuppressWarnings("unchecked") |
| 248 |
public void testShneCondition4() throws Exception { |
| 249 |
FlowScope blind = newScope(); |
| 250 |
testBinop(blind, |
| 251 |
Token.SHNE, |
| 252 |
createVar(blind, "a", createUnionType(STRING_TYPE, VOID_TYPE)), |
| 253 |
createVar(blind, "b", createUnionType(VOID_TYPE)), |
| 254 |
Sets.newHashSet(new TypedName("a", STRING_TYPE), |
| 255 |
new TypedName("b", VOID_TYPE)), |
| 256 |
Sets.newHashSet(new TypedName("a", VOID_TYPE), |
| 257 |
new TypedName("b", VOID_TYPE))); |
| 258 |
} |
| 259 |
|
| 260 |
@SuppressWarnings("unchecked") |
| 261 |
public void testShneCondition5() throws Exception { |
| 262 |
FlowScope blind = newScope(); |
| 263 |
testBinop(blind, |
| 264 |
Token.SHNE, |
| 265 |
createVar(blind, "a", createUnionType(NULL_TYPE, VOID_TYPE)), |
| 266 |
createVar(blind, "b", createUnionType(NULL_TYPE)), |
| 267 |
Sets.newHashSet(new TypedName("a", VOID_TYPE), |
| 268 |
new TypedName("b", NULL_TYPE)), |
| 269 |
Sets.newHashSet(new TypedName("a", NULL_TYPE), |
| 270 |
new TypedName("b", NULL_TYPE))); |
| 271 |
} |
| 272 |
|
| 273 |
@SuppressWarnings("unchecked") |
| 274 |
public void testShneCondition6() throws Exception { |
| 275 |
FlowScope blind = newScope(); |
| 276 |
testBinop(blind, |
| 277 |
Token.SHNE, |
| 278 |
createVar(blind, "a", createUnionType(STRING_TYPE, VOID_TYPE)), |
| 279 |
createVar(blind, "b", createUnionType(NUMBER_TYPE, VOID_TYPE)), |
| 280 |
Sets.newHashSet( |
| 281 |
new TypedName("a", |
| 282 |
createUnionType(STRING_TYPE, VOID_TYPE)), |
| 283 |
new TypedName("b", |
| 284 |
createUnionType(NUMBER_TYPE, VOID_TYPE))), |
| 285 |
Sets.newHashSet( |
| 286 |
new TypedName("a", VOID_TYPE), |
| 287 |
new TypedName("b", VOID_TYPE))); |
| 288 |
} |
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
@SuppressWarnings("unchecked") |
| 294 |
public void testEqCondition1() throws Exception { |
| 295 |
FlowScope blind = newScope(); |
| 296 |
testBinop(blind, |
| 297 |
Token.EQ, |
| 298 |
createVar(blind, "a", createUnionType(BOOLEAN_TYPE, VOID_TYPE)), |
| 299 |
createNull(), |
| 300 |
Sets.newHashSet(new TypedName("a", VOID_TYPE)), |
| 301 |
Sets.newHashSet(new TypedName("a", BOOLEAN_TYPE))); |
| 302 |
} |
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
@SuppressWarnings("unchecked") |
| 308 |
public void testEqCondition2() throws Exception { |
| 309 |
FlowScope blind = newScope(); |
| 310 |
testBinop(blind, |
| 311 |
Token.NE, |
| 312 |
createNull(), |
| 313 |
createVar(blind, "a", createUnionType(BOOLEAN_TYPE, VOID_TYPE)), |
| 314 |
Sets.newHashSet(new TypedName("a", BOOLEAN_TYPE)), |
| 315 |
Sets.newHashSet(new TypedName("a", VOID_TYPE))); |
| 316 |
} |
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
@SuppressWarnings("unchecked") |
| 322 |
public void testEqCondition3() throws Exception { |
| 323 |
FlowScope blind = newScope(); |
| 324 |
|
| 325 |
JSType nullableOptionalNumber = |
| 326 |
createUnionType(NULL_TYPE, VOID_TYPE, NUMBER_TYPE); |
| 327 |
|
| 328 |
JSType nullUndefined = |
| 329 |
createUnionType(VOID_TYPE, NULL_TYPE); |
| 330 |
testBinop(blind, |
| 331 |
Token.EQ, |
| 332 |
createVar(blind, "a", nullableOptionalNumber), |
| 333 |
createNull(), |
| 334 |
Sets.newHashSet(new TypedName("a", nullUndefined)), |
| 335 |
Sets.newHashSet(new TypedName("a", NUMBER_TYPE))); |
| 336 |
} |
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
@SuppressWarnings("unchecked") |
| 342 |
public void testEqCondition4() throws Exception { |
| 343 |
FlowScope blind = newScope(); |
| 344 |
testBinop(blind, |
| 345 |
Token.EQ, |
| 346 |
createVar(blind, "a", VOID_TYPE), |
| 347 |
createVar(blind, "b", VOID_TYPE), |
| 348 |
Sets.newHashSet( |
| 349 |
new TypedName("a", VOID_TYPE), |
| 350 |
new TypedName("b", VOID_TYPE)), |
| 351 |
Sets.newHashSet( |
| 352 |
new TypedName("a", NO_TYPE), |
| 353 |
new TypedName("b", NO_TYPE))); |
| 354 |
} |
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
@SuppressWarnings("unchecked") |
| 361 |
public void testInequalitiesCondition1() { |
| 362 |
for (int op : Arrays.asList(Token.LT, Token.GT, Token.LE, Token.GE)) { |
| 363 |
FlowScope blind = newScope(); |
| 364 |
testBinop(blind, |
| 365 |
op, |
| 366 |
createVar(blind, "a", createUnionType(STRING_TYPE, VOID_TYPE)), |
| 367 |
createNumber(8), |
| 368 |
Sets.newHashSet( |
| 369 |
new TypedName("a", STRING_TYPE)), |
| 370 |
Sets.newHashSet(new TypedName("a", |
| 371 |
createUnionType(STRING_TYPE, VOID_TYPE)))); |
| 372 |
} |
| 373 |
} |
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
@SuppressWarnings("unchecked") |
| 380 |
public void testInequalitiesCondition2() { |
| 381 |
for (int op : Arrays.asList(Token.LT, Token.GT, Token.LE, Token.GE)) { |
| 382 |
FlowScope blind = newScope(); |
| 383 |
testBinop(blind, |
| 384 |
op, |
| 385 |
createVar(blind, "a", |
| 386 |
createUnionType(STRING_TYPE, NUMBER_TYPE, VOID_TYPE)), |
| 387 |
createVar(blind, "b", |
| 388 |
createUnionType(NUMBER_TYPE, NULL_TYPE)), |
| 389 |
Sets.newHashSet( |
| 390 |
new TypedName("a", |
| 391 |
createUnionType(STRING_TYPE, NUMBER_TYPE)), |
| 392 |
new TypedName("b", |
| 393 |
createUnionType(NUMBER_TYPE, NULL_TYPE))), |
| 394 |
Sets.newHashSet( |
| 395 |
new TypedName("a", |
| 396 |
createUnionType(STRING_TYPE, NUMBER_TYPE, VOID_TYPE)), |
| 397 |
new TypedName("b", |
| 398 |
createUnionType(NUMBER_TYPE, NULL_TYPE)))); |
| 399 |
} |
| 400 |
} |
| 401 |
|
| 402 |
|
| 403 |
|
| 404 |
|
| 405 |
|
| 406 |
@SuppressWarnings("unchecked") |
| 407 |
public void testInequalitiesCondition3() { |
| 408 |
for (int op : Arrays.asList(Token.LT, Token.GT, Token.LE, Token.GE)) { |
| 409 |
FlowScope blind = newScope(); |
| 410 |
testBinop(blind, |
| 411 |
op, |
| 412 |
createUntypedNumber(8), |
| 413 |
createVar(blind, "a", createUnionType(STRING_TYPE, VOID_TYPE)), |
| 414 |
Sets.newHashSet( |
| 415 |
new TypedName("a", STRING_TYPE)), |
| 416 |
Sets.newHashSet(new TypedName("a", |
| 417 |
createUnionType(STRING_TYPE, VOID_TYPE)))); |
| 418 |
} |
| 419 |
} |
| 420 |
|
| 421 |
@SuppressWarnings("unchecked") |
| 422 |
public void testAnd() { |
| 423 |
FlowScope blind = newScope(); |
| 424 |
testBinop(blind, |
| 425 |
Token.AND, |
| 426 |
createVar(blind, "b", createUnionType(STRING_TYPE, NULL_TYPE)), |
| 427 |
createVar(blind, "a", createUnionType(NUMBER_TYPE, VOID_TYPE)), |
| 428 |
Sets.newHashSet(new TypedName("a", NUMBER_TYPE), |
| 429 |
new TypedName("b", STRING_TYPE)), |
| 430 |
Sets.newHashSet(new TypedName("a", |
| 431 |
createUnionType(NUMBER_TYPE, VOID_TYPE)), |
| 432 |
new TypedName("b", |
| 433 |
createUnionType(STRING_TYPE, NULL_TYPE)))); |
| 434 |
} |
| 435 |
|
| 436 |
@SuppressWarnings("unchecked") |
| 437 |
public void testTypeof1() { |
| 438 |
FlowScope blind = newScope(); |
| 439 |
testBinop(blind, |
| 440 |
Token.EQ, |
| 441 |
new Node(Token.TYPEOF, createVar(blind, "a", OBJECT_TYPE)), |
| 442 |
Node.newString("function"), |
| 443 |
Sets.newHashSet( |
| 444 |
new TypedName("a", U2U_CONSTRUCTOR_TYPE)), |
| 445 |
Sets.newHashSet( |
| 446 |
new TypedName("a", OBJECT_TYPE))); |
| 447 |
} |
| 448 |
|
| 449 |
@SuppressWarnings("unchecked") |
| 450 |
public void testTypeof2() { |
| 451 |
FlowScope blind = newScope(); |
| 452 |
testBinop(blind, |
| 453 |
Token.EQ, |
| 454 |
new Node(Token.TYPEOF, createVar(blind, "a", ALL_TYPE)), |
| 455 |
Node.newString("function"), |
| 456 |
Sets.newHashSet( |
| 457 |
new TypedName("a", U2U_CONSTRUCTOR_TYPE)), |
| 458 |
Sets.newHashSet( |
| 459 |
new TypedName("a", ALL_TYPE))); |
| 460 |
} |
| 461 |
|
| 462 |
@SuppressWarnings("unchecked") |
| 463 |
public void testTypeof3() { |
| 464 |
FlowScope blind = newScope(); |
| 465 |
testBinop(blind, |
| 466 |
Token.EQ, |
| 467 |
new Node(Token.TYPEOF, createVar( |
| 468 |
blind, "a", OBJECT_NUMBER_STRING_BOOLEAN)), |
| 469 |
Node.newString("function"), |
| 470 |
Sets.newHashSet( |
| 471 |
new TypedName("a", U2U_CONSTRUCTOR_TYPE)), |
| 472 |
Sets.newHashSet( |
| 473 |
new TypedName("a", OBJECT_NUMBER_STRING_BOOLEAN))); |
| 474 |
} |
| 475 |
|
| 476 |
@SuppressWarnings("unchecked") |
| 477 |
public void testTypeof4() { |
| 478 |
FlowScope blind = newScope(); |
| 479 |
testBinop(blind, |
| 480 |
Token.EQ, |
| 481 |
new Node(Token.TYPEOF, createVar( |
| 482 |
blind, "a", createUnionType( |
| 483 |
U2U_CONSTRUCTOR_TYPE,NUMBER_STRING_BOOLEAN))), |
| 484 |
Node.newString("function"), |
| 485 |
Sets.newHashSet( |
| 486 |
new TypedName("a", U2U_CONSTRUCTOR_TYPE)), |
| 487 |
Sets.newHashSet( |
| 488 |
new TypedName("a", NUMBER_STRING_BOOLEAN))); |
| 489 |
} |
| 490 |
|
| 491 |
@SuppressWarnings("unchecked") |
| 492 |
public void testInstanceOf() { |
| 493 |
FlowScope blind = newScope(); |
| 494 |
testBinop(blind, |
| 495 |
Token.INSTANCEOF, |
| 496 |
createVar(blind, "x", UNKNOWN_TYPE), |
| 497 |
createVar(blind, "s", STRING_OBJECT_FUNCTION_TYPE), |
| 498 |
Sets.newHashSet( |
| 499 |
new TypedName("x", STRING_OBJECT_TYPE), |
| 500 |
new TypedName("s", STRING_OBJECT_FUNCTION_TYPE)), |
| 501 |
Sets.newHashSet( |
| 502 |
new TypedName("s", STRING_OBJECT_FUNCTION_TYPE))); |
| 503 |
} |
| 504 |
|
| 505 |
@SuppressWarnings("unchecked") |
| 506 |
public void testInstanceOf2() { |
| 507 |
FlowScope blind = newScope(); |
| 508 |
testBinop(blind, |
| 509 |
Token.INSTANCEOF, |
| 510 |
createVar(blind, "x", |
| 511 |
createUnionType(STRING_OBJECT_TYPE, NUMBER_OBJECT_TYPE)), |
| 512 |
createVar(blind, "s", STRING_OBJECT_FUNCTION_TYPE), |
| 513 |
Sets.newHashSet( |
| 514 |
new TypedName("x", STRING_OBJECT_TYPE), |
| 515 |
new TypedName("s", STRING_OBJECT_FUNCTION_TYPE)), |
| 516 |
Sets.newHashSet( |
| 517 |
new TypedName("x", NUMBER_OBJECT_TYPE), |
| 518 |
new TypedName("s", STRING_OBJECT_FUNCTION_TYPE))); |
| 519 |
} |
| 520 |
|
| 521 |
@SuppressWarnings("unchecked") |
| 522 |
public void testInstanceOf3() { |
| 523 |
FlowScope blind = newScope(); |
| 524 |
testBinop(blind, |
| 525 |
Token.INSTANCEOF, |
| 526 |
createVar(blind, "x", OBJECT_TYPE), |
| 527 |
createVar(blind, "s", STRING_OBJECT_FUNCTION_TYPE), |
| 528 |
Sets.newHashSet( |
| 529 |
new TypedName("x", STRING_OBJECT_TYPE), |
| 530 |
new TypedName("s", STRING_OBJECT_FUNCTION_TYPE)), |
| 531 |
Sets.newHashSet( |
| 532 |
new TypedName("x", OBJECT_TYPE), |
| 533 |
new TypedName("s", STRING_OBJECT_FUNCTION_TYPE))); |
| 534 |
} |
| 535 |
|
| 536 |
@SuppressWarnings("unchecked") |
| 537 |
public void testInstanceOf4() { |
| 538 |
FlowScope blind = newScope(); |
| 539 |
testBinop(blind, |
| 540 |
Token.INSTANCEOF, |
| 541 |
createVar(blind, "x", ALL_TYPE), |
| 542 |
createVar(blind, "s", STRING_OBJECT_FUNCTION_TYPE), |
| 543 |
Sets.newHashSet( |
| 544 |
new TypedName("x", STRING_OBJECT_TYPE), |
| 545 |
new TypedName("s", STRING_OBJECT_FUNCTION_TYPE)), |
| 546 |
Sets.newHashSet( |
| 547 |
new TypedName("s", STRING_OBJECT_FUNCTION_TYPE))); |
| 548 |
} |
| 549 |
|
| 550 |
private void testBinop(FlowScope blind, int binop, Node left, Node right, |
| 551 |
Collection<TypedName> trueOutcome, |
| 552 |
Collection<TypedName> falseOutcome) { |
| 553 |
Node condition = new Node(binop); |
| 554 |
condition.addChildToBack(left); |
| 555 |
condition.addChildToBack(right); |
| 556 |
|
| 557 |
|
| 558 |
FlowScope informedTrue = interpreter. |
| 559 |
getPreciserScopeKnowingConditionOutcome(condition, blind, true); |
| 560 |
for (TypedName p : trueOutcome) { |
| 561 |
assertTypeEquals(p.name, p.type, getVarType(informedTrue, p.name)); |
| 562 |
} |
| 563 |
|
| 564 |
|
| 565 |
FlowScope informedFalse = interpreter. |
| 566 |
getPreciserScopeKnowingConditionOutcome(condition, blind, false); |
| 567 |
for (TypedName p : falseOutcome) { |
| 568 |
assertTypeEquals(p.type, getVarType(informedFalse, p.name)); |
| 569 |
} |
| 570 |
} |
| 571 |
|
| 572 |
private Node createNull() { |
| 573 |
Node n = new Node(Token.NULL); |
| 574 |
n.setJSType(NULL_TYPE); |
| 575 |
return n; |
| 576 |
} |
| 577 |
|
| 578 |
private Node createNumber(int n) { |
| 579 |
Node number = createUntypedNumber(n); |
| 580 |
number.setJSType(NUMBER_TYPE); |
| 581 |
return number; |
| 582 |
} |
| 583 |
|
| 584 |
private Node createUntypedNumber(int n) { |
| 585 |
return Node.newNumber(n); |
| 586 |
} |
| 587 |
|
| 588 |
private JSType getVarType(FlowScope scope, String name) { |
| 589 |
return scope.getSlot(name).getType(); |
| 590 |
} |
| 591 |
|
| 592 |
private Node createVar(FlowScope scope, String name, JSType type) { |
| 593 |
Node n = Node.newString(Token.NAME, name); |
| 594 |
functionScope.declare(name, n, null, null); |
| 595 |
((LinkedFlowScope) scope).inferSlotType(name, type); |
| 596 |
n.setJSType(type); |
| 597 |
return n; |
| 598 |
} |
| 599 |
|
| 600 |
private static class TypedName { |
| 601 |
private final String name; |
| 602 |
private final JSType type; |
| 603 |
|
| 604 |
private TypedName(String name, JSType type) { |
| 605 |
this.name = name; |
| 606 |
this.type = type; |
| 607 |
} |
| 608 |
} |
| 609 |
} |