Skip to content

Commit

Permalink
[dart:html] Change js_interop_constuctor_name tests to use namespace
Browse files Browse the repository at this point in the history
These tests test to make sure that @js interop classes can have the
same name as native classes but in a separate namespace. Since there
now exists a static error for @js classes that have the same name as
a @Native class, the class is defined in a separate namespace. This CL
also refactors some of the code to avoid duplication and removes html
files in favor of `eval`.

Change-Id: I5cbba7e4b49ea726234fc86848638cac6ad8065b
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/176200
Commit-Queue: Srujan Gaddam <[email protected]>
Reviewed-by: Sigmund Cherem <[email protected]>
Reviewed-by: Stephen Adams <[email protected]>
  • Loading branch information
srujzs authored and [email protected] committed Dec 24, 2020
1 parent bc43d06 commit c7b6e6a
Show file tree
Hide file tree
Showing 20 changed files with 116 additions and 352 deletions.
19 changes: 2 additions & 17 deletions tests/lib/html/js_interop_constructor_name/div_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,14 @@
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

library jsTest;

import 'dart:async';
import 'dart:html' as html;
import 'dart:js';
import 'package:js/js.dart';

import 'package:expect/expect.dart' show NoInline, AssumeDynamic;
import 'package:expect/minitest.dart';

@JS()
external makeDiv(String text);

@JS()
class HTMLDivElement {
external String bar();
}

@pragma('dart2js:noInline')
@pragma('dart2js:assumeDynamic')
confuse(x) => x;
import 'util.dart';

main() {
setUpJS();
test('dom-is-dom', () {
var e = confuse(new html.DivElement());
expect(e is html.DivElement, isTrue);
Expand Down
22 changes: 0 additions & 22 deletions tests/lib/html/js_interop_constructor_name/div_test.html

This file was deleted.

19 changes: 2 additions & 17 deletions tests/lib/html/js_interop_constructor_name/error1_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,14 @@
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

library jsTest;

import 'dart:async';
import 'dart:html' as html;
import 'dart:js';
import 'package:js/js.dart';

import 'package:expect/expect.dart' show NoInline, AssumeDynamic;
import 'package:expect/minitest.dart';

@JS()
external makeDiv(String text);

@JS()
class HTMLDivElement {
external String bar();
}

@pragma('dart2js:noInline')
@pragma('dart2js:assumeDynamic')
confuse(x) => x;
import 'util.dart';

main() {
setUpJS();
test('dom-is-js', () {
var e = confuse(new html.DivElement());
// Currently, HTML types are not [JavaScriptObject]s. We could change that
Expand Down
22 changes: 0 additions & 22 deletions tests/lib/html/js_interop_constructor_name/error1_test.html

This file was deleted.

19 changes: 2 additions & 17 deletions tests/lib/html/js_interop_constructor_name/error2_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,14 @@
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

library jsTest;

import 'dart:async';
import 'dart:html' as html;
import 'dart:js';
import 'package:js/js.dart';

import 'package:expect/expect.dart' show NoInline, AssumeDynamic;
import 'package:expect/minitest.dart';

@JS()
external makeDiv(String text);

@JS()
class HTMLDivElement {
external String bar();
}

@pragma('dart2js:noInline')
@pragma('dart2js:assumeDynamic')
confuse(x) => x;
import 'util.dart';

main() {
setUpJS();
test('String-is-not-js', () {
var e = confuse('kombucha');
// A String should not be a JS interop type. The type test flags are added
Expand Down
22 changes: 0 additions & 22 deletions tests/lib/html/js_interop_constructor_name/error2_test.html

This file was deleted.

20 changes: 3 additions & 17 deletions tests/lib/html/js_interop_constructor_name/method_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,15 @@
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

library jsTest;

import 'dart:async';
import 'dart:html' as html;
import 'dart:js';
import 'package:js/js.dart';

import 'package:expect/expect.dart' show NoInline, AssumeDynamic, Expect;
import 'package:expect/expect.dart' show Expect;
import 'package:expect/minitest.dart';

@JS()
external makeDiv(String text);

@JS()
class HTMLDivElement {
external String bar();
}

@pragma('dart2js:noInline')
@pragma('dart2js:assumeDynamic')
confuse(x) => x;
import 'util.dart';

main() {
setUpJS();
test('js-call-js-method', () {
var e = confuse(makeDiv('hello'));
expect(e.bar(), equals('hello'));
Expand Down
22 changes: 0 additions & 22 deletions tests/lib/html/js_interop_constructor_name/method_test.html

This file was deleted.

20 changes: 0 additions & 20 deletions tests/lib/html/js_interop_constructor_name/test_js.js

This file was deleted.

49 changes: 49 additions & 0 deletions tests/lib/html/js_interop_constructor_name/util.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// Copyright (c) 2020, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

@JS()
library util;

import 'package:expect/expect.dart' show NoInline, AssumeDynamic;
import 'package:js/js.dart';

@JS()
external void eval(String code);

@JS()
external makeDiv(String text);

// Static error to name @JS class the same as a @Native class, so we use a
// namespace `Foo` to avoid conflicting with the native class.
@JS('Foo.HTMLDivElement')
class HTMLDivElement {
external String bar();
}

@pragma('dart2js:noInline')
@pragma('dart2js:assumeDynamic')
confuse(x) => x;

void setUpJS() {
eval(r"""
var Foo = {}
// A constructor function with the same name as a HTML element.
Foo.HTMLDivElement = function(a) {
this.a = a;
}
Foo.HTMLDivElement.prototype.bar = function() {
return this.a;
}
Foo.HTMLDivElement.prototype.toString = function() {
return "HTMLDivElement(" + this.a + ")";
}
self.makeDiv = function(text) {
return new Foo.HTMLDivElement(text);
}
""");
}
19 changes: 2 additions & 17 deletions tests/lib_2/html/js_interop_constructor_name/div_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,14 @@
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

library jsTest;

import 'dart:async';
import 'dart:html' as html;
import 'dart:js';
import 'package:js/js.dart';

import 'package:expect/expect.dart' show NoInline, AssumeDynamic;
import 'package:expect/minitest.dart';

@JS()
external makeDiv(String text);

@JS()
class HTMLDivElement {
external String bar();
}

@pragma('dart2js:noInline')
@pragma('dart2js:assumeDynamic')
confuse(x) => x;
import 'util.dart';

main() {
setUpJS();
test('dom-is-dom', () {
var e = confuse(new html.DivElement());
expect(e is html.DivElement, isTrue);
Expand Down
22 changes: 0 additions & 22 deletions tests/lib_2/html/js_interop_constructor_name/div_test.html

This file was deleted.

19 changes: 2 additions & 17 deletions tests/lib_2/html/js_interop_constructor_name/error1_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,14 @@
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

library jsTest;

import 'dart:async';
import 'dart:html' as html;
import 'dart:js';
import 'package:js/js.dart';

import 'package:expect/expect.dart' show NoInline, AssumeDynamic;
import 'package:expect/minitest.dart';

@JS()
external makeDiv(String text);

@JS()
class HTMLDivElement {
external String bar();
}

@pragma('dart2js:noInline')
@pragma('dart2js:assumeDynamic')
confuse(x) => x;
import 'util.dart';

main() {
setUpJS();
test('dom-is-js', () {
var e = confuse(new html.DivElement());
// Currently, HTML types are not [JavaScriptObject]s. We could change that
Expand Down
Loading

0 comments on commit c7b6e6a

Please sign in to comment.