From 08615291b14c3372e4ba1eb03613fdecf3cb2a0b Mon Sep 17 00:00:00 2001 From: tbreisacher Date: Tue, 12 Dec 2017 14:36:13 -0800 Subject: [PATCH] Test cleanup to make FunctionToBlockMutatorTest easier to follow ------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=178816701 --- .../jscomp/FunctionToBlockMutatorTest.java | 116 +++++++----------- 1 file changed, 45 insertions(+), 71 deletions(-) diff --git a/test/com/google/javascript/jscomp/FunctionToBlockMutatorTest.java b/test/com/google/javascript/jscomp/FunctionToBlockMutatorTest.java index 494c4358ffd..a64cf93f137 100644 --- a/test/com/google/javascript/jscomp/FunctionToBlockMutatorTest.java +++ b/test/com/google/javascript/jscomp/FunctionToBlockMutatorTest.java @@ -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; @@ -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( @@ -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"); } @@ -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) {", @@ -92,9 +102,7 @@ public void testMutateWithMultipleReturns() { " result=void 0", " }", "}"), - "foo", - true, - false); + "foo"); } public void testMutateWithParameters1() { @@ -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:", " {", @@ -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() { @@ -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; }", @@ -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(){};", @@ -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()); @@ -267,20 +244,16 @@ 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(); @@ -288,7 +261,8 @@ public boolean call(NodeTraversal t, Node n, Node parent) { NodeTraversal.traverseEs6(compiler, script, test); } - interface Method { + @FunctionalInterface + private interface Method { boolean call(NodeTraversal t, Node n, Node parent); }