| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
package com.google.javascript.jscomp; |
| 17 |
|
| 18 |
import com.google.common.collect.Maps; |
| 19 |
import com.google.javascript.rhino.Node; |
| 20 |
|
| 21 |
import java.util.Map; |
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
public class FunctionNamesTest extends CompilerTestCase { |
| 28 |
private FunctionNames functionNames; |
| 29 |
|
| 30 |
public FunctionNamesTest() { |
| 31 |
this.functionNames = null; |
| 32 |
} |
| 33 |
|
| 34 |
@Override |
| 35 |
protected CompilerPass getProcessor(Compiler compiler) { |
| 36 |
functionNames = new FunctionNames(compiler); |
| 37 |
return functionNames; |
| 38 |
} |
| 39 |
|
| 40 |
public void testFunctionsNamesAndIds() { |
| 41 |
final String jsSource = |
| 42 |
"goog.widget = function(str) {\n" + |
| 43 |
" this.member_fn = function() {};\n" + |
| 44 |
" local_fn = function() {};\n" + |
| 45 |
" (function(a){})(1);\n" + |
| 46 |
"}\n" + |
| 47 |
"function foo() {\n" + |
| 48 |
" function bar() {}\n" + |
| 49 |
"}\n" + |
| 50 |
"literal = {f1 : function(){}, f2 : function(){}};\n" + |
| 51 |
"goog.array.map(arr, function named(){});\n" + |
| 52 |
"goog.array.map(arr, function(){});\n" + |
| 53 |
"named_twice = function quax(){};\n" + |
| 54 |
"recliteral = {l1 : {l2 : function(){}}};\n" + |
| 55 |
"namedliteral = {n1 : function litnamed(){}};\n" + |
| 56 |
"namedrecliteral = {n1 : {n2 : function reclitnamed(){}}};\n" + |
| 57 |
"numliteral = {1 : function(){}};\n" + |
| 58 |
"recnumliteral = {1 : {a : function(){}}};\n"; |
| 59 |
|
| 60 |
testSame(jsSource); |
| 61 |
|
| 62 |
final Map<Integer, String> idNameMap = Maps.newLinkedHashMap(); |
| 63 |
int count = 0; |
| 64 |
for (Node f : functionNames.getFunctionNodeList()) { |
| 65 |
int id = functionNames.getFunctionId(f); |
| 66 |
String name = functionNames.getFunctionName(f); |
| 67 |
idNameMap.put(id, name); |
| 68 |
count++; |
| 69 |
} |
| 70 |
|
| 71 |
assertEquals("Unexpected number of functions", 16, count); |
| 72 |
|
| 73 |
final Map<Integer, String> expectedMap = Maps.newLinkedHashMap(); |
| 74 |
|
| 75 |
expectedMap.put(0, "goog.widget.member_fn"); |
| 76 |
expectedMap.put(1, "goog.widget::local_fn"); |
| 77 |
expectedMap.put(2, "goog.widget::<anonymous>"); |
| 78 |
expectedMap.put(3, "goog.widget"); |
| 79 |
expectedMap.put(4, "foo::bar"); |
| 80 |
expectedMap.put(5, "foo"); |
| 81 |
expectedMap.put(6, "literal.f1"); |
| 82 |
expectedMap.put(7, "literal.f2"); |
| 83 |
expectedMap.put(8, "named"); |
| 84 |
expectedMap.put(9, "<anonymous>"); |
| 85 |
expectedMap.put(10, "quax"); |
| 86 |
expectedMap.put(11, "recliteral.l1.l2"); |
| 87 |
expectedMap.put(12, "litnamed"); |
| 88 |
expectedMap.put(13, "reclitnamed"); |
| 89 |
expectedMap.put(14, "numliteral.__2"); |
| 90 |
expectedMap.put(15, "recnumliteral.__3.a"); |
| 91 |
assertEquals("Function id/name mismatch", |
| 92 |
expectedMap, idNameMap); |
| 93 |
} |
| 94 |
} |