Skip to content

Commit 5a28f14

Browse files
srujzscommit-bot@chromium.org
authored andcommitted
[package:js] Add test for inheritance between classes
Adds a test to verify that JS interop and anonymous classes can properly extend one another and that member access behaves as expected. Change-Id: I98b77444363ec3f985a9a10f3716e15a7fd970f5 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/164882 Reviewed-by: Sigmund Cherem <[email protected]>
1 parent 00a28b5 commit 5a28f14

File tree

3 files changed

+205
-0
lines changed

3 files changed

+205
-0
lines changed
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
// Copyright (c) 2020, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
5+
import 'extends_test_util.dart';
6+
7+
void main() {
8+
// Use the old way to define inheritance between JS objects.
9+
eval(r"""
10+
function inherits(child, parent) {
11+
if (child.prototype.__proto__) {
12+
child.prototype.__proto__ = parent.prototype;
13+
} else {
14+
function tmp() {};
15+
tmp.prototype = parent.prototype;
16+
child.prototype = new tmp();
17+
child.prototype.constructor = child;
18+
}
19+
}
20+
function JSClass(a) {
21+
this.a = a;
22+
this.getA = function() {
23+
return this.a;
24+
}
25+
this.getAOrB = function() {
26+
return this.getA();
27+
}
28+
}
29+
function JSExtendJSClass(a, b) {
30+
JSClass.call(this, a);
31+
this.b = b;
32+
this.getB = function() {
33+
return this.b;
34+
}
35+
this.getAOrB = function() {
36+
return this.getB();
37+
}
38+
}
39+
inherits(JSExtendJSClass, JSClass);
40+
function JSExtendAnonymousClass(a, b) {
41+
this.a = a;
42+
this.b = b;
43+
this.getA = function() {
44+
return this.a;
45+
}
46+
this.getB = function() {
47+
return this.b;
48+
}
49+
this.getAOrB = function() {
50+
return this.getB();
51+
}
52+
}
53+
self.anonExtendAnon = new JSExtendAnonymousClass(1, 2);
54+
self.anonExtendJS = new JSExtendJSClass(1, 2);
55+
""");
56+
testInheritance();
57+
}
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
// Copyright (c) 2020, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
5+
// Tests inheritance relationships between `JS` and `anonymous` classes/objects.
6+
7+
@JS()
8+
library extends_test;
9+
10+
import 'package:expect/minitest.dart';
11+
import 'package:js/js.dart';
12+
13+
@JS()
14+
external void eval(String code);
15+
16+
@JS()
17+
class JSClass {
18+
external int get a;
19+
external int getA();
20+
external int getAOrB();
21+
}
22+
23+
@JS()
24+
@anonymous
25+
class AnonymousClass {
26+
external int get a;
27+
external int getA();
28+
}
29+
30+
@JS()
31+
class JSExtendJSClass extends JSClass {
32+
external JSExtendJSClass(int a, int b);
33+
external int get b;
34+
external int getB();
35+
external int getAOrB();
36+
}
37+
38+
@JS()
39+
class JSExtendAnonymousClass extends AnonymousClass {
40+
external JSExtendAnonymousClass(int a, int b);
41+
external int get b;
42+
external int getB();
43+
}
44+
45+
@JS()
46+
@anonymous
47+
class AnonymousExtendAnonymousClass extends AnonymousClass {
48+
external int get b;
49+
external int getB();
50+
}
51+
52+
@JS()
53+
@anonymous
54+
class AnonymousExtendJSClass extends JSClass {
55+
external int get b;
56+
external int getB();
57+
external int getAOrB();
58+
}
59+
60+
external AnonymousExtendAnonymousClass get anonExtendAnon;
61+
external AnonymousExtendJSClass get anonExtendJS;
62+
63+
void testInheritance() {
64+
// Note that for the following, there are no meaningful tests for is checks or
65+
// as casts, since the web compilers should return true and succeed for all JS
66+
// types.
67+
68+
var jsExtendJS = JSExtendJSClass(1, 2);
69+
expect(jsExtendJS.a, 1);
70+
expect(jsExtendJS.b, 2);
71+
expect(jsExtendJS.getA(), 1);
72+
expect(jsExtendJS.getB(), 2);
73+
// Test method overrides.
74+
expect(jsExtendJS.getAOrB(), 2);
75+
expect((jsExtendJS as JSClass).getAOrB(), 2);
76+
77+
var jsExtendAnon = JSExtendAnonymousClass(1, 2);
78+
expect(jsExtendAnon.a, 1);
79+
expect(jsExtendAnon.b, 2);
80+
expect(jsExtendAnon.getA(), 1);
81+
expect(jsExtendAnon.getB(), 2);
82+
83+
expect(anonExtendAnon.a, 1);
84+
expect(anonExtendAnon.b, 2);
85+
expect(anonExtendAnon.getA(), 1);
86+
expect(anonExtendAnon.getB(), 2);
87+
88+
expect(anonExtendJS.a, 1);
89+
expect(anonExtendJS.b, 2);
90+
expect(anonExtendJS.getA(), 1);
91+
expect(anonExtendJS.getB(), 2);
92+
expect(anonExtendJS.getAOrB(), 2);
93+
expect((anonExtendJS as JSClass).getAOrB(), 2);
94+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
// Copyright (c) 2020, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
5+
import 'extends_test_util.dart';
6+
7+
void main() {
8+
// Use the ES6 syntax for classes to make inheritance easier.
9+
eval(r"""
10+
class JSClass {
11+
constructor(a) {
12+
this.a = a;
13+
}
14+
getA() {
15+
return this.a;
16+
}
17+
getAOrB() {
18+
return this.getA();
19+
}
20+
}
21+
class JSExtendJSClass extends JSClass {
22+
constructor(a, b) {
23+
super(a);
24+
this.b = b;
25+
}
26+
getB() {
27+
return this.b;
28+
}
29+
getAOrB() {
30+
return this.getB();
31+
}
32+
}
33+
self.JSExtendJSClass = JSExtendJSClass;
34+
class JSExtendAnonymousClass {
35+
constructor(a, b) {
36+
this.a = a;
37+
this.b = b;
38+
}
39+
getA() {
40+
return this.a;
41+
}
42+
getB() {
43+
return this.b;
44+
}
45+
getAOrB() {
46+
return this.getB();
47+
}
48+
}
49+
self.JSExtendAnonymousClass = JSExtendAnonymousClass;
50+
self.anonExtendAnon = new JSExtendAnonymousClass(1, 2);
51+
self.anonExtendJS = new JSExtendJSClass(1, 2);
52+
""");
53+
testInheritance();
54+
}

0 commit comments

Comments
 (0)