Skip to content

Commit

Permalink
Clean up NewTypeInferenceTestBase & related tests
Browse files Browse the repository at this point in the history
1. setUp() should always call super.setUp() & declare 'throws Exception'
2. Don't call setUp() from any method. Provide a method to explicitly create a
   fresh compiler instead.
4. Rename 'getOptions()' to 'getDefaultOptions()' to make it clearer that it doesn't
   just fetch the options object out of the compiler.
2. Make it easy to modify compiler options for individual test cases.
3. Make "passes" a local variable.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=127739917
  • Loading branch information
brad4d authored and blickly committed Jul 18, 2016
1 parent 4bca626 commit 30eef27
Show file tree
Hide file tree
Showing 12 changed files with 45 additions and 38 deletions.
11 changes: 8 additions & 3 deletions test/com/google/javascript/jscomp/CompilerTypeTestCase.java
Expand Up @@ -66,7 +66,7 @@ abstract class CompilerTypeTestCase extends BaseJSTypeTestCase {


protected Compiler compiler; protected Compiler compiler;


protected CompilerOptions getOptions() { protected CompilerOptions getDefaultOptions() {
CompilerOptions options = new CompilerOptions(); CompilerOptions options = new CompilerOptions();
options.setLanguageIn(LanguageMode.ECMASCRIPT5); options.setLanguageIn(LanguageMode.ECMASCRIPT5);
options.setWarningLevel( options.setWarningLevel(
Expand Down Expand Up @@ -102,9 +102,14 @@ protected void checkReportedWarningsHelper(String[] expected) {
} }


@Override @Override
protected void setUp() { protected void setUp() throws Exception {
super.setUp();
initializeNewCompiler(getDefaultOptions());
}

protected void initializeNewCompiler(CompilerOptions options) {
compiler = new Compiler(); compiler = new Compiler();
compiler.initOptions(getOptions()); compiler.initOptions(options);
registry = compiler.getTypeRegistry(); registry = compiler.getTypeRegistry();
initTypes(); initTypes();
} }
Expand Down
2 changes: 1 addition & 1 deletion test/com/google/javascript/jscomp/LinkedFlowScopeTest.java
Expand Up @@ -40,7 +40,7 @@ public final class LinkedFlowScopeTest extends CompilerTypeTestCase {
private FlowScope localEntry; private FlowScope localEntry;


@Override @Override
public void setUp() { public void setUp() throws Exception {
super.setUp(); super.setUp();


globalScope = TypedScope.createGlobalScope(blockNode); globalScope = TypedScope.createGlobalScope(blockNode);
Expand Down
Expand Up @@ -16656,7 +16656,7 @@ public void testSimpleInferPrototypeProperties() {
} }


public void testReportUknownTypes() { public void testReportUknownTypes() {
this.reportUnknownTypes = true; compilerOptions.setWarningLevel(DiagnosticGroups.REPORT_UNKNOWN_TYPES, CheckLevel.WARNING);


typeCheck( typeCheck(
"var x = globalvar;", "var x = globalvar;",
Expand Down
42 changes: 20 additions & 22 deletions test/com/google/javascript/jscomp/NewTypeInferenceTestBase.java
Expand Up @@ -18,6 +18,7 @@


import com.google.common.base.Joiner; import com.google.common.base.Joiner;
import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableList;
import com.google.javascript.jscomp.CompilerOptions.LanguageMode;
import com.google.javascript.rhino.IR; import com.google.javascript.rhino.IR;
import com.google.javascript.rhino.InputId; import com.google.javascript.rhino.InputId;
import com.google.javascript.rhino.Node; import com.google.javascript.rhino.Node;
Expand All @@ -32,8 +33,8 @@
* @author dimvar@google.com (Dimitris Vardoulakis) * @author dimvar@google.com (Dimitris Vardoulakis)
*/ */
public abstract class NewTypeInferenceTestBase extends CompilerTypeTestCase { public abstract class NewTypeInferenceTestBase extends CompilerTypeTestCase {
private List<PassFactory> passes;
protected boolean reportUnknownTypes; protected CompilerOptions compilerOptions;


protected static final String CLOSURE_BASE = protected static final String CLOSURE_BASE =
LINE_JOINER.join( LINE_JOINER.join(
Expand Down Expand Up @@ -170,14 +171,21 @@ public abstract class NewTypeInferenceTestBase extends CompilerTypeTestCase {
""); "");


@Override @Override
protected void setUp() { protected void setUp() throws Exception {
super.setUp(); super.setUp();
this.passes = new ArrayList<>(); compilerOptions = getDefaultOptions();
} }


@Override @Override
protected void tearDown() { protected CompilerOptions getDefaultOptions() {
this.reportUnknownTypes = false; CompilerOptions compilerOptions = super.getDefaultOptions();
compilerOptions.setClosurePass(true);
compilerOptions.setNewTypeInference(true);
compilerOptions.setWarningLevel(
DiagnosticGroups.NEW_CHECK_TYPES_ALL_CHECKS, CheckLevel.WARNING);
// EC5 is the highest language level that type inference understands.
compilerOptions.setLanguage(LanguageMode.ECMASCRIPT5);
return compilerOptions;
} }


protected final PassFactory makePassFactory( protected final PassFactory makePassFactory(
Expand All @@ -191,23 +199,11 @@ protected CompilerPass create(AbstractCompiler compiler) {
} }


private final void parseAndTypeCheck(String externs, String js) { private final void parseAndTypeCheck(String externs, String js) {
// NOTE(dimvar): it's unusual that we run setUp for each test in a test initializeNewCompiler(compilerOptions);
// method rather than once per test method. But the parent class creates a
// new compiler object at every setUp call, and we need that.
setUp();
final CompilerOptions options = compiler.getOptions();
options.setClosurePass(true);
options.setNewTypeInference(true);
options.setWarningLevel(
DiagnosticGroups.NEW_CHECK_TYPES_ALL_CHECKS, CheckLevel.WARNING);
if (this.reportUnknownTypes) {
options.setWarningLevel(
DiagnosticGroups.REPORT_UNKNOWN_TYPES, CheckLevel.WARNING);
}
compiler.init( compiler.init(
ImmutableList.of(SourceFile.fromCode("[externs]", externs)), ImmutableList.of(SourceFile.fromCode("[externs]", externs)),
ImmutableList.of(SourceFile.fromCode("[testcode]", js)), ImmutableList.of(SourceFile.fromCode("[testcode]", js)),
options); compilerOptions);


Node externsRoot = IR.block(); Node externsRoot = IR.block();
externsRoot.setIsSyntheticBlock(true); externsRoot.setIsSyntheticBlock(true);
Expand All @@ -233,15 +229,17 @@ private final void parseAndTypeCheck(String externs, String js) {


DeclaredGlobalExternsOnWindow rewriteExterns = DeclaredGlobalExternsOnWindow rewriteExterns =
new DeclaredGlobalExternsOnWindow(compiler); new DeclaredGlobalExternsOnWindow(compiler);
List<PassFactory> passes = new ArrayList<>();
passes.add(makePassFactory("globalExternsOnWindow", rewriteExterns)); passes.add(makePassFactory("globalExternsOnWindow", rewriteExterns));
ProcessClosurePrimitives closurePass = ProcessClosurePrimitives closurePass =
new ProcessClosurePrimitives(compiler, null, CheckLevel.ERROR, false); new ProcessClosurePrimitives(compiler, null, CheckLevel.ERROR, false);
passes.add(makePassFactory("ProcessClosurePrimitives", closurePass)); passes.add(makePassFactory("ProcessClosurePrimitives", closurePass));
if (options.getLanguageIn() == CompilerOptions.LanguageMode.ECMASCRIPT6_TYPED) { if (compilerOptions.getLanguageIn() == LanguageMode.ECMASCRIPT6_TYPED) {
passes.add(makePassFactory("convertEs6TypedToEs6", passes.add(makePassFactory("convertEs6TypedToEs6",
new Es6TypedToEs6Converter(compiler))); new Es6TypedToEs6Converter(compiler)));
} }
if (options.getLanguageIn().isEs6OrHigher()) { if (compilerOptions.getLanguageIn().isEs6OrHigher()
&& !compilerOptions.getLanguageOut().isEs6OrHigher()) {
TranspilationPasses.addEs6EarlyPasses(passes); TranspilationPasses.addEs6EarlyPasses(passes);
TranspilationPasses.addEs6LatePasses(passes); TranspilationPasses.addEs6LatePasses(passes);
} }
Expand Down
Expand Up @@ -29,9 +29,10 @@
public final class NewTypeInferenceWithTranspilationTest extends NewTypeInferenceTestBase { public final class NewTypeInferenceWithTranspilationTest extends NewTypeInferenceTestBase {


@Override @Override
protected void setUp() { protected void setUp() throws Exception {
super.setUp(); super.setUp();
compiler.getOptions().setLanguageIn(LanguageMode.ECMASCRIPT6); compilerOptions.setLanguageIn(LanguageMode.ECMASCRIPT6);
compilerOptions.setLanguageOut(LanguageMode.ECMASCRIPT3);
} }


public void testSimpleClasses() { public void testSimpleClasses() {
Expand Down
Expand Up @@ -34,9 +34,10 @@ public final class NewTypeInferenceWithTypeSyntaxTranspilationTest
extends NewTypeInferenceTestBase { extends NewTypeInferenceTestBase {


@Override @Override
protected void setUp() { protected void setUp() throws Exception {
super.setUp(); super.setUp();
compiler.getOptions().setLanguageIn(LanguageMode.ECMASCRIPT6_TYPED); compilerOptions.setLanguageIn(LanguageMode.ECMASCRIPT6_TYPED);
compilerOptions.setLanguageOut(LanguageMode.ECMASCRIPT3);
} }


public void testSimpleAnnotationsNoWarnings() { public void testSimpleAnnotationsNoWarnings() {
Expand Down Expand Up @@ -214,6 +215,8 @@ public void testAmbientDeclarationsInCode() {
} }


public void testGetterReturnNonDeclaredType() { public void testGetterReturnNonDeclaredType() {
// getters cannot be transpiled to EC3
compilerOptions.setLanguageOut(LanguageMode.ECMASCRIPT5);
typeCheck( typeCheck(
"var x = {get a(): number { return 'str'; }}", "var x = {get a(): number { return 'str'; }}",
NewTypeInference.RETURN_NONDECLARED_TYPE); NewTypeInference.RETURN_NONDECLARED_TYPE);
Expand Down
Expand Up @@ -31,7 +31,7 @@ public class PolymerBehaviorExtractorTest extends CompilerTypeTestCase {
private Node behaviorArray; private Node behaviorArray;


@Override @Override
protected void setUp() { protected void setUp() throws Exception {
super.setUp(); super.setUp();
behaviorArray = null; behaviorArray = null;
} }
Expand Down
Expand Up @@ -25,7 +25,7 @@ public final class PolymerClassDefinitionTest extends CompilerTypeTestCase {
private Node polymerCall; private Node polymerCall;


@Override @Override
protected void setUp() { protected void setUp() throws Exception {
super.setUp(); super.setUp();
polymerCall = null; polymerCall = null;
} }
Expand Down
Expand Up @@ -60,7 +60,7 @@ public final class PolymerClassRewriterTest extends CompilerTypeTestCase {
private Node polymerCall; private Node polymerCall;


@Override @Override
protected void setUp() { protected void setUp() throws Exception {
super.setUp(); super.setUp();
polymerCall = null; polymerCall = null;
rootNode = null; rootNode = null;
Expand Down Expand Up @@ -116,7 +116,7 @@ public void testPathAssignmentTarget() {
} }


@Override @Override
protected CompilerOptions getOptions() { protected CompilerOptions getDefaultOptions() {
CompilerOptions options = new CompilerOptions(); CompilerOptions options = new CompilerOptions();
options.setLanguageIn(LanguageMode.ECMASCRIPT5); options.setLanguageIn(LanguageMode.ECMASCRIPT5);
options.setWarningLevel( options.setWarningLevel(
Expand Down
Expand Up @@ -33,7 +33,7 @@ public final class SemanticReverseAbstractInterpreterTest
private TypedScope functionScope; private TypedScope functionScope;


@Override @Override
protected void setUp() { protected void setUp() throws Exception {
super.setUp(); super.setUp();


interpreter = new SemanticReverseAbstractInterpreter(registry); interpreter = new SemanticReverseAbstractInterpreter(registry);
Expand Down
2 changes: 1 addition & 1 deletion test/com/google/javascript/jscomp/TypeCheckTest.java
Expand Up @@ -56,7 +56,7 @@ public final class TypeCheckTest extends CompilerTypeTestCase {
+ " make sure to give it a type.)"; + " make sure to give it a type.)";


@Override @Override
public void setUp() { public void setUp() throws Exception {
super.setUp(); super.setUp();
} }


Expand Down
Expand Up @@ -39,7 +39,7 @@ public final class TypeTransformationTest extends CompilerTypeTestCase {
+ "var n = 10;"; + "var n = 10;";


@Override @Override
public void setUp() { public void setUp() throws Exception {
super.setUp(); super.setUp();
errorReporter = new TestErrorReporter(null, null); errorReporter = new TestErrorReporter(null, null);
initRecordTypeTests(); initRecordTypeTests();
Expand Down

0 comments on commit 30eef27

Please sign in to comment.