From d6b015d13407e0a8e011236252ce33216357301b Mon Sep 17 00:00:00 2001 From: johnlenz Date: Tue, 13 Jun 2017 22:22:32 -0700 Subject: [PATCH] Unify tests with JSModule[] sources with the common test path. ------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=158941525 --- .../javascript/jscomp/CompilerTestCase.java | 109 ++++++++++++------ .../jscomp/ProcessClosurePrimitivesTest.java | 4 +- .../javascript/jscomp/VarCheckTest.java | 20 +--- 3 files changed, 83 insertions(+), 50 deletions(-) diff --git a/test/com/google/javascript/jscomp/CompilerTestCase.java b/test/com/google/javascript/jscomp/CompilerTestCase.java index 8eb0973b317..469c1e62f27 100644 --- a/test/com/google/javascript/jscomp/CompilerTestCase.java +++ b/test/com/google/javascript/jscomp/CompilerTestCase.java @@ -1069,8 +1069,12 @@ protected void testInternal( CompilerOptions options = getOptions(); - options.setCheckTypes(parseTypeInfo || this.typeCheckEnabled); - compiler.init(externs.externs, inputs.sources, options); + if (inputs instanceof FlatSources) { + options.setCheckTypes(parseTypeInfo || this.typeCheckEnabled); + compiler.init(externs.externs, ((FlatSources) inputs).sources, options); + } else { + compiler.initModules(externsInputs, ((ModuleSources) inputs).modules, getOptions()); + } if (this.typeCheckEnabled) { BaseJSTypeTestCase.addNativeProperties(compiler.getTypeRegistry()); @@ -1117,12 +1121,19 @@ protected void test(List js, List expected) { } private List createSources(String name, String... sources) { + if (sources == null) { + return null; + } + return createSources(name, ImmutableList.copyOf(sources)); + } + + private List createSources(String name, List sources) { if (sources == null) { return null; } List expectedSources = new ArrayList<>(); - for (int i = 0; i < sources.length; i++) { - expectedSources.add(SourceFile.fromCode(name + i, sources[i])); + for (int i = 0; i < sources.size(); i++) { + expectedSources.add(SourceFile.fromCode(name + i, sources.get(i))); } return expectedSources; } @@ -1134,7 +1145,7 @@ private List createSources(String name, String... sources) { * @param expected Expected JS outputs (one per module) */ protected void test(JSModule[] modules, String[] expected) { - test(modules, expected(expected), null); + test(srcs(modules), expected(expected)); } /** @@ -1147,12 +1158,8 @@ protected void test(JSModule[] modules, String[] expected) { * @param diagnostic the warning or error expected */ protected void test( - JSModule[] modules, Expected expected, Diagnostic diagnostic) { - Compiler compiler = createCompiler(); - lastCompiler = compiler; - - compiler.initModules(externsInputs, ImmutableList.copyOf(modules), getOptions()); - test(compiler, null, expected, diagnostic); + JSModule[] modules, String[] expected, Diagnostic diagnostic) { + test(srcs(modules), expected(expected), diagnostic); } /** @@ -1262,7 +1269,7 @@ protected void testSameWarning(String[] js, DiagnosticType warning) { * @param modules Module inputs */ protected void testSame(JSModule[] modules) { - testSame(modules, null); + test(srcs(modules), expected(modules)); } /** @@ -1272,18 +1279,7 @@ protected void testSame(JSModule[] modules) { * @param warning A warning, or null for no expected warning. */ protected void testSame(JSModule[] modules, DiagnosticType warning) { - try { - String[] expected = new String[modules.length]; - for (int i = 0; i < modules.length; i++) { - expected[i] = ""; - for (CompilerInput input : modules[i].getInputs()) { - expected[i] += input.getSourceFile().getCode(); - } - } - test(modules, expected(expected), warning(warning)); - } catch (IOException e) { - throw new RuntimeException(e); - } + test(srcs(modules), expected(modules), warning(warning)); } /** @@ -1320,7 +1316,10 @@ protected void testInternal( Sources inputsObj, // TODO remove this parameter Expected expectedObj, Diagnostic diagnostic) { - List inputs = inputsObj != null ? inputsObj.sources : null; + List inputs = + (inputsObj instanceof FlatSources) + ? ((FlatSources) inputsObj).sources + : null; List expected = expectedObj != null ? expectedObj.expected : null; checkState(!this.typeCheckEnabled || !this.newTypeInferenceEnabled); checkState(this.setUpRan, "CompilerTestCase.setUp not run: call super.setUp() from overrides."); @@ -1998,15 +1997,19 @@ protected String lines(String ...lines) { } protected Sources srcs(String srcText) { - return new Sources(maybeCreateSources(filename, srcText)); + return new FlatSources(maybeCreateSources(filename, srcText)); } protected Sources srcs(String[] srcTexts) { - return new Sources(createSources("input", srcTexts)); + return new FlatSources(createSources("input", srcTexts)); } protected Sources srcs(List files) { - return new Sources(files); + return new FlatSources(files); + } + + protected Sources srcs(JSModule[] modules) { + return new ModuleSources(modules); } protected Expected expected(String srcText) { @@ -2021,6 +2024,23 @@ protected Expected expected(List files) { return new Expected(files); } + protected Expected expected(JSModule[] modules) { + // create an expected source output from the list of inputs in the modules in order. + List expectedSrcs = new ArrayList<>(); + for (JSModule module : modules) { + String expectedSrc = ""; + for (CompilerInput input : module.getInputs()) { + try { + expectedSrc += input.getSourceFile().getCode(); + } catch (IOException e) { + throw new RuntimeException("ouch", e); + } + } + expectedSrcs.add(expectedSrc); + } + return expected(expectedSrcs.toArray(new String[0])); + } + protected Externs externs(String externSrc) { return new Externs(maybeCreateSources("externs", externSrc)); } @@ -2054,13 +2074,14 @@ protected ErrorDiagnostic error(DiagnosticType type, String match) { protected void testSame(TestPart ...parts) { Expected expected = null; + // Pick out the "srcs" and create a coorisponding "expected" to match. int i = 0; TestPart[] finalParts = new TestPart[parts.length + 1]; for (TestPart part : parts) { finalParts[i++] = part; if (part instanceof Sources) { Preconditions.checkState(expected == null); - expected = expected(((Sources) part).sources); + expected = fromSources((Sources) part); } } Preconditions.checkState(expected != null); @@ -2069,6 +2090,17 @@ protected void testSame(TestPart ...parts) { test(finalParts); } + private Expected fromSources(Sources srcs) { + if (srcs instanceof FlatSources) { + return expected(((FlatSources) srcs).sources); + } else if (srcs instanceof ModuleSources) { + ModuleSources modules = ((ModuleSources) srcs); + return expected(modules.modules.toArray(new JSModule[0])); + } else { + throw new IllegalStateException("unexpected"); + } + } + protected void test(TestPart ...parts) { // TODO(johnlenz): make "ignore" and "nothing" explicit. Externs externs = null; @@ -2111,11 +2143,22 @@ protected static final class Expected implements TestPart { } } - protected static final class Sources implements TestPart { - final List sources; + protected abstract static class Sources implements TestPart { + } + + protected static class FlatSources extends Sources { + final ImmutableList sources; + + FlatSources(List files) { + sources = ImmutableList.copyOf(files); + } + } + + protected static final class ModuleSources extends Sources { + final ImmutableList modules; - Sources(List files) { - sources = files; + ModuleSources(JSModule[] modules) { + this.modules = ImmutableList.copyOf(modules); } } diff --git a/test/com/google/javascript/jscomp/ProcessClosurePrimitivesTest.java b/test/com/google/javascript/jscomp/ProcessClosurePrimitivesTest.java index 8c3d2de464f..8afaee6a8b6 100644 --- a/test/com/google/javascript/jscomp/ProcessClosurePrimitivesTest.java +++ b/test/com/google/javascript/jscomp/ProcessClosurePrimitivesTest.java @@ -137,7 +137,7 @@ public void process(Node externs, Node root) { } private void testModule(String[] moduleInputs, String[] expected) { - test(createModuleStar(moduleInputs), expected(expected), null); + test(createModuleStar(moduleInputs), expected); } public void testSimpleProvides() { @@ -580,7 +580,7 @@ public void testSetCssNameMappingValidity() { public void testBadCrossModuleRequire() { test( createModuleStar("", "goog.provide('goog.ui');", "goog.require('goog.ui');"), - expected(new String[] {"", "/** @const */ goog.ui = {};", ""}), + new String[] {"", "/** @const */ goog.ui = {};", ""}, warning(XMODULE_REQUIRE_ERROR)); } diff --git a/test/com/google/javascript/jscomp/VarCheckTest.java b/test/com/google/javascript/jscomp/VarCheckTest.java index 4640f1fc285..eedfc91db05 100644 --- a/test/com/google/javascript/jscomp/VarCheckTest.java +++ b/test/com/google/javascript/jscomp/VarCheckTest.java @@ -19,7 +19,6 @@ import static com.google.common.truth.Truth.assertThat; import static com.google.javascript.jscomp.VarCheck.VAR_MULTIPLY_DECLARED_ERROR; -import com.google.common.base.Preconditions; import com.google.javascript.jscomp.CompilerOptions.LanguageMode; import com.google.javascript.jscomp.NodeTraversal.AbstractPostOrderCallback; import com.google.javascript.rhino.Node; @@ -427,16 +426,6 @@ private void testIndependentModules(String code1, String code2, testTwoModules(code1, code2, false, error, warning); } - private Diagnostic fromDiagnosticType( - DiagnosticType error, - DiagnosticType warning, - String description) { - Preconditions.checkState(error == null || warning == null); - Diagnostic diagnotic = (error != null) ? error(error, description) - : (warning != null) ? warning(warning, description) : null; - return diagnotic; - } - private void testTwoModules(String code1, String code2, boolean m2DependsOnm1, DiagnosticType error, DiagnosticType warning) { JSModule m1 = new JSModule("m1"); @@ -446,11 +435,12 @@ private void testTwoModules(String code1, String code2, boolean m2DependsOnm1, if (m2DependsOnm1) { m2.addDependency(m1); } - if (error == null) { - test(new JSModule[] { m1, m2 }, - expected(new String[] { code1, code2 }), warning(warning)); + if (error == null && warning == null) { + test(new JSModule[] { m1, m2 }, new String[] { code1, code2 }); + } else if (error == null) { + test(new JSModule[] { m1, m2 }, new String[] { code1, code2 }, warning(warning)); } else { - test(new JSModule[] { m1, m2 }, null, fromDiagnosticType(error, warning, null)); + testError(srcs(new JSModule[] { m1, m2 }), error(error)); } }