Skip to content

Commit

Permalink
Add a couple tests of ExpressionDecomposer showing that it can move c…
Browse files Browse the repository at this point in the history
…lass expressions.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=173193898
  • Loading branch information
tbreisacher authored and blickly committed Oct 24, 2017
1 parent 633c46a commit 886cb82
Showing 1 changed file with 71 additions and 26 deletions.
97 changes: 71 additions & 26 deletions test/com/google/javascript/jscomp/ExpressionDecomposerTest.java
Expand Up @@ -16,13 +16,16 @@

package com.google.javascript.jscomp;

import static com.google.common.truth.Truth.assertWithMessage;

import com.google.javascript.jscomp.CompilerOptions.LanguageMode;
import com.google.javascript.jscomp.ExpressionDecomposer.DecompositionType;
import com.google.javascript.rhino.Node;
import com.google.javascript.rhino.Token;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
import java.util.function.Function;
import javax.annotation.Nullable;
import junit.framework.TestCase;

Expand Down Expand Up @@ -418,6 +421,20 @@ public void testExposeExpression16() {
"var temp$jscomp$0; if (temp$jscomp$0 = bar()) temp$jscomp$0=foo(); throw temp$jscomp$0;");
}

public void testMoveClass1() {
helperMoveExpression(
"alert(class X {});",
ExpressionDecomposerTest::findClass,
"var result$jscomp$0 = class X {}; alert(result$jscomp$0);");
}

public void testMoveClass2() {
helperMoveExpression(
"console.log(1, 2, class X {});",
ExpressionDecomposerTest::findClass,
"var result$jscomp$0 = class X {}; console.log(1, 2, result$jscomp$0);");
}

public void testExposeYieldExpression1() {
helperMoveExpression(
"function *f() { return { a: yield 1, c: foo(yield 2, yield 3) }; }",
Expand Down Expand Up @@ -556,10 +573,10 @@ public void testExposePlusEquals5() {
}

public void testExposeObjectLit1() {
// Validate that getter and setters methods are see as side-effect
// Validate that getter and setters methods are seen as side-effect
// free and that values can move past them. We don't need to be
// concerned with exposing the getter or setter here but the
// decomposer does not have a method of exposing properties only variables.
// decomposer does not have a method of exposing properties, only variables.
helperMoveExpression(
"var x = {get a() {}, b: foo()};",
"foo",
Expand All @@ -576,8 +593,7 @@ public void testExposeObjectLit1() {
private void helperCanExposeExpression(
DecompositionType expectedResult,
String code,
String fnName
) {
String fnName) {
helperCanExposeExpression(expectedResult, code, fnName, null);
}

Expand All @@ -600,17 +616,15 @@ private void helperCanExposeFunctionExpression(
assertNotNull("Call " + call + " was not found.", callSite);

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

private void helperCanExposeExpression(
DecompositionType expectedResult,
String code,
String fnName,
Set<String> knownConstants
) {
Set<String> knownConstants) {
Compiler compiler = getCompiler();
if (knownConstants == null) {
knownConstants = new HashSet<>();
Expand Down Expand Up @@ -638,12 +652,11 @@ private void helperCanExposeExpression(
private void helperExposeExpression(
String code,
String fnName,
String expectedResult
) {
helperExposeExpression(
code, fnName, expectedResult, null);
String expectedResult) {
helperExposeExpression(code, fnName, expectedResult, null);
}


private void validateSourceInfo(Compiler compiler, Node subtree) {
(new LineNumberCheck(compiler)).setCheckSubTree(subtree);
// Source information problems are reported as compiler errors.
Expand All @@ -655,13 +668,19 @@ private void validateSourceInfo(Compiler compiler, Node subtree) {
assertEquals(msg, 0, compiler.getErrorCount());
}
}

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

private void helperExposeExpression(
String code,
Function<Node, Node> nodeFinder,
String expectedResult,
Set<String> knownConstants) {
Compiler compiler = getCompiler();
if (knownConstants == null) {
knownConstants = new HashSet<>();
Expand All @@ -675,8 +694,8 @@ private void helperExposeExpression(
Node tree = parse(compiler, code);
assertNotNull(tree);

Node callSite = findCall(tree, fnName);
assertNotNull("Call to " + fnName + " was not found.", callSite);
Node callSite = nodeFinder.apply(tree);
assertWithMessage("Expected node was not found.").that(callSite).isNotNull();

DecompositionType result = decomposer.canExposeExpression(callSite);
assertEquals(DecompositionType.DECOMPOSABLE, result);
Expand All @@ -693,18 +712,30 @@ private void helperExposeExpression(
private void helperMoveExpression(
String code,
String fnName,
String expectedResult
) {
helperMoveExpression(
code, fnName, expectedResult, null);
String expectedResult) {
helperMoveExpression(code, fnName, expectedResult, null);
}

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

private void helperMoveExpression(
String code,
String fnName,
String expectedResult,
Set<String> knownConstants
) {
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();
if (knownConstants == null) {
knownConstants = new HashSet<>();
Expand All @@ -719,8 +750,8 @@ private void helperMoveExpression(
Node tree = parse(compiler, code);
assertNotNull(tree);

Node callSite = findCall(tree, fnName);
assertNotNull("Call to " + fnName + " was not found.", callSite);
Node callSite = nodeFinder.apply(tree);
assertWithMessage("Expected node was not found.").that(callSite).isNotNull();

compiler.resetUniqueNameId();
decomposer.moveExpression(callSite);
Expand All @@ -744,8 +775,22 @@ private static Node findCall(Node n, String name) {
return findCall(n, name, 1);
}

@Nullable
private static Node findClass(Node n) {
if (n.isClass()) {
return n;
}
for (Node child : n.children()) {
Node maybeClass = findClass(child);
if (maybeClass != null) {
return maybeClass;
}
}
return null;
}

/**
* @param name The name to look for.
* @param name The name to look for. If name is null, look for a yield expression instead.
* @param call The call to look for.
* @return The return the Nth instance of the CALL/YIELD node
* matching name found in a pre-order traversal.
Expand Down

0 comments on commit 886cb82

Please sign in to comment.