Debian Codegraph @

test/com/google/javascript/jscomp/PeepholeFoldWithTypesTest.java

raw symbols SCIP
1 /*
2 * Copyright 2010 The Closure Compiler Authors.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 package com.google.javascript.jscomp;
18
19 /**
20 * Tests for {@link ExternExportsPass}.
21 *
22 * @author [email protected] (Devin Coughlin)
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 // typeof null is "object" in JavaScript
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 // For now we don't do anything with union types
79 testSame("var x = (unknown ? {} : null);typeof x");
80 }
81
82 public void testDontFoldTypeofSideEffects() {
83 // Shouldn't fold if argument to typeof has side effects
84 testSame("var x = 6 ;typeof (x++)");
85 }
86
87 public void testDontFoldTypeofWithTypeCheckDisabled() {
88 disableTypeCheck();
89 testSame("var x = {};typeof x");
90 }
91 }