Skip to content

Commit

Permalink
Test cleanup: Remove unused overloads that take a knownConstants field.
Browse files Browse the repository at this point in the history
Individual tests can still pass a specific set of known constants by setting knownConstants before calling a helper method.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=177618717
  • Loading branch information
tbreisacher authored and blickly committed Dec 4, 2017
1 parent 407123e commit a500208
Showing 1 changed file with 28 additions and 72 deletions.
100 changes: 28 additions & 72 deletions test/com/google/javascript/jscomp/ExpressionDecomposerTest.java
Expand Up @@ -26,7 +26,6 @@
import com.google.javascript.jscomp.ExpressionDecomposer.DecompositionType; import com.google.javascript.jscomp.ExpressionDecomposer.DecompositionType;
import com.google.javascript.rhino.Node; import com.google.javascript.rhino.Node;
import com.google.javascript.rhino.Token; import com.google.javascript.rhino.Token;
import java.util.Arrays;
import java.util.HashSet; import java.util.HashSet;
import java.util.Set; import java.util.Set;
import java.util.function.Function; import java.util.function.Function;
Expand All @@ -41,10 +40,12 @@
// Note: functions "foo" and "goo" are external functions in the helper. // Note: functions "foo" and "goo" are external functions in the helper.
public final class ExpressionDecomposerTest extends TestCase { public final class ExpressionDecomposerTest extends TestCase {
private boolean allowMethodCallDecomposing; private boolean allowMethodCallDecomposing;
private final Set<String> knownConstants = new HashSet<>();


@Override @Override
public void setUp() { public void setUp() {
allowMethodCallDecomposing = false; allowMethodCallDecomposing = false;
knownConstants.clear();
} }


public void testCanExposeExpression1() { public void testCanExposeExpression1() {
Expand Down Expand Up @@ -740,17 +741,9 @@ private Node findExpressionRoot(String js, String name) {
return ExpressionDecomposer.findExpressionRoot(call); return ExpressionDecomposer.findExpressionRoot(call);
} }


private void helperCanExposeExpression(
DecompositionType expectedResult,
String code,
String fnName) {
helperCanExposeExpression(expectedResult, code, fnName, null);
}

private void helperCanExposeFunctionExpression( private void helperCanExposeFunctionExpression(
DecompositionType expectedResult, String code, int call) { DecompositionType expectedResult, String code, int call) {
Compiler compiler = getCompiler(); Compiler compiler = getCompiler();
Set<String> knownConstants = new HashSet<>();
ExpressionDecomposer decomposer = new ExpressionDecomposer( ExpressionDecomposer decomposer = new ExpressionDecomposer(
compiler, compiler.getUniqueNameIdSupplier(), compiler, compiler.getUniqueNameIdSupplier(),
knownConstants, newScope(), allowMethodCallDecomposing); knownConstants, newScope(), allowMethodCallDecomposing);
Expand All @@ -771,12 +764,8 @@ private void helperCanExposeFunctionExpression(
private void helperCanExposeExpression( private void helperCanExposeExpression(
DecompositionType expectedResult, DecompositionType expectedResult,
String code, String code,
String fnName, String fnName) {
Set<String> knownConstants) {
Compiler compiler = getCompiler(); Compiler compiler = getCompiler();
if (knownConstants == null) {
knownConstants = new HashSet<>();
}
ExpressionDecomposer decomposer = new ExpressionDecomposer( ExpressionDecomposer decomposer = new ExpressionDecomposer(
compiler, compiler.getUniqueNameIdSupplier(), compiler, compiler.getUniqueNameIdSupplier(),
knownConstants, newScope(), allowMethodCallDecomposing); knownConstants, newScope(), allowMethodCallDecomposing);
Expand All @@ -790,47 +779,22 @@ private void helperCanExposeExpression(
assertNotNull("Call to " + fnName + " was not found.", callSite); assertNotNull("Call to " + fnName + " was not found.", callSite);


compiler.resetUniqueNameId(); compiler.resetUniqueNameId();
DecompositionType result = decomposer.canExposeExpression( DecompositionType result = decomposer.canExposeExpression(callSite);
callSite);
assertEquals(expectedResult, result); assertEquals(expectedResult, result);
} }


private void helperExposeExpression( private void helperExposeExpression(
String code, String code,
String fnName, String fnName,
String expectedResult) { String expectedResult) {
helperExposeExpression(code, fnName, expectedResult, null); helperExposeExpression(code, (tree) -> findCall(tree, fnName), expectedResult);
}


private void validateSourceInfo(Compiler compiler, Node subtree) {
(new LineNumberCheck(compiler)).setCheckSubTree(subtree);
// Source information problems are reported as compiler errors.
if (compiler.getErrorCount() != 0) {
String msg = "Error encountered: ";
for (JSError err : compiler.getErrors()) {
msg += err + "\n";
}
assertEquals(msg, 0, compiler.getErrorCount());
}
}
private void helperExposeExpression(
String code,
String fnName,
String expectedResult,
Set<String> knownConstants) {
helperExposeExpression(code, (tree) -> findCall(tree, fnName), expectedResult, knownConstants);
} }


private void helperExposeExpression( private void helperExposeExpression(
String code, String code,
Function<Node, Node> nodeFinder, Function<Node, Node> nodeFinder,
String expectedResult, String expectedResult) {
Set<String> knownConstants) {
Compiler compiler = getCompiler(); Compiler compiler = getCompiler();
if (knownConstants == null) {
knownConstants = new HashSet<>();
}
ExpressionDecomposer decomposer = new ExpressionDecomposer( ExpressionDecomposer decomposer = new ExpressionDecomposer(
compiler, compiler.getUniqueNameIdSupplier(), compiler, compiler.getUniqueNameIdSupplier(),
knownConstants, newScope(), allowMethodCallDecomposing); knownConstants, newScope(), allowMethodCallDecomposing);
Expand All @@ -840,14 +804,14 @@ private void helperExposeExpression(
Node tree = parse(compiler, code); Node tree = parse(compiler, code);
assertNotNull(tree); assertNotNull(tree);


Node callSite = nodeFinder.apply(tree); Node expr = nodeFinder.apply(tree);
assertWithMessage("Expected node was not found.").that(callSite).isNotNull(); assertWithMessage("Expected node was not found.").that(expr).isNotNull();


DecompositionType result = decomposer.canExposeExpression(callSite); DecompositionType result = decomposer.canExposeExpression(expr);
assertEquals(DecompositionType.DECOMPOSABLE, result); assertEquals(DecompositionType.DECOMPOSABLE, result);


compiler.resetUniqueNameId(); compiler.resetUniqueNameId();
decomposer.exposeExpression(callSite); decomposer.exposeExpression(expr);
validateSourceInfo(compiler, tree); validateSourceInfo(compiler, tree);
String explanation = expectedRoot.checkTreeEquals(tree); String explanation = expectedRoot.checkTreeEquals(tree);
assertNull("\nExpected: " + compiler.toSource(expectedRoot) assertNull("\nExpected: " + compiler.toSource(expectedRoot)
Expand All @@ -859,33 +823,14 @@ private void helperMoveExpression(
String code, String code,
String fnName, String fnName,
String expectedResult) { String expectedResult) {
helperMoveExpression(code, fnName, expectedResult, null); helperMoveExpression(code, (tree) -> findCall(tree, fnName), expectedResult);
} }


private void helperMoveExpression( private void helperMoveExpression(
String code, String code,
Function<Node, Node> nodeFinder, Function<Node, Node> nodeFinder,
String expectedResult) { String expectedResult) {
helperMoveExpression(code, nodeFinder, expectedResult, null);
}

private void helperMoveExpression(
String code,
String fnName,
String expectedResult,
Set<String> knownConstants) {
helperMoveExpression(code, (tree) -> findCall(tree, fnName), expectedResult, knownConstants);
}

private void helperMoveExpression(
String code,
Function<Node, Node> nodeFinder,
String expectedResult,
Set<String> knownConstants) {
Compiler compiler = getCompiler(); Compiler compiler = getCompiler();
if (knownConstants == null) {
knownConstants = new HashSet<>();
}


ExpressionDecomposer decomposer = new ExpressionDecomposer( ExpressionDecomposer decomposer = new ExpressionDecomposer(
compiler, compiler.getUniqueNameIdSupplier(), compiler, compiler.getUniqueNameIdSupplier(),
Expand All @@ -896,11 +841,11 @@ private void helperMoveExpression(
Node tree = parse(compiler, code); Node tree = parse(compiler, code);
assertNotNull(tree); assertNotNull(tree);


Node callSite = nodeFinder.apply(tree); Node expr = nodeFinder.apply(tree);
assertWithMessage("Expected node was not found.").that(callSite).isNotNull(); assertWithMessage("Expected node was not found.").that(expr).isNotNull();


compiler.resetUniqueNameId(); compiler.resetUniqueNameId();
decomposer.moveExpression(callSite); decomposer.moveExpression(expr);
validateSourceInfo(compiler, tree); validateSourceInfo(compiler, tree);
String explanation = expectedRoot.checkTreeEquals(tree); String explanation = expectedRoot.checkTreeEquals(tree);
assertNull("\nExpected: " + compiler.toSource(expectedRoot) assertNull("\nExpected: " + compiler.toSource(expectedRoot)
Expand Down Expand Up @@ -972,14 +917,25 @@ Node find(Node n) {
return (new Find()).find(root); return (new Find()).find(root);
} }


private void validateSourceInfo(Compiler compiler, Node subtree) {
(new LineNumberCheck(compiler)).setCheckSubTree(subtree);
// Source information problems are reported as compiler errors.
if (compiler.getErrorCount() != 0) {
String msg = "Error encountered: ";
for (JSError err : compiler.getErrors()) {
msg += err + "\n";
}
assertEquals(msg, 0, compiler.getErrorCount());
}
}

private static Node parse(Compiler compiler, String js) { private static Node parse(Compiler compiler, String js) {
Node n = Normalize.parseAndNormalizeTestCode(compiler, js); Node n = Normalize.parseAndNormalizeTestCode(compiler, js);
assertEquals(Arrays.toString(compiler.getErrors()), assertThat(compiler.getErrors()).isEmpty();
0, compiler.getErrorCount());
return n; return n;
} }


private Scope newScope() { private Scope newScope() {
return Scope.createGlobalScope(new Node(Token.SCRIPT)); return Scope.createGlobalScope(new Node(Token.ROOT));
} }
} }

0 comments on commit a500208

Please sign in to comment.