Debian Codegraph @

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

raw symbols SCIP
1 /*
2 * Copyright 2008 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 import com.google.javascript.jscomp.ExtractPrototypeMemberDeclarations.Pattern;
20
21 /**
22 * Tests for {@link ExtractPrototypeMemberDeclarations}.
23 *
24 */
25 public class ExtractPrototypeMemberDeclarationsTest extends CompilerTestCase {
26 private static final String TMP = "JSCompiler_prototypeAlias";
27 private Pattern pattern = Pattern.USE_GLOBAL_TEMP;
28
29 @Override
30 protected void setUp() {
31 super.enableLineNumberCheck(true);
32 enableNormalize();
33 pattern = Pattern.USE_GLOBAL_TEMP;
34 }
35
36 @Override
37 protected CompilerPass getProcessor(Compiler compiler) {
38 return new ExtractPrototypeMemberDeclarations(compiler, pattern);
39 }
40
41 public void testNotEnoughPrototypeToExtract() {
42 // switch statement with stuff after "return"
43 for (int i = 0; i < 7; i++) {
44 testSame(generatePrototypeDeclarations("x", i));
45 }
46 }
47
48 public void testExtractingSingleClassPrototype() {
49 extract(generatePrototypeDeclarations("x", 7),
50 loadPrototype("x") +
51 generateExtractedDeclarations(7));
52 }
53
54 public void testExtractingTwoClassPrototype() {
55 extract(
56 generatePrototypeDeclarations("x", 6) +
57 generatePrototypeDeclarations("y", 6),
58 loadPrototype("x") +
59 generateExtractedDeclarations(6) +
60 loadPrototype("y") +
61 generateExtractedDeclarations(6));
62 }
63
64 public void testExtractingTwoClassPrototypeInDifferentBlocks() {
65 extract(
66 generatePrototypeDeclarations("x", 6) +
67 "if (foo()) {" +
68 generatePrototypeDeclarations("y", 6) +
69 "}",
70 loadPrototype("x") +
71 generateExtractedDeclarations(6) +
72 "if (foo()) {" +
73 loadPrototype("y") +
74 generateExtractedDeclarations(6) +
75 "}");
76 }
77
78 public void testNoMemberDeclarations() {
79 testSame(
80 "x.prototype = {}; x.prototype = {}; x.prototype = {};" +
81 "x.prototype = {}; x.prototype = {}; x.prototype = {};" +
82 "x.prototype = {}; x.prototype = {}; x.prototype = {};");
83 }
84
85 public void testExtractingPrototypeWithQName() {
86 extract(
87 generatePrototypeDeclarations("com.google.javascript.jscomp.x", 7),
88 loadPrototype("com.google.javascript.jscomp.x") +
89 generateExtractedDeclarations(7));
90 }
91
92 public void testInterweaved() {
93 testSame(
94 "x.prototype.a=1; y.prototype.a=1;" +
95 "x.prototype.b=1; y.prototype.b=1;" +
96 "x.prototype.c=1; y.prototype.c=1;" +
97 "x.prototype.d=1; y.prototype.d=1;" +
98 "x.prototype.e=1; y.prototype.e=1;" +
99 "x.prototype.f=1; y.prototype.f=1;");
100 }
101
102 public void testExtractingPrototypeWithNestedMembers() {
103 extract(
104 "x.prototype.y.a = 1;" +
105 "x.prototype.y.b = 1;" +
106 "x.prototype.y.c = 1;" +
107 "x.prototype.y.d = 1;" +
108 "x.prototype.y.e = 1;" +
109 "x.prototype.y.f = 1;" +
110 "x.prototype.y.g = 1;",
111 loadPrototype("x") +
112 TMP + ".y.a = 1;" +
113 TMP + ".y.b = 1;" +
114 TMP + ".y.c = 1;" +
115 TMP + ".y.d = 1;" +
116 TMP + ".y.e = 1;" +
117 TMP + ".y.f = 1;" +
118 TMP + ".y.g = 1;");
119 }
120
121 public void testWithDevirtualization() {
122 extract(
123 "x.prototype.a = 1;" +
124 "x.prototype.b = 1;" +
125 "function devirtualize1() { }" +
126 "x.prototype.c = 1;" +
127 "x.prototype.d = 1;" +
128 "x.prototype.e = 1;" +
129 "x.prototype.f = 1;" +
130 "x.prototype.g = 1;",
131
132 loadPrototype("x") +
133 TMP + ".a = 1;" +
134 TMP + ".b = 1;" +
135 "function devirtualize1() { }" +
136 TMP + ".c = 1;" +
137 TMP + ".d = 1;" +
138 TMP + ".e = 1;" +
139 TMP + ".f = 1;" +
140 TMP + ".g = 1;");
141
142 extract(
143 "x.prototype.a = 1;" +
144 "x.prototype.b = 1;" +
145 "function devirtualize1() { }" +
146 "x.prototype.c = 1;" +
147 "x.prototype.d = 1;" +
148 "function devirtualize2() { }" +
149 "x.prototype.e = 1;" +
150 "x.prototype.f = 1;" +
151 "function devirtualize3() { }" +
152 "x.prototype.g = 1;",
153
154 loadPrototype("x") +
155 TMP + ".a = 1;" +
156 TMP + ".b = 1;" +
157 "function devirtualize1() { }" +
158 TMP + ".c = 1;" +
159 TMP + ".d = 1;" +
160 "function devirtualize2() { }" +
161 TMP + ".e = 1;" +
162 TMP + ".f = 1;" +
163 "function devirtualize3() { }" +
164 TMP + ".g = 1;");
165 }
166
167 public void testAnonSimple() {
168 pattern = Pattern.USE_ANON_FUNCTION;
169
170 extract(
171 generatePrototypeDeclarations("x", 3),
172 generateExtractedDeclarations(3) +
173 loadPrototype("x"));
174
175 testSame(generatePrototypeDeclarations("x", 1));
176 testSame(generatePrototypeDeclarations("x", 2));
177
178 extract(
179 generatePrototypeDeclarations("x", 7),
180 generateExtractedDeclarations(7) +
181 loadPrototype("x"));
182
183 }
184
185 public void testAnonWithDevirtualization() {
186 pattern = Pattern.USE_ANON_FUNCTION;
187
188 extract(
189 "x.prototype.a = 1;" +
190 "x.prototype.b = 1;" +
191 "function devirtualize() { }" +
192 "x.prototype.c = 1;",
193
194 "(function(" + TMP + "){" +
195 TMP + ".a = 1;" +
196 TMP + ".b = 1;" +
197 TMP + ".c = 1;" +
198 loadPrototype("x") +
199 "function devirtualize() { }");
200
201 extract(
202 "x.prototype.a = 1;" +
203 "function devirtualize1() { }" +
204 "x.prototype.b = 1;" +
205 "function devirtualize2() { }" +
206 "x.prototype.c = 1;" +
207 "function devirtualize3() { }",
208
209 "(function(" + TMP + "){" +
210 TMP + ".a = 1;" +
211 TMP + ".b = 1;" +
212 TMP + ".c = 1;" +
213 loadPrototype("x") +
214 "function devirtualize1() { }" +
215 "function devirtualize2() { }" +
216 "function devirtualize3() { }");
217 }
218
219 public void testAnonWithSideFx() {
220 pattern = Pattern.USE_ANON_FUNCTION;
221 testSame(
222 "function foo() {};" +
223 "foo.prototype.a1 = 1;" +
224 "bar();;" +
225 "foo.prototype.a2 = 2;" +
226 "bar();;" +
227 "foo.prototype.a3 = 3;" +
228 "bar();;" +
229 "foo.prototype.a4 = 4;" +
230 "bar();;" +
231 "foo.prototype.a5 = 5;" +
232 "bar();;" +
233 "foo.prototype.a6 = 6;" +
234 "bar();;" +
235 "foo.prototype.a7 = 7;" +
236 "bar();");
237 }
238
239 public String loadPrototype(String qName) {
240 if (pattern == Pattern.USE_GLOBAL_TEMP) {
241 return TMP + " = " + qName + ".prototype;";
242 } else {
243 return "})(" + qName + ".prototype);";
244 }
245 }
246
247 public void extract(String src, String expected) {
248 if (pattern == Pattern.USE_GLOBAL_TEMP) {
249 test(src, "var " + TMP + ";" + expected);
250 } else {
251 test(src, expected);
252 }
253 }
254
255 public String generatePrototypeDeclarations(String className, int num) {
256 StringBuilder builder = new StringBuilder();
257 for (int i = 0; i < num; i++) {
258 char member = (char) ('a' + i);
259 builder.append(generatePrototypeDeclaration(
260 className, "" + member, "" + member));
261 }
262 return builder.toString();
263 }
264
265 public String generatePrototypeDeclaration(String className, String member,
266 String value) {
267 return className + ".prototype." + member + " = " + value + ";";
268 }
269
270 public String generateExtractedDeclarations(int num) {
271 StringBuilder builder = new StringBuilder();
272
273 if (pattern == Pattern.USE_ANON_FUNCTION) {
274 builder.append("(function(" + TMP + "){");
275 }
276
277 for (int i = 0; i < num; i++) {
278 char member = (char) ('a' + i);
279 builder.append(generateExtractedDeclaration("" + member, "" + member));
280 }
281 return builder.toString();
282 }
283
284 public String generateExtractedDeclaration(String member, String value) {
285 return TMP + "." + member + " = " + value + ";";
286 }
287 }