Debian Codegraph @

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

raw symbols SCIP
1 /*
2 * Copyright 2006 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 * Unit test for {@link NameAnonymousFunctionsTest}.
21 *
22 */
23 public class NameAnonymousFunctionsTest extends CompilerTestCase {
24
25 private static final String EXTERNS = "var document;";
26
27 public NameAnonymousFunctionsTest() {
28 super(EXTERNS);
29 }
30
31 @Override public CompilerPass getProcessor(Compiler compiler) {
32 return new NameAnonymousFunctions(compiler);
33 }
34
35 public void testSimpleVarAssignment() {
36 test("var a = function() { return 1; }",
37 "var a = function $a$() { return 1; }");
38 }
39
40 public void testAssignmentToProperty() {
41 test("var a = {}; a.b = function() { return 1; }",
42 "var a = {}; a.b = function $a$b$() { return 1; }");
43 }
44
45 public void testAssignmentToPrototype() {
46 test("function a() {} a.prototype.b = function() { return 1; };",
47 "function a() {} " +
48 "a.prototype.b = function $a$$b$() { return 1; };");
49 }
50
51 public void testAssignmentToPrototype2() {
52 test("var a = {}; " +
53 "a.b = function() {}; " +
54 "a.b.prototype.c = function() { return 1; };",
55 "var a = {}; " +
56 "a.b = function $a$b$() {}; " +
57 "a.b.prototype.c = function $a$b$$c$() { return 1; };");
58 }
59
60 public void testAssignmentToPrototype3() {
61 test("function a() {} a.prototype['b'] = function() { return 1; };",
62 "function a() {} " +
63 "a.prototype['b'] = function $a$$b$() { return 1; };");
64 }
65
66 public void testAssignmentToPrototype4() {
67 test("function a() {} a['prototype']['b'] = function() { return 1; };",
68 "function a() {} " +
69 "a['prototype']['b'] = function $a$$b$() { return 1; };");
70 }
71
72 public void testPrototypeInitializer() {
73 test("function a(){} a.prototype = {b: function() { return 1; }};",
74 "function a(){} " +
75 "a.prototype = {b: function $a$$b$() { return 1; }};");
76 }
77
78 public void testMultiplePrototypeInitializer() {
79 test("function a(){} a.prototype = {b: function() { return 1; }, " +
80 "c: function() { return 2; }};",
81 "function a(){} " +
82 "a.prototype = {b: function $a$$b$() { return 1; }," +
83 "c: function $a$$c$() { return 2; }};");
84 }
85
86 public void testRecursiveObjectLiteral() {
87 test("function a(){} a.prototype = {b: {c: function() { return 1; }}}",
88 "function a(){}a.prototype={b:{c:function $a$$b$c$(){return 1}}}");
89 }
90
91 public void testAssignmentToPropertyOfCallReturnValue() {
92 test("document.getElementById('x').onClick = function() {};",
93 "document.getElementById('x').onClick = " +
94 "function $document$getElementById$onClick$() {};");
95 }
96
97 public void testAssignmentToPropertyOfArrayElement() {
98 test("var a = {}; a.b = [{}]; a.b[0].c = function() {};",
99 "var a = {}; a.b = [{}]; a.b[0].c = function $a$b$0$c$() {};");
100 test("var a = {b: {'c': {}}}; a.b['c'].d = function() {};",
101 "var a = {b: {'c': {}}}; a.b['c'].d = function $a$b$c$d$() {};");
102 test("var a = {b: {'c': {}}}; a.b[x()].d = function() {};",
103 "var a = {b: {'c': {}}}; a.b[x()].d = function $a$b$x$d$() {};");
104 }
105
106 public void testAssignmentToGetElem() {
107 test("function f() {win['x' + this.id] = function(a){};}",
108 "function f() {win['x' + this.id] = function $win$x$this$id$(a){};}");
109 }
110
111 public void testGetElemWithDashes() {
112 test("var foo = {}; foo['-'] = function() {};",
113 "var foo = {}; foo['-'] = function $foo$__0$() {};");
114 }
115
116 public void testWhatCausedIeToFail() {
117 // If the function was given the name main, for some reason IE failed to
118 // handle this case properly. That's why we give it the name $main$. FF
119 // handled the case fine.
120 test("var main;" +
121 "(function() {" +
122 " main = function() {" +
123 " return 5;" +
124 " };" +
125 "})();" +
126 "" +
127 "main();",
128 "var main;(function(){main=function $main$(){return 5}})();main()");
129 }
130 }