Skip to content

Commit

Permalink
Add CompilerTestCase option to not compare synthetic code
Browse files Browse the repository at this point in the history
-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=190965292
  • Loading branch information
lauraharker authored and Tyler Breisacher committed Mar 30, 2018
1 parent dbd18af commit ac0b647
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 11 deletions.
5 changes: 4 additions & 1 deletion src/com/google/javascript/jscomp/Compiler.java
Expand Up @@ -2592,6 +2592,9 @@ void recordFunctionInformation() {
*/
static final String SYNTHETIC_EXTERNS_AT_END = "{SyntheticVarsAtEnd}";

/** Prefix of the generated file name for synthetic injected libraries */
static final String SYNTHETIC_CODE_PREFIX = " [synthetic:";

private CompilerInput synthesizedExternsInput = null;
private CompilerInput synthesizedExternsInputAtEnd = null;

Expand Down Expand Up @@ -3397,7 +3400,7 @@ Node ensureLibraryInjected(String resourceName, boolean force) {
// Load/parse the code.
String originalCode = ResourceLoader.loadTextResource(
Compiler.class, "js/" + resourceName + ".js");
Node ast = parseSyntheticCode(" [synthetic:" + resourceName + "] ", originalCode);
Node ast = parseSyntheticCode(SYNTHETIC_CODE_PREFIX + resourceName + "] ", originalCode);

// Look for string literals of the form 'require foo bar' or 'externs baz' or 'normalize'.
// As we process each one, remove it from its parent.
Expand Down
28 changes: 28 additions & 0 deletions test/com/google/javascript/jscomp/CompilerTestCase.java
Expand Up @@ -66,6 +66,9 @@ public abstract class CompilerTestCase extends TestCase {
/** Externs for the test */
final List<SourceFile> externsInputs;

/** Whether to include synthetic code when comparing actual to expected */
private boolean compareSyntheticCode;

/** Whether to compare input and output as trees instead of strings */
private boolean compareAsTree;

Expand Down Expand Up @@ -580,6 +583,7 @@ protected void setUp() throws Exception {
this.closurePassEnabledForExpected = false;
this.compareAsTree = true;
this.compareJsDoc = true;
this.compareSyntheticCode = true;
this.computeSideEffects = false;
this.expectParseWarningsThisTest = false;
this.expectedSymbolTableError = null;
Expand Down Expand Up @@ -785,6 +789,17 @@ protected void enableNewTypeInference() {
this.newTypeInferenceEnabled = true;
}

/**
* When comparing expected to actual, ignore nodes created through compiler.ensureLibraryInjected
*
* <p>This differs from using a NonInjecting compiler in that the compiler still injects the
* polyfills when requested.
*/
protected final void disableCompareSyntheticCode() {
checkState(this.setUpRan, "Attempted to configure before running setUp().");
compareSyntheticCode = false;
}

/**
* Run using multistage compilation.
*/
Expand Down Expand Up @@ -1654,6 +1669,19 @@ private void testInternal(
}

if (expected != null) {
if (!compareSyntheticCode) {
// remove code in files starting with [synthetic:
Node scriptRoot = mainRoot.getFirstChild();
Node child = scriptRoot.getFirstChild();
while (child != null) {
Node nextChild = child.getNext();
String sourceFile = child.getSourceFileName();
if (sourceFile != null && sourceFile.startsWith(Compiler.SYNTHETIC_CODE_PREFIX)) {
scriptRoot.removeChild(child);
}
child = nextChild;
}
}
if (compareAsTree) {
String explanation;
if (compareJsDoc) {
Expand Down
11 changes: 1 addition & 10 deletions test/com/google/javascript/jscomp/Es6RewriteGeneratorsTest.java
Expand Up @@ -27,6 +27,7 @@ protected void setUp() throws Exception {
allowMethodCallDecomposing = false;
setAcceptedLanguage(LanguageMode.ECMASCRIPT_2015);
enableRunTypeCheckAfterProcessing();
disableCompareSyntheticCode();
}

@Override
Expand Down Expand Up @@ -1111,14 +1112,4 @@ public void testFinally() {
" thrown = $jscomp$generator$context.enterCatchBlock();",
" return $jscomp$generator$context.yield(thrown,0)"));
}

@Override
protected Compiler createCompiler() {
return new NoninjectingCompiler();
}

@Override
protected NoninjectingCompiler getLastCompiler() {
return (NoninjectingCompiler) super.getLastCompiler();
}
}

0 comments on commit ac0b647

Please sign in to comment.