Skip to content

Commit

Permalink
Switch ExternExportsPassTest to use CompilerTestCase so that it's mor…
Browse files Browse the repository at this point in the history
…e consistent with other tests.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=152535605
  • Loading branch information
tbreisacher committed Apr 7, 2017
1 parent 57183f0 commit 333e143
Showing 1 changed file with 36 additions and 67 deletions.
103 changes: 36 additions & 67 deletions test/com/google/javascript/jscomp/ExternExportsPassTest.java
Expand Up @@ -13,46 +13,34 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */

package com.google.javascript.jscomp; package com.google.javascript.jscomp;


import static com.google.common.truth.Truth.assertThat; import static com.google.common.truth.Truth.assertThat;


import com.google.common.base.Joiner;
import com.google.common.collect.ImmutableList;
import com.google.javascript.jscomp.testing.BlackHoleErrorManager;
import java.util.List;
import junit.framework.TestCase;

/** /**
* Tests for {@link ExternExportsPass}. * Tests for {@link ExternExportsPass}.
* *
*/ */
// TODO(tbreisacher): Convert this to use CompilerTestCase. public final class ExternExportsPassTest extends CompilerTestCase {


public final class ExternExportsPassTest extends TestCase { @Override
private static final Joiner LINE_JOINER = Joiner.on('\n'); public void setUp() throws Exception {

super.setUp();
/** enableNormalize();
* ExternExportsPass relies on type information to emit JSDoc annotations for enableTypeCheck();
* exported externs. However, the user can disable type checking and still
* ask for externs to be exported. Set this flag to enable or disable checking
* of types during a test.
*/
private boolean runCheckTypes = true;

private void enableTypeCheck() {
runCheckTypes = true;
} }


private void disableTypeCheck() { @Override
runCheckTypes = false; public CompilerOptions getOptions(CompilerOptions options) {
options.externExportsPath = "externs.js";
// Check types so we can make sure our exported externs have type information.
options.setCheckSymbols(true);
return options;
} }


@Override @Override
public void setUp() throws Exception { public CompilerPass getProcessor(Compiler compiler) {
super.setUp(); return new ExternExportsPass(compiler);
enableTypeCheck();
} }


public void testExportSymbol() throws Exception { public void testExportSymbol() throws Exception {
Expand Down Expand Up @@ -571,24 +559,22 @@ public void testUseExportsAsExterns() {
String librarySource = String librarySource =
LINE_JOINER.join( LINE_JOINER.join(
"/**", "/**",
" * @param {number} a", " * @param {number} n",
" * @constructor", " * @constructor",
" */", " */",
"var InternalName = function(a) {", "var InternalName = function(n) {",
"};", "};",
"goog.exportSymbol('ExternalName', InternalName)"); "goog.exportSymbol('ExternalName', InternalName)");


String clientSource = String clientSource =
LINE_JOINER.join( LINE_JOINER.join(
"var a = new ExternalName(6);", "var foo = new ExternalName(6);",
"/**", "/**",
" * @param {ExternalName} x", " * @param {ExternalName} x",
" */", " */",
"var b = function(x) {};"); "var bar = function(x) {};");


Result libraryCompileResult = compileAndExportExterns(librarySource); String generatedExterns = compileAndExportExterns(librarySource);

String generatedExterns = libraryCompileResult.externExport;


compileAndExportExterns(clientSource, generatedExterns); compileAndExportExterns(clientSource, generatedExterns);
} }
Expand Down Expand Up @@ -873,7 +859,7 @@ public void testNullabilityInRecordTypes() throws Exception {
} }


private void compileAndCheck(String js, String expected) { private void compileAndCheck(String js, String expected) {
Result result = compileAndExportExterns(js); String generatedExterns = compileAndExportExterns(js);


String fileoverview = LINE_JOINER.join( String fileoverview = LINE_JOINER.join(
"/**", "/**",
Expand All @@ -882,7 +868,7 @@ private void compileAndCheck(String js, String expected) {
" */", " */",
""); "");


assertEquals(fileoverview + expected, result.externExport); assertThat(generatedExterns).isEqualTo(fileoverview + expected);
} }


public void testDontWarnOnExportFunctionWithUnknownParameterTypes() { public void testDontWarnOnExportFunctionWithUnknownParameterTypes() {
Expand All @@ -900,7 +886,13 @@ public void testDontWarnOnExportFunctionWithUnknownParameterTypes() {
compileAndExportExterns(librarySource); compileAndExportExterns(librarySource);
} }


private Result compileAndExportExterns(String js) { /**
* Compiles the passed in JavaScript and returns the new externs exported by the this pass.
*
* @param js the source to be compiled
* @return the externs generated from {@code js}
*/
private String compileAndExportExterns(String js) {
return compileAndExportExterns(js, ""); return compileAndExportExterns(js, "");
} }


Expand All @@ -912,38 +904,15 @@ private Result compileAndExportExterns(String js) {
* @param externs the externs the {@code js} source needs * @param externs the externs the {@code js} source needs
* @return the externs generated from {@code js} * @return the externs generated from {@code js}
*/ */
private Result compileAndExportExterns(String js, String externs) { private String compileAndExportExterns(String js, String externs) {
Compiler compiler = new Compiler(); js = LINE_JOINER.join(
BlackHoleErrorManager.silence(compiler); "var goog = {};",
CompilerOptions options = new CompilerOptions(); "goog.exportSymbol = function(a, b) {};",
options.externExportsPath = "externs.js"; "goog.exportProperty = function(a, b, c) {};",
options.declaredGlobalExternsOnWindow = false; js);

/* Check types so we can make sure our exported externs have
* type information.
*/
options.setCheckSymbols(true);
options.setCheckTypes(runCheckTypes);

List<SourceFile> inputs =
ImmutableList.of(
SourceFile.fromCode(
"testcode",
LINE_JOINER.join(
"var goog = {};",
"goog.exportSymbol = function(a, b) {};",
"goog.exportProperty = function(a, b, c) {};",
js)));

List<SourceFile> externFiles = ImmutableList.of(
SourceFile.fromCode("externs", externs));

Result result = compiler.compile(externFiles, inputs, options);


assertThat(result.warnings).isEmpty(); testSame(externs, js, null);
assertThat(result.errors).isEmpty();
assertThat(result.success).isTrue();


return result; return getLastCompiler().getResult().externExport;
} }
} }

0 comments on commit 333e143

Please sign in to comment.