| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package com.google.javascript.jscomp; |
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
public class PeepholeFoldWithTypesTest extends CompilerTestCase { |
| 25 |
|
| 26 |
@Override |
| 27 |
protected CompilerPass getProcessor(Compiler compiler) { |
| 28 |
return new PeepholeOptimizationsPass(compiler, new PeepholeFoldWithTypes()); |
| 29 |
} |
| 30 |
|
| 31 |
@Override |
| 32 |
public void setUp() { |
| 33 |
enableTypeCheck(CheckLevel.WARNING); |
| 34 |
} |
| 35 |
|
| 36 |
public void testFoldTypeofObject() { |
| 37 |
test("var x = {};typeof x", |
| 38 |
"var x = {};\"object\""); |
| 39 |
|
| 40 |
test("var x = [];typeof x", |
| 41 |
"var x = [];\"object\""); |
| 42 |
|
| 43 |
|
| 44 |
test("var x = null;typeof x", |
| 45 |
"var x = null;\"object\""); |
| 46 |
} |
| 47 |
|
| 48 |
public void testFoldTypeofString() { |
| 49 |
test("var x = \"foo\";typeof x", |
| 50 |
"var x = \"foo\";\"string\""); |
| 51 |
|
| 52 |
test("var x = new String(\"foo\");typeof x", |
| 53 |
"var x = new String(\"foo\");\"object\""); |
| 54 |
} |
| 55 |
|
| 56 |
public void testFoldTypeofNumber() { |
| 57 |
test("var x = 10;typeof x", |
| 58 |
"var x = 10;\"number\""); |
| 59 |
|
| 60 |
test("var x = new Number(6);typeof x", |
| 61 |
"var x = new Number(6);\"object\""); |
| 62 |
} |
| 63 |
|
| 64 |
public void testFoldTypeofBoolean() { |
| 65 |
test("var x = false;typeof x", |
| 66 |
"var x = false;\"boolean\""); |
| 67 |
|
| 68 |
test("var x = new Boolean(true);typeof x", |
| 69 |
"var x = new Boolean(true);\"object\""); |
| 70 |
} |
| 71 |
|
| 72 |
public void testFoldTypeofUndefined() { |
| 73 |
test("var x = undefined;typeof x", |
| 74 |
"var x = undefined;\"undefined\""); |
| 75 |
} |
| 76 |
|
| 77 |
public void testDontFoldTypeofUnionTypes() { |
| 78 |
|
| 79 |
testSame("var x = (unknown ? {} : null);typeof x"); |
| 80 |
} |
| 81 |
|
| 82 |
public void testDontFoldTypeofSideEffects() { |
| 83 |
|
| 84 |
testSame("var x = 6 ;typeof (x++)"); |
| 85 |
} |
| 86 |
|
| 87 |
public void testDontFoldTypeofWithTypeCheckDisabled() { |
| 88 |
disableTypeCheck(); |
| 89 |
testSame("var x = {};typeof x"); |
| 90 |
} |
| 91 |
} |