Skip to content

Commit

Permalink
Add more tests of ES2015+ features in externs
Browse files Browse the repository at this point in the history
-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=189233382
  • Loading branch information
tbreisacher authored and blickly committed Mar 15, 2018
1 parent 25ac0fd commit dc8db26
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 6 deletions.
4 changes: 2 additions & 2 deletions test/com/google/javascript/jscomp/CompilerTestCase.java
Expand Up @@ -262,11 +262,11 @@ public abstract class CompilerTestCase extends TestCase {
"function IArrayLike() {};",
"/**",
" * @template T",
" * @constructor ",
" * @constructor",
" * @implements {IArrayLike<T>} ",
" * @implements {Iterable<T>}",
" * @param {...*} var_args",
" * @return {!Array.<?>}",
" * @return {!Array<?>}",
" */",
"function Array(var_args) {}");

Expand Down
53 changes: 50 additions & 3 deletions test/com/google/javascript/jscomp/IntegrationTest.java
Expand Up @@ -3178,15 +3178,62 @@ public void testDuplicateVariablesInExterns() {
testSame(options, "");
}

public void testEs6InExterns() {
public void testEs6ClassInExterns() {
CompilerOptions options = createCompilerOptions();
options.setWarningLevel(DiagnosticGroups.CHECK_TYPES, CheckLevel.WARNING);
externs = ImmutableList.of(SourceFile.fromCode(
"externs", "class ExternalClass { externalMethod() {} }"));
options.setLanguageIn(LanguageMode.ECMASCRIPT_2017);
options.setLanguageOut(LanguageMode.ECMASCRIPT3);

ImmutableList.Builder<SourceFile> externsList = ImmutableList.builder();
externsList.addAll(externs);
externsList.add(
SourceFile.fromCode("extraExterns", "class ExternalClass { externalMethod() {} }"));
externs = externsList.build();

testSame(options, "(new ExternalClass).externalMethod();");
test(options, "(new ExternalClass).nonexistentMethod();", TypeCheck.INEXISTENT_PROPERTY);
}

public void testGeneratorFunctionInExterns() {
CompilerOptions options = createCompilerOptions();
options.setWarningLevel(DiagnosticGroups.CHECK_TYPES, CheckLevel.WARNING);
options.setLanguageIn(LanguageMode.ECMASCRIPT_2017);
options.setLanguageOut(LanguageMode.ECMASCRIPT3);

ImmutableList.Builder<SourceFile> externsList = ImmutableList.builder();
externsList.addAll(externs);
externsList.add(
SourceFile.fromCode(
"extraExterns", "/** @return {!Iterable<number>} */ function* gen() {}"));
externs = externsList.build();

test(
options,
"for (let x of gen()) { x.call(); }",
// Property call never defined on Number.
TypeCheck.INEXISTENT_PROPERTY);
}

public void testAsyncFunctionInExterns() {
CompilerOptions options = createCompilerOptions();
options.setWarningLevel(DiagnosticGroups.CHECK_TYPES, CheckLevel.WARNING);
options.setLanguageIn(LanguageMode.ECMASCRIPT_2017);
options.setLanguageOut(LanguageMode.ECMASCRIPT3);

ImmutableList.Builder<SourceFile> externsList = ImmutableList.builder();
externsList.addAll(externs);
externsList.add(
SourceFile.fromCode(
"extraExterns", "/** @return {!Promise<number>} */ async function init() {}"));
externs = externsList.build();

test(
options,
"init().then((n) => { n.call(); });",
// Property call never defined on Number.
TypeCheck.INEXISTENT_PROPERTY);
}

public void testLanguageMode() {
CompilerOptions options = createCompilerOptions();

Expand Down
37 changes: 36 additions & 1 deletion test/com/google/javascript/jscomp/IntegrationTestCase.java
Expand Up @@ -98,6 +98,8 @@ abstract class IntegrationTestCase extends TestCase {
"/** @constructor */",
"function ObjectPropertyDescriptor() {};",
"",
"ObjectPropertyDescriptor.prototype.value;",
"",
"/** @constructor */ function Window() {}",
"/** @type {string} */ Window.prototype.name;",
"/** @type {string} */ Window.prototype.offsetWidth;",
Expand Down Expand Up @@ -131,6 +133,11 @@ abstract class IntegrationTestCase extends TestCase {
"/** @return {IteratorIterable<T>} */",
"Array.prototype.values;",
"",
"Array.prototype.splice;",
"Array.prototype.push;",
"Array.prototype.reverse;",
"Array.prototype.pop;",
"",
"/**",
" * @constructor",
" * @return {number}",
Expand All @@ -146,6 +153,8 @@ abstract class IntegrationTestCase extends TestCase {
" */",
"function String(opt_str) {}",
"",
"String.prototype.split = function(delimiter) {};",
"",
"/**",
" * @constructor",
" * @return {boolean}",
Expand Down Expand Up @@ -178,6 +187,7 @@ abstract class IntegrationTestCase extends TestCase {
"function Object(opt_value) {}",
"Object.seal;",
"Object.defineProperties;",
"Object.defineProperty;",
"",
"Object.prototype;",
"",
Expand Down Expand Up @@ -236,7 +246,32 @@ abstract class IntegrationTestCase extends TestCase {
" * @param {?} exception",
" * @return {!IIterableResult<VALUE>}",
" */",
"Generator.prototype.throw = function(exception) {};")));
"Generator.prototype.throw = function(exception) {};",
"",
"/**",
" * @constructor",
" * @template T",
" */",
"function Promise(resolver) {}",
"",
"Promise.resolve = function(value) {};",
"",
"/**",
" * @param {function(T)=} opt_successCallback",
" * @param {function(!Error)=} opt_errorCallback",
" */",
"Promise.prototype.then = function(opt_successCallback, opt_errorCallback) {};",
"",
"/**",
" * @typedef {{then: ?}}",
" */",
"var Thenable;",
"",
"/** @interface */",
"function IThenable() {}",
"",
"IThenable.prototype.then = function(callback) {};",
"")));

protected List<SourceFile> externs = DEFAULT_EXTERNS;

Expand Down

0 comments on commit dc8db26

Please sign in to comment.