Debian Codegraph @

test/com/google/javascript/jscomp/OptimizeReturnsTest.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 import com.google.common.base.Joiner;
20
21 /**
22 * Tests OptimizeReturns
23 * @author [email protected] (John Lenz)
24 */
25 public class OptimizeReturnsTest extends CompilerTestCase {
26
27 @Override
28 protected CompilerPass getProcessor(Compiler compiler) {
29 return new OptimizeReturns(compiler);
30 }
31
32 private static final String EXTERNAL_SYMBOLS =
33 "var extern;extern.externalMethod";
34
35 public OptimizeReturnsTest() {
36 super(EXTERNAL_SYMBOLS);
37 }
38
39 @Override
40 protected int getNumRepetitions() {
41 // run pass once.
42 return 1;
43 }
44
45 @Override
46 protected void setUp() throws Exception {
47 super.setUp();
48 super.enableLineNumberCheck(true);
49 disableTypeCheck();
50 }
51
52 /**
53 * Combine source strings using '\n' as the separator.
54 */
55 private static String newlineJoin(String ... parts) {
56 return Joiner.on("\n").join(parts);
57 }
58
59 public void testNoRewriteUsedResult1() throws Exception {
60 String source = newlineJoin(
61 "function a(){return 1}",
62 "var x = a()");
63 testSame(source);
64 }
65
66 public void testNoRewriteUsedResult2() throws Exception {
67 String source = newlineJoin(
68 "var a = function(){return 1}",
69 "a(); var b = a()");
70 testSame(source);
71 }
72
73 public void testRewriteUnusedResult1() throws Exception {
74 String source = newlineJoin(
75 "function a(){return 1}",
76 "a()");
77 String expected = newlineJoin(
78 "function a(){return}",
79 "a()");
80 test(source, expected);
81 }
82
83 public void testRewriteUnusedResult2() throws Exception {
84 String source = newlineJoin(
85 "var a; a = function(){return 1}",
86 "a()");
87 String expected = newlineJoin(
88 "var a; a = function(){return}",
89 "a()");
90 test(source, expected);
91 }
92
93 public void testRewriteUnusedResult3() throws Exception {
94 String source = newlineJoin(
95 "var a = function(){return 1}",
96 "a()");
97 String expected = newlineJoin(
98 "var a = function(){return}",
99 "a()");
100 test(source, expected);
101 }
102
103 public void testRewriteUnusedResult4a() throws Exception {
104 String source = newlineJoin(
105 "var a = function(){return a()}",
106 "a()");
107 testSame(source);
108 }
109
110 public void testRewriteUnusedResult4b() throws Exception {
111 String source = newlineJoin(
112 "var a = function b(){return b()}",
113 "a()");
114 testSame(source);
115 }
116
117 public void testRewriteUnusedResult4c() throws Exception {
118 String source = newlineJoin(
119 "function a(){return a()}",
120 "a()");
121 testSame(source);
122 }
123
124 public void testRewriteUnusedResult5() throws Exception {
125 String source = newlineJoin(
126 "function a(){}",
127 "a.prototype.foo = function(args) {return args};",
128 "var o = new a;",
129 "o.foo()");
130 String expected = newlineJoin(
131 "function a(){}",
132 "a.prototype.foo = function(args) {return};",
133 "var o = new a;",
134 "o.foo()");
135 test(source, expected);
136 }
137
138 public void testRewriteUnusedResult6() throws Exception {
139 String source = newlineJoin(
140 "function a(){return (g = 1)}",
141 "a()");
142 String expected = newlineJoin(
143 "function a(){g = 1;return}",
144 "a()");
145 test(source, expected);
146 }
147
148 public void testRewriteUnusedResult7a() throws Exception {
149 String source = newlineJoin(
150 "function a() { return 1 }",
151 "function b() { return a() }",
152 "function c() { return b() }",
153 "c();");
154
155 String expected = newlineJoin(
156 "function a() { return 1 }",
157 "function b() { return a() }",
158 "function c() { b(); return }",
159 "c();");
160 test(source, expected);
161 }
162
163 public void testRewriteUnusedResult7b() throws Exception {
164 String source = newlineJoin(
165 "c();",
166 "function c() { return b() }",
167 "function b() { return a() }",
168 "function a() { return 1 }");
169
170 // Iteration 1.
171 String expected = newlineJoin(
172 "c();",
173 "function c() { b(); return }",
174 "function b() { return a() }",
175 "function a() { return 1 }");
176 test(source, expected);
177
178 // Iteration 2.
179 source = expected;
180 expected = newlineJoin(
181 "c();",
182 "function c() { b(); return }",
183 "function b() { a(); return }",
184 "function a() { return 1 }");
185 test(source, expected);
186
187 // Iteration 3.
188 source = expected;
189 expected = newlineJoin(
190 "c();",
191 "function c() { b(); return }",
192 "function b() { a(); return }",
193 "function a() { return }");
194 test(source, expected);
195 }
196
197 public void testRewriteUnusedResult8() throws Exception {
198 String source = newlineJoin(
199 "function a() { return c() }",
200 "function b() { return a() }",
201 "function c() { return b() }",
202 "c();");
203 testSame(source);
204 }
205
206 public void testNoRewriteObjLit1() throws Exception {
207 String source = newlineJoin(
208 "var a = {b:function(){return 1;}}",
209 "for(c in a) (a[c])();",
210 "a.b()");
211 testSame(source);
212 }
213
214 public void testNoRewriteObjLit2() throws Exception {
215 String source = newlineJoin(
216 "var a = {b:function fn(){return 1;}}",
217 "for(c in a) (a[c])();",
218 "a.b()");
219 testSame(source);
220 }
221
222 public void testNoRewriteArrLit() throws Exception {
223 String source = newlineJoin(
224 "var a = [function(){return 1;}]",
225 "(a[0])();");
226 testSame(source);
227 }
228
229 public void testPrototypeMethod1() throws Exception {
230 String source = newlineJoin(
231 "function c(){}",
232 "c.prototype.a = function(){return 1}",
233 "var x = new c;",
234 "x.a()");
235 String result = newlineJoin(
236 "function c(){}",
237 "c.prototype.a = function(){return}",
238 "var x = new c;",
239 "x.a()");
240 test(source, result);
241 }
242
243 public void testPrototypeMethod2() throws Exception {
244 String source = newlineJoin(
245 "function c(){}",
246 "c.prototype.a = function(){return 1}",
247 "goog.reflect.object({a: 'v'})",
248 "var x = new c;",
249 "x.a()");
250 testSame(source);
251 }
252
253 public void testPrototypeMethod3() throws Exception {
254 String source = newlineJoin(
255 "function c(){}",
256 "c.prototype.a = function(){return 1}",
257 "var x = new c;",
258 "for(var key in goog.reflect.object({a: 'v'})){ x[key](); }",
259 "x.a()");
260 testSame(source);
261 }
262
263 public void testPrototypeMethod4() throws Exception {
264 String source = newlineJoin(
265 "function c(){}",
266 "c.prototype.a = function(){return 1}",
267 "var x = new c;",
268 "for(var key in goog.reflect.object({a: 'v'})){ x[key](); }");
269 testSame(source);
270 }
271
272 public void testCallOrApply() throws Exception {
273 // TODO(johnlenz): Add support for .call and .apply
274 testSame("function a() {return 1}; a.call(new foo);");
275
276 testSame("function a() {return 1}; a.apply(new foo);");
277 }
278
279 public void testRewriteUseSiteRemoval() throws Exception {
280 String source = newlineJoin(
281 "function a() { return {\"_id\" : 1} }",
282 "a();");
283 String expected = newlineJoin(
284 "function a() { return }",
285 "a();");
286 test(source, expected);
287 }
288 }