Debian Codegraph @

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

raw symbols SCIP
1 /*
2 * Copyright 2011 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 * Tests for {@link ExpandJqueryAliases}
21 */
22 public class ExpandJqueryAliasesTest extends CompilerTestCase {
23 private JqueryCodingConvention conv = new JqueryCodingConvention();
24
25 final DiagnosticType NAME_ERROR =
26 ExpandJqueryAliases.JQUERY_UNABLE_TO_EXPAND_INVALID_NAME_ERROR;
27
28 final DiagnosticType INVALID_LIT_ERROR =
29 ExpandJqueryAliases.JQUERY_UNABLE_TO_EXPAND_INVALID_LIT_ERROR;
30
31 final DiagnosticType USELESS_EACH_ERROR =
32 ExpandJqueryAliases.JQUERY_USELESS_EACH_EXPANSION;
33
34 public ExpandJqueryAliasesTest() {}
35
36 @Override
37 protected CompilerPass getProcessor(Compiler compiler) {
38 compiler.options.setCodingConvention(conv);
39 return new ExpandJqueryAliases(compiler);
40 }
41
42 public void testJqueryFnAliasExpansion() {
43 String setupCode = "var jQuery={};jQuery.fn=jQuery.prototype;";
44
45 testSame(setupCode);
46
47 test(setupCode + "jQuery.fn.foo='bar';",
48 setupCode + "jQuery.prototype.foo='bar';");
49
50 test(setupCode + "jQuerySub.fn.foo='bar';",
51 setupCode + "jQuerySub.prototype.foo='bar';");
52 }
53
54 public void testJqueryExtendExpansion() {
55 String setupCode = "var jQuery={},obj2={};";
56
57 // test for extend call that should not be expanded - no arguments
58 testSame(setupCode + "jQuery.extend()");
59
60 // test for extend call that should not be expanded - empty argument
61 // this statement has no effect in actual code
62 testSame(setupCode + "jQuery.extend({})");
63
64 // test single argument call - should assign to the jQuery namespace
65 test(setupCode + "jQuery.extend({a:'test'})",
66 setupCode + "{jQuery.a = 'test';}");
67
68 // test expansion when extending the jQuery prototype
69 test(setupCode + "jQuery.fn=jQuery.prototype;" +
70 "jQuery.fn.extend({a:'test', b:'test2'});",
71 setupCode + "jQuery.fn=jQuery.prototype;" +
72 "{jQuery.prototype.a = 'test'; jQuery.prototype.b = 'test2';}");
73
74 // Expand the extension of obj2
75 test(setupCode + "jQuery.extend(obj2, {a:'test', b:'test2'});",
76 setupCode + "{obj2=obj2||{}; obj2.a='test'; obj2.b='test2';}");
77
78 // Expand the jQuery namespace - 2 argument call
79 // Must ensure that the first argument is defined
80 test(setupCode + "jQuery.extend(jQuery,{a:'test', b:'test2'});",
81 setupCode + "{jQuery = jQuery || {}; jQuery.a = 'test';" +
82 "jQuery.b = 'test2';}");
83
84 // Test extend call where first argument includes a method call
85 testSame(setupCode+"obj2.meth=function() { return { a:{} }; };" +
86 "jQuery.extend(obj2.meth().a, {a: 'test'});");
87 }
88
89 public void testJqueryExpandedEachExpansion() {
90 String setupCode = "var jQuery={};" +
91 "jQuery.expandedEach=function(vals, callback){};";
92
93 testSame(setupCode);
94
95 // Test expansion with object literal
96 test(setupCode + "jQuery.expandedEach({'a': 1, 'b': 2, 'c': 8}," +
97 "function(key, val) { var a = key; jQuery[key] = val; });",
98 setupCode + "(function(){ var a = 'a'; jQuery.a = 1 })();" +
99 "(function(){ var a = 'b'; jQuery.b = 2 })();" +
100 "(function(){ var a = 'c'; jQuery.c = 8 })();");
101
102 // Test expansion with array literal
103 // For array literals, the key parameter will be the element index number
104 // and the value parameter will be the string literal. In this case, the
105 // string literal value should become a property name.
106 test(setupCode + "jQuery.expandedEach(['a', 'b', 'c']," +
107 "function(key, val){ jQuery[val] = key; });",
108 setupCode + "(function(){ jQuery.a = 0; })();" +
109 "(function(){ jQuery.b = 1; })();" +
110 "(function(){ jQuery.c = 2 })();");
111
112 // Test expansion with object literal using 'this' keyword
113 test(setupCode + "jQuery.expandedEach({'a': 1, 'b': 2, 'c': 8}," +
114 "function(key, val) { var a = key; jQuery[key] = this; });",
115 setupCode + "(function(){ var a = 'a'; jQuery.a = 1 })();" +
116 "(function(){ var a = 'b'; jQuery.b = 2 })();" +
117 "(function(){ var a = 'c'; jQuery.c = 8 })();");
118
119 // Test expansion with array literal using 'this' keyword
120 test(setupCode + "jQuery.expandedEach(['a', 'b', 'c']," +
121 "function(key, val){ jQuery[this] = key; });",
122 setupCode + "(function(){ jQuery.a = 0; })();" +
123 "(function(){ jQuery.b = 1; })();" +
124 "(function(){ jQuery.c = 2 })();");
125
126 // test nested function using argument name to shadow callback name
127 test(setupCode + "jQuery.expandedEach(['a'], function(key,val) {" +
128 "jQuery[val] = key; (function(key) { jQuery[key] = 1;})('test'); })",
129 setupCode + "(function(){ jQuery.a = 0;" +
130 "(function(key){ jQuery[key] = 1})('test') })()");
131
132 // test nested function using var name to shadow callback name
133 test(setupCode + "jQuery.expandedEach(['a'], function(key,val) {" +
134 "jQuery[val] = key; (function(key) { var val = 2;" +
135 "jQuery[key] = val;})('test');})",
136 setupCode + "(function(){" +
137 "jQuery.a=0;" +
138 "(function(key){var val = 2; jQuery[key] = val;})('test')})()");
139
140 // test nested function using function name to shadow callback name
141 test(setupCode + "jQuery.expandedEach(['a'], function(key,val) {" +
142 "jQuery[val] = key; (function(key1) {" +
143 "function key() {}; key();" +
144 "})('test');})",
145 setupCode + "(function(){" +
146 "jQuery.a=0;(function(key1) {" +
147 "function key() {}; key(); })('test')})()");
148
149 // test using return val
150 test(setupCode + "alert(jQuery.expandedEach(['a']," +
151 "function(key,val) { jQuery[val] = key;})[0])",
152 setupCode + "alert((function(){" +
153 "(function(){ jQuery.a = 0;})(); return ['a']})()[0]);");
154
155 // Loop object is a variable. Test that warning is raised.
156 testSame(setupCode + "var a = ['a'];" +
157 "jQuery.expandedEach(a, function(key,val){ jQuery[key]=val; })",
158 INVALID_LIT_ERROR);
159
160 // Invalid property name. Test that warning is raised.
161 testSame(setupCode + "var obj2={};" +
162 "jQuery.expandedEach(['foo','bar'], function(i, name) {" +
163 "obj2[ '[object ' + name + ']' ] = 'a';});", NAME_ERROR, true);
164
165 // Useless expansion (key not used). Test that warning is raised.
166 testSame(setupCode + "var obj2={};" +
167 "jQuery.expandedEach(['foo','bar'], function(i, name) {" +
168 "obj2[i] = 1;});", USELESS_EACH_ERROR, false);
169 }
170 }