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 * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */


package com.google.javascript.jscomp; package com.google.javascript.jscomp;


import static com.google.common.base.Preconditions.checkState; 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.AbstractCompiler.LifeCycleStage;
import com.google.javascript.jscomp.NodeTraversal.Callback; import com.google.javascript.jscomp.NodeTraversal.Callback;
import com.google.javascript.rhino.IR; import com.google.javascript.rhino.IR;
Expand All @@ -31,7 +30,15 @@
* @author johnlenz@google.com (John Lenz) * @author johnlenz@google.com (John Lenz)
*/ */
public final class FunctionToBlockMutatorTest extends TestCase { 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() { public void testMutateNoReturnWithoutResultAssignment() {
helperMutate( helperMutate(
Expand All @@ -41,10 +48,11 @@ public void testMutateNoReturnWithoutResultAssignment() {
} }


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




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


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


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


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


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


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


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


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


public void testMutateFunctionDefinitionHoisting() { public void testMutateFunctionDefinitionHoisting() {
helperMutate( helperMutate(
LINE_JOINER.join( lines(
"function foo(a){", "function foo(a){",
" var b = g(a);", " var b = g(a);",
" function g(c){ return c; }", " function g(c){ return c; }",
Expand All @@ -198,7 +201,7 @@ public void testMutateFunctionDefinitionHoisting() {
" function i(){}", " function i(){}",
"}", "}",
"foo(1);"), "foo(1);"),
LINE_JOINER.join( lines(
"{", "{",
" var g$jscomp$inline_2 = function(c$jscomp$inline_6) {return c$jscomp$inline_6};", " var g$jscomp$inline_2 = function(c$jscomp$inline_6) {return c$jscomp$inline_6};",
" var h$jscomp$inline_4 = function(){};", " var h$jscomp$inline_4 = function(){};",
Expand All @@ -210,42 +213,16 @@ public void testMutateFunctionDefinitionHoisting() {
null); 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) { private void validateSourceInfo(Compiler compiler, Node subtree) {
(new LineNumberCheck(compiler)).setCheckSubTree(subtree); (new LineNumberCheck(compiler)).setCheckSubTree(subtree);
// Source information problems are reported as compiler errors. assertThat(compiler.getErrors()).isEmpty();
if (compiler.getErrorCount() != 0) {
String msg = "Error encountered: ";
for (JSError err : compiler.getErrors()) {
msg += err + "\n";
}
assertEquals(msg, 0, compiler.getErrorCount());
}
} }


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


// inline tester // inline tester
Method tester = new Method() { Method tester = (NodeTraversal t, Node n, Node parent) -> {
@Override Node result = mutator.mutate(
public boolean call(NodeTraversal t, Node n, Node parent) { fnName, fnNode, n, resultName,

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


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


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


Expand Down

0 comments on commit 0861529

Please sign in to comment.