Skip to content

Commit

Permalink
Test cleanup to make FunctionToBlockMutatorTest easier to follow
Browse files Browse the repository at this point in the history
-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=178816701
  • Loading branch information
tbreisacher authored and brad4d committed Dec 13, 2017
1 parent e3a30f2 commit 0861529
Showing 1 changed file with 45 additions and 71 deletions.
116 changes: 45 additions & 71 deletions test/com/google/javascript/jscomp/FunctionToBlockMutatorTest.java
Expand Up @@ -13,13 +13,12 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/


package com.google.javascript.jscomp;

import static com.google.common.base.Preconditions.checkState;
import static com.google.common.truth.Truth.assertThat;
import static com.google.javascript.jscomp.CompilerTestCase.lines;

import com.google.common.base.Joiner;
import com.google.javascript.jscomp.AbstractCompiler.LifeCycleStage;
import com.google.javascript.jscomp.NodeTraversal.Callback;
import com.google.javascript.rhino.IR;
Expand All @@ -31,7 +30,15 @@
* @author johnlenz@google.com (John Lenz)
*/
public final class FunctionToBlockMutatorTest extends TestCase {
protected static final Joiner LINE_JOINER = Joiner.on('\n');

private boolean needsDefaultResult;
private boolean isCallInLoop;

@Override
public void setUp() {
needsDefaultResult = false;
isCallInLoop = false;
}

public void testMutateNoReturnWithoutResultAssignment() {
helperMutate(
Expand All @@ -41,10 +48,11 @@ public void testMutateNoReturnWithoutResultAssignment() {
}

public void testMutateNoReturnWithResultAssignment() {
needsDefaultResult = true;
helperMutate(
"function foo(){}; var result = foo();",
"{result = void 0}",
"foo", true, false);
"foo");
}


Expand All @@ -70,16 +78,18 @@ public void testMutateValueReturnWithoutResultAssignment() {
}

public void testMutateValueReturnWithResultAssignment() {
needsDefaultResult = true;
helperMutate(
"function foo(){return true;}; var x=foo();",
"{x=true}",
"foo", "x", true, false);
"foo", "x");
}

public void testMutateWithMultipleReturns() {
needsDefaultResult = true;
helperMutate(
"function foo(){ if (0) {return 0} else {return 1} }; var result=foo();",
LINE_JOINER.join(
lines(
"{",
" JSCompiler_inline_label_foo_0: {",
" if (0) {",
Expand All @@ -92,9 +102,7 @@ public void testMutateWithMultipleReturns() {
" result=void 0",
" }",
"}"),
"foo",
true,
false);
"foo");
}

public void testMutateWithParameters1() {
Expand Down Expand Up @@ -130,19 +138,18 @@ public void testMutate8() {
}

public void testMutateInitializeUninitializedVars1() {
isCallInLoop = true;
helperMutate(
"function foo(a){var b;return a;}; foo(1);",
"{var b$jscomp$inline_1 = void 0; 1;}",
"foo",
null,
false,
true);
null);
}

public void testMutateInitializeUninitializedVars2() {
helperMutate(
"function foo(a) {for(var b in c)return a;}; foo(1);",
LINE_JOINER.join(
lines(
"{",
" JSCompiler_inline_label_foo_2:",
" {",
Expand All @@ -158,24 +165,20 @@ public void testMutateInitializeUninitializedVars2() {

public void testMutateCallInLoopVars1() {
// baseline: outside a loop, the constant remains constant.
boolean callInLoop = false;
isCallInLoop = false;
helperMutate(
"function foo(a){var B = bar(); a;}; foo(1);",
"{var B$jscomp$inline_1 = bar(); 1;}",
"foo",
null,
false,
callInLoop);
null);
// ... in a loop, the constant-ness is removed.
// TODO(johnlenz): update this test to look for the const annotation.
callInLoop = true;
isCallInLoop = true;
helperMutate(
"function foo(a){var B = bar(); a;}; foo(1);",
"{var B$jscomp$inline_1 = bar(); 1;}",
"foo",
null,
false,
callInLoop);
null);
}

public void testMutateFunctionDefinition() {
Expand All @@ -189,7 +192,7 @@ public void testMutateFunctionDefinition() {

public void testMutateFunctionDefinitionHoisting() {
helperMutate(
LINE_JOINER.join(
lines(
"function foo(a){",
" var b = g(a);",
" function g(c){ return c; }",
Expand All @@ -198,7 +201,7 @@ public void testMutateFunctionDefinitionHoisting() {
" function i(){}",
"}",
"foo(1);"),
LINE_JOINER.join(
lines(
"{",
" var g$jscomp$inline_2 = function(c$jscomp$inline_6) {return c$jscomp$inline_6};",
" var h$jscomp$inline_4 = function(){};",
Expand All @@ -210,42 +213,16 @@ public void testMutateFunctionDefinitionHoisting() {
null);
}

public void helperMutate(
String code, final String expectedResult, final String fnName) {
helperMutate(code, expectedResult, fnName, false, false);
}

public void helperMutate(
String code, final String expectedResult, final String fnName,
final boolean needsDefaultResult,
final boolean isCallInLoop) {
helperMutate(code, expectedResult, fnName,
"result", needsDefaultResult, isCallInLoop);
}

public void helperMutate(
String code, final String expectedResult, final String fnName,
final String resultName) {
helperMutate(code, expectedResult, fnName, resultName, false, false);
}

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());
}
assertThat(compiler.getErrors()).isEmpty();
}

public void helperMutate(
String code, final String expectedResult, final String fnName,
final String resultName,
final boolean needsDefaultResult,
final boolean isCallInLoop) {
public void helperMutate(String code, String expectedResult, String fnName) {
helperMutate(code, expectedResult, fnName, "result");
}

public void helperMutate(String code, String expectedResult, String fnName, String resultName) {
final Compiler compiler = new Compiler();
final FunctionToBlockMutator mutator = new FunctionToBlockMutator(
compiler, compiler.getUniqueNameIdSupplier());
Expand All @@ -267,28 +244,25 @@ public void helperMutate(
compiler.setLifeCycleStage(LifeCycleStage.NORMALIZED);

// inline tester
Method tester = new Method() {
@Override
public boolean call(NodeTraversal t, Node n, Node parent) {

Node result = mutator.mutate(
fnName, fnNode, n, resultName,
needsDefaultResult, isCallInLoop);
validateSourceInfo(compiler, result);
String explanation = expected.checkTreeEquals(result);
assertNull("\nExpected: " + compiler.toSource(expected)
+ "\nResult: " + compiler.toSource(result)
+ "\n" + explanation, explanation);
return true;
}
Method tester = (NodeTraversal t, Node n, Node parent) -> {
Node result = mutator.mutate(
fnName, fnNode, n, resultName,
needsDefaultResult, isCallInLoop);
validateSourceInfo(compiler, result);
String explanation = expected.checkTreeEquals(result);
assertNull("\nExpected: " + compiler.toSource(expected)
+ "\nResult: " + compiler.toSource(result)
+ "\n" + explanation, explanation);
return true;
};

compiler.resetUniqueNameId();
TestCallback test = new TestCallback(fnName, tester);
NodeTraversal.traverseEs6(compiler, script, test);
}

interface Method {
@FunctionalInterface
private interface Method {
boolean call(NodeTraversal t, Node n, Node parent);
}

Expand Down

0 comments on commit 0861529

Please sign in to comment.