This repository has been archived on 2023-06-18. You can view files and clone it, but cannot push or open issues or pull requests.
ima02/resources/defects4j-checkout-closure-1f/test/com/google/javascript/jscomp/FunctionNamesTest.java
github-classroom[bot] e42e547e48
Initial commit
2023-04-25 11:33:41 +00:00

94 lines
3.1 KiB
Java

/*
* Copyright 2008 The Closure Compiler Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.javascript.jscomp;
import com.google.common.collect.Maps;
import com.google.javascript.rhino.Node;
import java.util.Map;
/**
* Tests for {@link FunctionNames}
*
*/
public class FunctionNamesTest extends CompilerTestCase {
private FunctionNames functionNames;
public FunctionNamesTest() {
this.functionNames = null;
}
@Override
protected CompilerPass getProcessor(Compiler compiler) {
functionNames = new FunctionNames(compiler);
return functionNames;
}
public void testFunctionsNamesAndIds() {
final String jsSource =
"goog.widget = function(str) {\n" +
" this.member_fn = function() {};\n" +
" local_fn = function() {};\n" +
" (function(a){})(1);\n" +
"}\n" +
"function foo() {\n" +
" function bar() {}\n" +
"}\n" +
"literal = {f1 : function(){}, f2 : function(){}};\n" +
"goog.array.map(arr, function named(){});\n" +
"goog.array.map(arr, function(){});\n" +
"named_twice = function quax(){};\n" +
"recliteral = {l1 : {l2 : function(){}}};\n" +
"namedliteral = {n1 : function litnamed(){}};\n" +
"namedrecliteral = {n1 : {n2 : function reclitnamed(){}}};\n" +
"numliteral = {1 : function(){}};\n" +
"recnumliteral = {1 : {a : function(){}}};\n";
testSame(jsSource);
final Map<Integer, String> idNameMap = Maps.newLinkedHashMap();
int count = 0;
for (Node f : functionNames.getFunctionNodeList()) {
int id = functionNames.getFunctionId(f);
String name = functionNames.getFunctionName(f);
idNameMap.put(id, name);
count++;
}
assertEquals("Unexpected number of functions", 16, count);
final Map<Integer, String> expectedMap = Maps.newLinkedHashMap();
expectedMap.put(0, "goog.widget.member_fn");
expectedMap.put(1, "goog.widget::local_fn");
expectedMap.put(2, "goog.widget::<anonymous>");
expectedMap.put(3, "goog.widget");
expectedMap.put(4, "foo::bar");
expectedMap.put(5, "foo");
expectedMap.put(6, "literal.f1");
expectedMap.put(7, "literal.f2");
expectedMap.put(8, "named");
expectedMap.put(9, "<anonymous>");
expectedMap.put(10, "quax");
expectedMap.put(11, "recliteral.l1.l2");
expectedMap.put(12, "litnamed");
expectedMap.put(13, "reclitnamed");
expectedMap.put(14, "numliteral.__2");
expectedMap.put(15, "recnumliteral.__3.a");
assertEquals("Function id/name mismatch",
expectedMap, idNameMap);
}
}