Skip to content

Commit

Permalink
Unify tests with JSModule[] sources with the common test path.
Browse files Browse the repository at this point in the history
-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=158941525
  • Loading branch information
concavelenz authored and Tyler Breisacher committed Jun 14, 2017
1 parent af4c16d commit d6b015d
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 50 deletions.
109 changes: 76 additions & 33 deletions test/com/google/javascript/jscomp/CompilerTestCase.java
Expand Up @@ -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());
Expand Down Expand Up @@ -1117,12 +1121,19 @@ protected void test(List<SourceFile> js, List<SourceFile> expected) {
}

private List<SourceFile> createSources(String name, String... sources) {
if (sources == null) {
return null;
}
return createSources(name, ImmutableList.copyOf(sources));
}

private List<SourceFile> createSources(String name, List<String> sources) {
if (sources == null) {
return null;
}
List<SourceFile> 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;
}
Expand All @@ -1134,7 +1145,7 @@ private List<SourceFile> 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));
}

/**
Expand All @@ -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);
}

/**
Expand Down Expand Up @@ -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));
}

/**
Expand All @@ -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));
}

/**
Expand Down Expand Up @@ -1320,7 +1316,10 @@ protected void testInternal(
Sources inputsObj, // TODO remove this parameter
Expected expectedObj,
Diagnostic diagnostic) {
List<SourceFile> inputs = inputsObj != null ? inputsObj.sources : null;
List<SourceFile> inputs =
(inputsObj instanceof FlatSources)
? ((FlatSources) inputsObj).sources
: null;
List<SourceFile> expected = expectedObj != null ? expectedObj.expected : null;
checkState(!this.typeCheckEnabled || !this.newTypeInferenceEnabled);
checkState(this.setUpRan, "CompilerTestCase.setUp not run: call super.setUp() from overrides.");
Expand Down Expand Up @@ -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<SourceFile> files) {
return new Sources(files);
return new FlatSources(files);
}

protected Sources srcs(JSModule[] modules) {
return new ModuleSources(modules);
}

protected Expected expected(String srcText) {
Expand All @@ -2021,6 +2024,23 @@ protected Expected expected(List<SourceFile> 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<String> 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));
}
Expand Down Expand Up @@ -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);
Expand All @@ -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;
Expand Down Expand Up @@ -2111,11 +2143,22 @@ protected static final class Expected implements TestPart {
}
}

protected static final class Sources implements TestPart {
final List<SourceFile> sources;
protected abstract static class Sources implements TestPart {
}

protected static class FlatSources extends Sources {
final ImmutableList<SourceFile> sources;

FlatSources(List<SourceFile> files) {
sources = ImmutableList.copyOf(files);
}
}

protected static final class ModuleSources extends Sources {
final ImmutableList<JSModule> modules;

Sources(List<SourceFile> files) {
sources = files;
ModuleSources(JSModule[] modules) {
this.modules = ImmutableList.copyOf(modules);
}
}

Expand Down
Expand Up @@ -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() {
Expand Down Expand Up @@ -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));
}

Expand Down
20 changes: 5 additions & 15 deletions test/com/google/javascript/jscomp/VarCheckTest.java
Expand Up @@ -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;
Expand Down Expand Up @@ -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");
Expand All @@ -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));
}
}

Expand Down

0 comments on commit d6b015d

Please sign in to comment.