| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package com.google.javascript.jscomp; |
| 18 |
|
| 19 |
|
| 20 |
|
| 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 |
|
| 58 |
testSame(setupCode + "jQuery.extend()"); |
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
testSame(setupCode + "jQuery.extend({})"); |
| 63 |
|
| 64 |
|
| 65 |
test(setupCode + "jQuery.extend({a:'test'})", |
| 66 |
setupCode + "{jQuery.a = 'test';}"); |
| 67 |
|
| 68 |
|
| 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 |
|
| 75 |
test(setupCode + "jQuery.extend(obj2, {a:'test', b:'test2'});", |
| 76 |
setupCode + "{obj2=obj2||{}; obj2.a='test'; obj2.b='test2';}"); |
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
test(setupCode + "jQuery.extend(jQuery,{a:'test', b:'test2'});", |
| 81 |
setupCode + "{jQuery = jQuery || {}; jQuery.a = 'test';" + |
| 82 |
"jQuery.b = 'test2';}"); |
| 83 |
|
| 84 |
|
| 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 |
|
| 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 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 156 |
testSame(setupCode + "var a = ['a'];" + |
| 157 |
"jQuery.expandedEach(a, function(key,val){ jQuery[key]=val; })", |
| 158 |
INVALID_LIT_ERROR); |
| 159 |
|
| 160 |
|
| 161 |
testSame(setupCode + "var obj2={};" + |
| 162 |
"jQuery.expandedEach(['foo','bar'], function(i, name) {" + |
| 163 |
"obj2[ '[object ' + name + ']' ] = 'a';});", NAME_ERROR, true); |
| 164 |
|
| 165 |
|
| 166 |
testSame(setupCode + "var obj2={};" + |
| 167 |
"jQuery.expandedEach(['foo','bar'], function(i, name) {" + |
| 168 |
"obj2[i] = 1;});", USELESS_EACH_ERROR, false); |
| 169 |
} |
| 170 |
} |