Debian Codegraph @

test/com/google/javascript/jscomp/NameAnonymousFunctionsMappedTest.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.common.collect.ImmutableMap;
20
21 /**
22 * Test cases for {@link NameAnonymousFunctionsMapped}.
23 *
24 */
25 public class NameAnonymousFunctionsMappedTest extends CompilerTestCase {
26 private static final String EXTERNS = "var document;";
27
28 private NameAnonymousFunctionsMapped pass;
29 private VariableMap previous;
30
31 public NameAnonymousFunctionsMappedTest() {
32 super(EXTERNS);
33 }
34
35 @Override
36 protected int getNumRepetitions() {
37 return 1;
38 }
39
40 @Override
41 protected void setUp() throws Exception {
42 super.setUp();
43 previous = null;
44 }
45
46 @Override
47 public CompilerPass getProcessor(Compiler compiler) {
48 return pass = new NameAnonymousFunctionsMapped(compiler, previous);
49 }
50
51 private void assertMapping(String... pairs) {
52 VariableMap functionMap = pass.getFunctionMap();
53 assertTrue(pairs.length % 2 == 0);
54 for (int i = 0; i < pairs.length; i += 2) {
55 String s = functionMap.lookupSourceName(pairs[i]);
56 assertEquals(pairs[i + 1], s);
57 }
58 assertEquals(pairs.length / 2,
59 functionMap.getNewNameToOriginalNameMap().size());
60 }
61
62 public void testSimpleVarAssignment1() {
63 test("var a = function() { return 1; }",
64 "var a = function $() { return 1; }");
65 assertMapping("$", "a");
66 }
67
68 public void testSimpleVarAssignment2() {
69 previous = VariableMap.fromMap(ImmutableMap.<String,String>of(
70 "a", "previous"));
71
72 test("var a = function() { return 1; }",
73 "var a = function previous() { return 1; }");
74
75 assertMapping("previous", "a");
76 }
77
78 public void testSimpleVarAssignment3() {
79 previous = VariableMap.fromMap(ImmutableMap.<String,String>of(
80 "unused", "$"));
81
82 test("var fn = function() { return 1; }",
83 "var fn = function $a() { return 1; }");
84
85 assertMapping("$a", "fn");
86 }
87
88 public void testAssignmentToProperty() {
89 test("var a = {}; a.b = function() { return 1; }",
90 "var a = {}; a.b = function $() { return 1; }");
91 assertMapping("$", "a.b");
92 }
93
94 public void testAssignmentToPrototype() {
95 test("function a() {} a.prototype.b = function() { return 1; };",
96 "function a() {} " +
97 "a.prototype.b = function $() { return 1; };");
98 assertMapping("$", "a.prototype.b");
99 }
100
101 public void testAssignmentToPrototype2() {
102 test("var a = {}; " +
103 "a.b = function() {}; " +
104 "a.b.prototype.c = function() { return 1; };",
105 "var a = {}; " +
106 "a.b = function $() {}; " +
107 "a.b.prototype.c = function $a() { return 1; };");
108 assertMapping("$", "a.b", "$a", "a.b.prototype.c");
109 }
110
111 public void testAssignmentToPrototype3() {
112 test("function a() {} a.prototype['XXX'] = function() { return 1; };",
113 "function a() {} " +
114 "a.prototype['XXX'] = function $() { return 1; };");
115 assertMapping("$", "a.prototype[\"XXX\"]");
116 test("function a() {} a.prototype['\\n'] = function() { return 1; };",
117 "function a() {} " +
118 "a.prototype['\\n'] = function $() { return 1; };");
119 assertMapping("$", "a.prototype[\"\\n\"]");
120 }
121
122 public void testAssignmentToPrototype4() {
123 test("var Y = 1; function a() {} " +
124 "a.prototype[Y] = function() { return 1; };",
125 "var Y = 1; function a() {} " +
126 "a.prototype[Y] = function $() { return 1; };");
127 assertMapping("$", "a.prototype[Y]");
128 }
129
130 public void testAssignmentToPrototype5() {
131 test("function a() {} a['prototype'].b = function() { return 1; };",
132 "function a() {} " +
133 "a['prototype'].b = function $() { return 1; };");
134 assertMapping("$", "a[\"prototype\"].b");
135 }
136
137
138 public void testPrototypeInitializer() {
139 test("function a(){} a.prototype = {b: function() { return 1; }};",
140 "function a(){} " +
141 "a.prototype = {b: function $() { return 1; }};");
142 assertMapping("$", "a.prototype.b");
143 }
144
145 public void testAssignmentToPropertyOfCallReturnValue() {
146 test("document.getElementById('x').onClick = function() {};",
147 "document.getElementById('x').onClick = " +
148 "function $() {};");
149 assertMapping("$", "document.getElementById(\"x\").onClick");
150 }
151
152 public void testAssignmentToPropertyOfArrayElement() {
153 test("var a = {}; a.b = [{}]; a.b[0].c = function() {};",
154 "var a = {}; a.b = [{}]; a.b[0].c = function $() {};");
155 assertMapping("$", "a.b[0].c");
156 test("var a = {b: {'c': {}}}; a.b['c'].d = function() {};",
157 "var a = {b: {'c': {}}}; a.b['c'].d = function $() {};");
158 assertMapping("$", "a.b[\"c\"].d");
159 test("var a = {b: {'c': {}}}; a.b[x()].d = function() {};",
160 "var a = {b: {'c': {}}}; a.b[x()].d = function $() {};");
161 assertMapping("$", "a.b[x()].d");
162 }
163
164 public void testAssignmentToGetElem() {
165 test("function f() { win['x' + this.id] = function(a){}; }",
166 "function f() { win['x' + this.id] = function $(a){}; }");
167
168 // TODO - could probably do a better job encoding these
169 assertMapping("$", "win[\"x\"+this.id]");
170 }
171
172 public void testGetElemWithDashes() {
173 test("var foo = {}; foo['-'] = function() {};",
174 "var foo = {}; foo['-'] = function $() {};");
175 assertMapping("$", "foo[\"-\"]");
176 }
177
178 public void testDuplicateNames() {
179 test("var a = function() { return 1; };a = function() { return 2; }",
180 "var a = function $() { return 1; };a = function $() { return 2; }");
181 assertMapping("$", "a");
182 }
183 }