Debian Codegraph @

test/com/google/javascript/jscomp/CollapseAnonymousFunctionsTest.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
20 /**
21 * Tests for {@link CollapseAnonymousFunctions}
22 *
23 */
24 public class CollapseAnonymousFunctionsTest extends CompilerTestCase {
25 public CollapseAnonymousFunctionsTest() {
26 this.enableNormalize();
27 }
28
29 @Override
30 protected CompilerPass getProcessor(Compiler compiler) {
31 return new CollapseAnonymousFunctions(compiler);
32 }
33
34 public void testGlobalScope() {
35 test("var f = function(){}", "function f(){}");
36 }
37
38 public void testLocalScope1() {
39 test("function f(){ var x = function(){}; x() }",
40 "function f(){ function x(){} x() }");
41 }
42
43 public void testLocalScope2() {
44 test("function f(){ var x = function(){}; return x }",
45 "function f(){ function x(){} return x }");
46 }
47
48 public void testVarNotImmediatelyBelowScriptOrBlock1() {
49 testSame("if (x) var f = function(){}");
50 }
51
52 public void testVarNotImmediatelyBelowScriptOrBlock2() {
53 testSame("var x = 1;" +
54 "if (x == 1) {" +
55 " var f = function () { alert('b')}" +
56 "} else {" +
57 " f = function() { alert('c')}" +
58 "}" +
59 "f();");
60 }
61
62 public void testVarNotImmediatelyBelowScriptOrBlock3() {
63 testSame("var x = 1; if (x) {var f = function(){return x}; f(); x--;}");
64 }
65
66 public void testMultipleVar() {
67 test("var f = function(){}; var g = f", "function f(){} var g = f");
68 }
69
70 public void testMultipleVar2() {
71 test("var f = function(){}; var g = f; var h = function(){}",
72 "function f(){}var g = f;function h(){}");
73 }
74
75 public void testBothScopes() {
76 test("var x = function() { var y = function(){} }",
77 "function x() { function y(){} }");
78 }
79
80 public void testLocalScopeOnly1() {
81 test("if (x) var f = function(){ var g = function(){} }",
82 "if (x) var f = function(){ function g(){} }");
83 }
84
85 public void testLocalScopeOnly2() {
86 test("if (x) var f = function(){ var g = function(){} };",
87 "if (x) var f = function(){ function g(){} }");
88 }
89
90 public void testReturn() {
91 test("var f = function(x){return 2*x}; var g = f(2);",
92 "function f(x){return 2*x} var g = f(2)");
93 }
94
95 public void testAlert() {
96 test("var x = 1; var f = function(){alert(x)};",
97 "var x = 1; function f(){alert(x)}");
98 }
99
100 public void testRecursiveInternal1() {
101 testSame("var f = function foo() { foo() }");
102 }
103
104 public void testRecursiveInternal2() {
105 testSame("var f = function foo() { function g(){foo()} g() }");
106 }
107
108 public void testRecursiveExternal1() {
109 test("var f = function foo() { f() }",
110 "function f() { f() }");
111 }
112
113 public void testRecursiveExternal2() {
114 test("var f = function foo() { function g(){f()} g() }",
115 "function f() { function g(){f()} g() }");
116 }
117
118 public void testConstantFunction1() {
119 test("var FOO = function(){};FOO()",
120 "function FOO(){}FOO()");
121 }
122
123 public void testInnerFunction1() {
124 test(
125 "function f() { " +
126 " var x = 3; var y = function() { return 4; }; return x + y();" +
127 "}",
128 "function f() { " +
129 " function y() { return 4; } var x = 3; return x + y();" +
130 "}");
131 }
132 }