From 83d52f6931da10adc08effe94d5fcadffef0437c Mon Sep 17 00:00:00 2001 From: sdh Date: Fri, 1 Sep 2017 14:39:03 -0700 Subject: [PATCH] [NTI] Enable NTI in GatherExternPropertiesTest. ------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=167326565 --- .../jscomp/GatherExternPropertiesTest.java | 99 +++++++++++++++---- .../jscomp/TypeICompilerTestCase.java | 11 +-- 2 files changed, 83 insertions(+), 27 deletions(-) diff --git a/test/com/google/javascript/jscomp/GatherExternPropertiesTest.java b/test/com/google/javascript/jscomp/GatherExternPropertiesTest.java index 5039ce74c47..64205794b15 100644 --- a/test/com/google/javascript/jscomp/GatherExternPropertiesTest.java +++ b/test/com/google/javascript/jscomp/GatherExternPropertiesTest.java @@ -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 externs) {} + @Override protected void setUp() throws Exception { super.setUp(); - enableTypeCheck(); + this.mode = TypeInferenceMode.BOTH; } @Override @@ -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. @@ -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"); @@ -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"); @@ -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;", @@ -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 {", @@ -192,7 +244,6 @@ public void testExternClassNoTypeCheck() { } public void testExternClassWithTypeCheck() { - enableTypeCheck(); allowExternsChanges(); enableTranspile(); assertExternProperties( @@ -208,7 +259,7 @@ public void testExternClassWithTypeCheck() { } public void testExternWithMethod() { - disableTypeCheck(); + this.mode = TypeInferenceMode.NEITHER; assertExternProperties( LINE_JOINER.join( "foo = {", @@ -218,7 +269,7 @@ public void testExternWithMethod() { } public void testExternAsyncFunction() { - disableTypeCheck(); + this.mode = TypeInferenceMode.NEITHER; assertExternProperties( LINE_JOINER.join( "function *gen() {", @@ -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)); } } diff --git a/test/com/google/javascript/jscomp/TypeICompilerTestCase.java b/test/com/google/javascript/jscomp/TypeICompilerTestCase.java index a7ed161eea5..418ad820930 100644 --- a/test/com/google/javascript/jscomp/TypeICompilerTestCase.java +++ b/test/com/google/javascript/jscomp/TypeICompilerTestCase.java @@ -80,9 +80,7 @@ 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()) { @@ -90,17 +88,18 @@ protected void testInternal( } } - private static boolean findMinimalExterns(Iterable externs) { + // Note: may be overridden to allow different externs if necessary. + void checkMinimalExterns(Iterable 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(