Debian Codegraph @

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

raw symbols SCIP
1 /*
2 * Copyright 2012 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 * @author [email protected] (John Lenz)
22 */
23 public class InlinePropertiesTest extends CompilerTestCase {
24
25 private static final String EXTERNS =
26 "Function.prototype.call=function(){};" +
27 "Function.prototype.inherits=function(){};" +
28 "prop.toString;" +
29 "var google = { gears: { factory: {}, workerPool: {} } };";
30
31 public InlinePropertiesTest() {
32 super(EXTERNS);
33 enableNormalize();
34 enableTypeCheck(CheckLevel.WARNING);
35 enableClosurePass();
36 }
37
38 @Override
39 protected CompilerPass getProcessor(Compiler compiler) {
40 return new InlineProperties(compiler);
41 }
42
43 public void testConstInstanceProp1() {
44 // Replace a reference to known constant property.
45 test(
46 "/** @constructor */\n" +
47 "function C() {\n" +
48 " this.foo = 1;\n" +
49 "}\n" +
50 "new C().foo;",
51 "function C() {\n" +
52 " this.foo = 1;\n" +
53 "}\n" +
54 "new C(), 1;");
55 }
56
57 public void testConstInstanceProp2() {
58 // Replace a constant reference
59 test(
60 "/** @constructor */\n" +
61 "function C() {\n" +
62 " this.foo = 1;\n" +
63 "}\n" +
64 "var x = new C();\n" +
65 "x.foo;",
66 "function C() {\n" +
67 " this.foo = 1\n" +
68 "}\n" +
69 "var x = new C();\n" +
70 "1;\n");
71 }
72
73
74 public void testConstInstanceProp3() {
75 // Replace a constant reference
76 test(
77 "/** @constructor */\n" +
78 "function C() {\n" +
79 " this.foo = 1;\n" +
80 "}\n" +
81 "/** @type {C} */\n" +
82 "var x = new C();\n" +
83 "x.foo;",
84 "function C() {\n" +
85 " this.foo = 1\n" +
86 "}\n" +
87 "var x = new C();\n" +
88 "1;\n");
89 }
90
91 public void testConstInstanceProp4() {
92 // This pass replies on DisambiguateProperties to distinguish like named
93 // properties so it doesn't handle this case.
94 testSame(
95 "/** @constructor */\n" +
96 "function C() {\n" +
97 " this.foo = 1;\n" +
98 "}\n" +
99 "/** @constructor */\n" +
100 "function B() {\n" +
101 " this.foo = 1;\n" +
102 "}\n" +
103 "new C().foo;\n");
104 }
105
106
107 public void testConstClassProps1() {
108 // For now, don't inline constant class properties,
109 // CollapseProperties should handle this in most cases.
110 testSame(
111 "/** @constructor */\n" +
112 "function C() {\n" +
113 "}\n" +
114 "C.foo = 1;\n" +
115 "C.foo;");
116 }
117
118 public void testConstClassProps2() {
119 // Don't confuse, class properties with instance properties
120 testSame(
121 "/** @constructor */\n" +
122 "function C() {\n" +
123 " this.foo = 1;\n" +
124 "}\n" +
125 "C.foo;");
126 }
127
128 public void testConstClassProps3() {
129 // Don't confuse, class properties with prototype properties
130 testSame(
131 "/** @constructor */\n" +
132 "function C() {}\n" +
133 "C.prototype.foo = 1;\n" +
134 "c.foo;\n");
135 }
136
137 public void testNonConstClassProp1() {
138 testSame(
139 "/** @constructor */\n" +
140 "function C() {\n" +
141 " this.foo = 1;\n" +
142 "}\n" +
143 "var x = new C();\n" +
144 "alert(x.foo);\n" +
145 "delete x.foo;");
146 }
147
148 public void testNonConstClassProp2() {
149 testSame(
150 "/** @constructor */\n" +
151 "function C() {\n" +
152 " this.foo = 1;\n" +
153 "}\n" +
154 "var x = new C();\n" +
155 "alert(x.foo);\n" +
156 "x.foo = 2;");
157 }
158
159 public void testNonConstructorClassProp1() {
160 testSame(
161 "function C() {\n" +
162 " this.foo = 1;\n" +
163 " return this;\n" +
164 "}\n" +
165 "C().foo;");
166 }
167
168 public void testConditionalClassProp1() {
169 testSame(
170 "/** @constructor */\n" +
171 "function C() {\n" +
172 " if (false) this.foo = 1;\n" +
173 "}\n" +
174 "new C().foo;");
175 }
176
177 public void testConstPrototypeProp1() {
178 test(
179 "/** @constructor */\n" +
180 "function C() {}\n" +
181 "C.prototype.foo = 1;\n" +
182 "new C().foo;\n",
183 "function C() {}\n" +
184 "C.prototype.foo = 1;\n" +
185 "new C(), 1;\n");
186 }
187
188 public void testConstPrototypeProp2() {
189 test(
190 "/** @constructor */\n" +
191 "function C() {}\n" +
192 "C.prototype.foo = 1;\n" +
193 "var x = new C();\n" +
194 "x.foo;\n",
195 "function C() {}\n" +
196 "C.prototype.foo = 1;\n" +
197 "var x = new C();\n" +
198 "1;\n");
199 }
200 }