| 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 |
|
| 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 |
|
| 118 |
|
| 119 |
|
| 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 |
} |