Skip to content

Commit

Permalink
Add integration tests for Polymer including one revealing a bug that …
Browse files Browse the repository at this point in the history
…occurs when compiling Polymer 2 code with OTI

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=189415157
  • Loading branch information
tbreisacher authored and blickly committed Mar 19, 2018
1 parent 527f489 commit 7903b85
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 4 deletions.
83 changes: 79 additions & 4 deletions test/com/google/javascript/jscomp/IntegrationTest.java
Expand Up @@ -572,21 +572,96 @@ public void testWindowIsTypedEs6() {
TypeValidator.TYPE_MISMATCH_WARNING); TypeValidator.TYPE_MISMATCH_WARNING);
} }


public void testConstPolymerNotAllowed() { private void addPolymerExterns() {
ImmutableList.Builder<SourceFile> externsList = ImmutableList.builder();
externsList.addAll(externs);
externsList.add(
SourceFile.fromCode(
"polymer_externs.js",
lines(
"/** @return {function(new: PolymerElement)} */",
"var Polymer = function(descriptor) {};",
"",
"/** @constructor @extends {HTMLElement} */",
"var PolymerElement = function() {};", // Polymer 1
"",
"/** @constructor @extends {HTMLElement} */",
"Polymer.Element = function() {};", // Polymer 2
"")));
externs = externsList.build();
}

public void testPolymer1() {
CompilerOptions options = createCompilerOptions(); CompilerOptions options = createCompilerOptions();
options.setPolymerVersion(1); options.setPolymerVersion(1);
options.setLanguageIn(LanguageMode.ECMASCRIPT_2015); options.setWarningLevel(DiagnosticGroups.CHECK_TYPES, CheckLevel.ERROR);
options.setLanguageIn(LanguageMode.ECMASCRIPT_2017);
options.setLanguageOut(LanguageMode.ECMASCRIPT5); options.setLanguageOut(LanguageMode.ECMASCRIPT5);
addPolymerExterns();


externs = ImmutableList.of(SourceFile.fromCode("<externs>", test(
"var Polymer = function() {}; var PolymerElement = function() {};")); options,
"var XFoo = Polymer({ is: 'x-foo' });",
"var XFoo=function(){};XFoo=Polymer({is:'x-foo'})");
}

public void testConstPolymerElementNotAllowed() {
CompilerOptions options = createCompilerOptions();
options.setPolymerVersion(1);
options.setWarningLevel(DiagnosticGroups.CHECK_TYPES, CheckLevel.ERROR);
options.setLanguageIn(LanguageMode.ECMASCRIPT_2017);
options.setLanguageOut(LanguageMode.ECMASCRIPT5);
addPolymerExterns();


test( test(
options, options,
"const Foo = Polymer({ is: 'x-foo' });", "const Foo = Polymer({ is: 'x-foo' });",
PolymerPassErrors.POLYMER_INVALID_DECLARATION); PolymerPassErrors.POLYMER_INVALID_DECLARATION);
} }


public void testPolymer2_nti() {
CompilerOptions options = createCompilerOptions();
options.setPolymerVersion(2);
options.setNewTypeInference(true);
options.setWarningLevel(DiagnosticGroups.CHECK_TYPES, CheckLevel.ERROR);
options.declaredGlobalExternsOnWindow = true;
options.setLanguageIn(LanguageMode.ECMASCRIPT_2017);
options.setLanguageOut(LanguageMode.ECMASCRIPT5);
addPolymerExterns();

Compiler compiler = compile(
options,
lines(
"class XFoo extends PolymerElement {",
" get is() { return 'x-foo'; }",
" static get properties() { return { bar: Boolean, }; }",
"}"));
assertThat(compiler.getErrors()).isEmpty();
assertThat(compiler.getWarnings()).isEmpty();
}

public void testPolymer2_oti() {
CompilerOptions options = createCompilerOptions();
options.setPolymerVersion(2);
options.setNewTypeInference(false);
options.setWarningLevel(DiagnosticGroups.CHECK_TYPES, CheckLevel.ERROR);
options.declaredGlobalExternsOnWindow = true;
options.setLanguageIn(LanguageMode.ECMASCRIPT_2017);
options.setLanguageOut(LanguageMode.ECMASCRIPT5);
addPolymerExterns();

// TODO(b/74549003): Fix this so that OTI doesn't report an error.
test(
options,
lines(
"class XFoo extends Polymer.Element {",
" get is() { return 'x-foo'; }",
" static get properties() { return {}; }",
"}"),
// Bad type annotation. Unknown type Polymer.ElementProperties
RhinoErrorReporter.UNRECOGNIZED_TYPE_ERROR);
}

public void testPreservedForwardDeclare() { public void testPreservedForwardDeclare() {
CompilerOptions options = createCompilerOptions(); CompilerOptions options = createCompilerOptions();
WarningLevel.VERBOSE.setOptionsForWarningLevel(options); WarningLevel.VERBOSE.setOptionsForWarningLevel(options);
Expand Down
4 changes: 4 additions & 0 deletions test/com/google/javascript/jscomp/IntegrationTestCase.java
Expand Up @@ -188,6 +188,7 @@ abstract class IntegrationTestCase extends TestCase {
"Object.seal;", "Object.seal;",
"Object.defineProperties;", "Object.defineProperties;",
"Object.defineProperty;", "Object.defineProperty;",
"Object.getOwnPropertyDescriptor;",
"", "",
"Object.prototype;", "Object.prototype;",
"", "",
Expand Down Expand Up @@ -271,6 +272,9 @@ abstract class IntegrationTestCase extends TestCase {
"function IThenable() {}", "function IThenable() {}",
"", "",
"IThenable.prototype.then = function(callback) {};", "IThenable.prototype.then = function(callback) {};",
"",
"/** @constructor */",
"var HTMLElement = function() {};",
""))); "")));


protected List<SourceFile> externs = DEFAULT_EXTERNS; protected List<SourceFile> externs = DEFAULT_EXTERNS;
Expand Down

0 comments on commit 7903b85

Please sign in to comment.