Debian Codegraph @

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

raw symbols SCIP
1 /*
2 * Copyright 2009 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 /**
21 * {@link IgnoreCajaProperties} tests.
22 *
23 */
24 public class IgnoreCajaPropertiesTest extends CompilerTestCase {
25
26 private static final String EXTERNS =
27 "var z = {}, " +
28 "f = function(y) { z[y] = z[y] ? (z[y]+1) : 1; }, " +
29 "x, i;";
30
31 public IgnoreCajaPropertiesTest() {
32 super(EXTERNS);
33 }
34
35 @Override
36 public void setUp() {
37 super.enableLineNumberCheck(false);
38 }
39
40 @Override
41 public int getNumRepetitions() {
42 return 1;
43 }
44
45 public void testSimpleKey() {
46 // Test a one-statement body.
47 test("for (i in x) f(i);",
48 "for (var JSCompiler_IgnoreCajaProperties_0 in x)" +
49 " if (!JSCompiler_IgnoreCajaProperties_0.match(/___$/)) {" +
50 " i = JSCompiler_IgnoreCajaProperties_0;" +
51 " { f(i); }" +
52 " }");
53 // Test a two-statement body.
54 test("for (i in x) { f(i); f(i); }",
55 "for (var JSCompiler_IgnoreCajaProperties_0 in x)" +
56 " if (!JSCompiler_IgnoreCajaProperties_0.match(/___$/)) {" +
57 " i = JSCompiler_IgnoreCajaProperties_0;" +
58 " { f(i); f(i); }" +
59 " }");
60 // Check that the counter's incrementing properly and
61 // that nested loops work.
62 test("for (i in x) for (j in y) f(i,j);",
63 "for (var JSCompiler_IgnoreCajaProperties_1 in x)" +
64 " if (!JSCompiler_IgnoreCajaProperties_1.match(/___$/)) {" +
65 " i = JSCompiler_IgnoreCajaProperties_1;" +
66 " {" +
67 " for (var JSCompiler_IgnoreCajaProperties_0 in y)" +
68 " if (!JSCompiler_IgnoreCajaProperties_0.match(/___$/)) {" +
69 " j = JSCompiler_IgnoreCajaProperties_0;" +
70 " { f(i,j); }" +
71 " }" +
72 " }" +
73 " }");
74 }
75
76 public void testPropertyKey() {
77 test("for (z.i in x) { f(z.i); f(z.i); }",
78 "for (var JSCompiler_IgnoreCajaProperties_0 in x) {" +
79 " if (!JSCompiler_IgnoreCajaProperties_0.match(/___$/)) {" +
80 " z.i = JSCompiler_IgnoreCajaProperties_0;" +
81 " { f(z.i); f(z.i); }" +
82 " }" +
83 "}");
84 }
85
86 public void testFunctionPropertyKey() {
87 // Note that both in the original code and the
88 // rewritten code, z.j() is invoked on every
89 // iteration of the loop.
90 test("for (z.j().i in x) { f(z.j().i); f(z.j().i); }",
91 "for (var JSCompiler_IgnoreCajaProperties_0 in x) {" +
92 " if (!JSCompiler_IgnoreCajaProperties_0.match(/___$/)) {" +
93 " z.j().i = JSCompiler_IgnoreCajaProperties_0;" +
94 " { f(z.j().i); f(z.j().i); }" +
95 " }" +
96 "}");
97 }
98
99 public void testVarKey() {
100 // Test a one-statement body.
101 test("for (var j in x) { f(j); }",
102 "for (var JSCompiler_IgnoreCajaProperties_0 in x) {" +
103 " if (!JSCompiler_IgnoreCajaProperties_0.match(/___$/)) {" +
104 " var j;" +
105 " j = JSCompiler_IgnoreCajaProperties_0;" +
106 " { f(j); }" +
107 " }" +
108 "}");
109 // Test a two-statement body.
110 test("for (var j in x) { f(j); f(j); }",
111 "for (var JSCompiler_IgnoreCajaProperties_0 in x) {" +
112 " if (!JSCompiler_IgnoreCajaProperties_0.match(/___$/)) {" +
113 " var j;" +
114 " j = JSCompiler_IgnoreCajaProperties_0;" +
115 " { f(j); f(j); }" +
116 " }" +
117 "}");
118 // Test two loops.
119 test("for (var i in x) for (var j in y) f(i,j);",
120 "for (var JSCompiler_IgnoreCajaProperties_1 in x)" +
121 " if (!JSCompiler_IgnoreCajaProperties_1.match(/___$/)) {" +
122 " var i;" +
123 " i = JSCompiler_IgnoreCajaProperties_1;" +
124 " {" +
125 " for (var JSCompiler_IgnoreCajaProperties_0 in y)" +
126 " if (!JSCompiler_IgnoreCajaProperties_0.match(/___$/)) {" +
127 " var j;" +
128 " j = JSCompiler_IgnoreCajaProperties_0;" +
129 " { f(i,j); }" +
130 " }" +
131 " }" +
132 " }");
133 }
134
135 public void testFourChildFor() {
136 test("for (i = 0; i < 10; ++i) { f(i); }",
137 "for (i = 0; i < 10; ++i) { f(i); }");
138 }
139
140 @Override
141 public CompilerPass getProcessor(Compiler compiler) {
142 return new IgnoreCajaProperties(compiler);
143 }
144 }