Skip to content

Commit

Permalink
[NTI] Enable NTI in GatherExternPropertiesTest.
Browse files Browse the repository at this point in the history
-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=167326565
  • Loading branch information
shicks authored and blickly committed Sep 1, 2017
1 parent 4ffbb2a commit 83d52f6
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 27 deletions.
99 changes: 78 additions & 21 deletions test/com/google/javascript/jscomp/GatherExternPropertiesTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,53 @@

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

import com.google.common.collect.ImmutableSet;
import com.google.javascript.jscomp.newtypes.JSTypeCreatorFromJSDoc;

/**
* Test case for {@link GatherExternProperties}.
*/
public final class GatherExternPropertiesTest extends CompilerTestCase {
public final class GatherExternPropertiesTest extends TypeICompilerTestCase {

private static final String EXTERNS = LINE_JOINER.join(
"/**",
" * @constructor",
" * @param {*=} opt_value",
" * @return {!Object}",
" */",
"function Object(opt_value) {}",
"/**",
" * @constructor",
" * @param {...*} var_args",
" */",
"function Function(var_args) {}",
"/**",
" * @constructor",
" * @param {*=} arg",
" * @return {string}",
" */",
"function String(arg) {}",
"/**",
" * @record",
" * @template VALUE",
" */",
"/**",
" * @template T",
" * @constructor ",
" * @param {...*} var_args",
" * @return {!Array<?>}",
" */",
"function Array(var_args) {}");

public GatherExternPropertiesTest() {
super(EXTERNS);
}

@Override void checkMinimalExterns(Iterable<SourceFile> externs) {}

@Override
protected void setUp() throws Exception {
super.setUp();
enableTypeCheck();
this.mode = TypeInferenceMode.BOTH;
}

@Override
Expand Down Expand Up @@ -106,7 +143,7 @@ public void testGatherExternPropertiesIncludingRecordTypes() {

// Record types as template arguments.
assertExternProperties(
"/** @type {Array.<{bar: string, baz: string}>} */ var foo;",
"/** @type {Array<{bar: string, baz: string}>} */ var foo;",
"bar", "baz");

// Record types in implemented interfaces.
Expand All @@ -115,10 +152,10 @@ public void testGatherExternPropertiesIncludingRecordTypes() {
" * @interface",
" * @template T",
" */",
"var Foo;",
"var Foo = function() {};",
"/**",
" * @constructor",
" * @implements {Foo.<{bar: string, baz: string}>}",
" * @implements {Foo<{bar: string, baz: string}>}",
" */",
"var Bar;"),
"bar", "baz");
Expand All @@ -132,7 +169,7 @@ public void testGatherExternPropertiesIncludingRecordTypes() {
"var Foo = function() {};",
"/**",
" * @constructor",
" * @extends {Foo.<{bar: string, baz: string}>}",
" * @extends {Foo<{bar: string, baz: string}>}",
" */",
"var Bar = function() {};"),
"bar", "baz");
Expand All @@ -150,7 +187,24 @@ public void testGatherExternPropertiesIncludingRecordTypes() {
"/** @type {{bar: string, baz: {foobar: string}}} */ var foo;",
"bar", "baz", "foobar");

// Recursive types.
// Recursive @record types.
assertExternProperties(LINE_JOINER.join(
"/** @record */",
"function D1() { /** @type {D2} */ this.a; }",
"",
"/** @record */",
"function D2() { /** @type {D1} */ this.b; }"),
"a", "b");
assertExternProperties(LINE_JOINER.join(
"/** @record */",
"function D1() { /** @type {function(D2)} */ this.a; }",
"",
"/** @record */",
"function D2() { /** @type {D1} */ this.b; }"),
"a", "b");

// Recursive types
ignoreWarnings(JSTypeCreatorFromJSDoc.CIRCULAR_TYPEDEF_ENUM);
assertExternProperties(LINE_JOINER.join(
"/** @typedef {{a: D2}} */",
"var D1;",
Expand All @@ -169,16 +223,14 @@ public void testGatherExternPropertiesIncludingRecordTypes() {
// Record types defined in normal code and referenced in externs should
// not bleed-through.
testSame(
// Externs.
"/** @type {NonExternType} */ var foo;",
// Normal code.
"/** @typedef {{bar: string, baz: string}} */ var NonExternType;");
// Check that no properties were found.
assertThat(getLastCompiler().getExternProperties()).isEmpty();
externs(EXTERNS + "/** @type {NonExternType} */ var foo;"),
srcs("/** @typedef {{bar: string, baz: string}} */ var NonExternType;"),
// Check that no properties were found.
expectExterns());
}

public void testExternClassNoTypeCheck() {
disableTypeCheck();
this.mode = TypeInferenceMode.NEITHER;
assertExternProperties(
LINE_JOINER.join(
"class Foo {",
Expand All @@ -192,7 +244,6 @@ public void testExternClassNoTypeCheck() {
}

public void testExternClassWithTypeCheck() {
enableTypeCheck();
allowExternsChanges();
enableTranspile();
assertExternProperties(
Expand All @@ -208,7 +259,7 @@ public void testExternClassWithTypeCheck() {
}

public void testExternWithMethod() {
disableTypeCheck();
this.mode = TypeInferenceMode.NEITHER;
assertExternProperties(
LINE_JOINER.join(
"foo = {",
Expand All @@ -218,7 +269,7 @@ public void testExternWithMethod() {
}

public void testExternAsyncFunction() {
disableTypeCheck();
this.mode = TypeInferenceMode.NEITHER;
assertExternProperties(
LINE_JOINER.join(
"function *gen() {",
Expand All @@ -230,9 +281,15 @@ public void testExternAsyncFunction() {
"next", "value");
}

private static Postcondition expectExterns(final String... properties) {
return new Postcondition() {
@Override void verify(Compiler compiler) {
assertThat(compiler.getExternProperties()).containsExactly((Object[]) properties);
}
};
}

private void assertExternProperties(String externs, String... properties) {
testSame(externs, "");
assertEquals(ImmutableSet.copyOf(properties),
getLastCompiler().getExternProperties());
testSame(externs(EXTERNS + externs), srcs(""), expectExterns(properties));
}
}
11 changes: 5 additions & 6 deletions test/com/google/javascript/jscomp/TypeICompilerTestCase.java
Original file line number Diff line number Diff line change
Expand Up @@ -80,27 +80,26 @@ protected void testInternal(
testOTI(externs, js, expected, diagnostic, postconditions);
}
if (this.mode.runsNTI()) {
if (!findMinimalExterns(externs.externs)) {
fail("NTI reqires at least the MINIMAL_EXTERNS");
}
checkMinimalExterns(externs.externs);
testNTI(externs, js, expected, diagnostic, postconditions);
}
if (this.mode.runsNeither()) {
super.testInternal(externs, js, expected, diagnostic, postconditions);
}
}

private static boolean findMinimalExterns(Iterable<SourceFile> externs) {
// Note: may be overridden to allow different externs if necessary.
void checkMinimalExterns(Iterable<SourceFile> externs) {
try {
for (SourceFile extern : externs) {
if (extern.getCode().contains(MINIMAL_EXTERNS)) {
return true;
return;
}
}
} catch (IOException e) {
throw new RuntimeException(e);
}
return false;
fail("NTI reqires at least the MINIMAL_EXTERNS");
}

private void testOTI(
Expand Down

0 comments on commit 83d52f6

Please sign in to comment.