Skip to content

Commit dbb793a

Browse files
srujzsCommit Queue
authored and
Commit Queue
committed
[pkg:js] Refactor createStaticInteropMock tests to account for new changes
Change-Id: I4de0c94bf1f00336432d61b174703fde9476545a Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/262863 Reviewed-by: Riley Porter <[email protected]> Reviewed-by: Sigmund Cherem <[email protected]> Commit-Queue: Srujan Gaddam <[email protected]>
1 parent 8c62ad7 commit dbb793a

16 files changed

+508
-788
lines changed

tests/lib/js/export/extension_conflict_test.dart

Lines changed: 67 additions & 141 deletions
Original file line numberDiff line numberDiff line change
@@ -3,176 +3,102 @@
33
// BSD-style license that can be found in the LICENSE file.
44

55
// Test that `createStaticInteropMock` checks for extension member conflicts.
6+
// We should only require users to implement one of these conflicts (or a
7+
// getter/setter pair).
68

79
import 'package:js/js.dart';
810
import 'package:js/js_util.dart';
911

10-
class EmptyDart {}
11-
1212
@JS()
1313
@staticInterop
14-
class Method {}
14+
class SameKindConflict {}
1515

16-
extension on Method {
17-
external void member();
16+
extension E1 on SameKindConflict {
17+
external int get getter;
18+
external set setter(int val);
19+
external int method();
1820
}
1921

20-
@JS()
21-
@staticInterop
22-
class Getter {}
23-
24-
extension NamedExtension on Getter {
25-
external int get member;
22+
extension E2 on SameKindConflict {
23+
external String get getter;
24+
external set setter(int val);
25+
external String method();
2626
}
2727

28-
@JS()
29-
@staticInterop
30-
class Field {}
31-
32-
extension on Field {
33-
external final String member;
34-
}
35-
36-
@JS()
37-
@staticInterop
38-
class ExtendsImplementsConflict extends Method implements Getter {}
39-
40-
@JS()
41-
@staticInterop
42-
class ImplementsConflict implements Method, Getter {}
43-
44-
@JS()
45-
@staticInterop
46-
class ManyConflicts extends Method implements Getter, Field {}
47-
48-
@JS()
49-
@staticInterop
50-
class Override implements Method {}
51-
52-
extension on Override {
53-
external int member();
28+
@JSExport()
29+
class DartSameKindConflict {
30+
String getter = '';
31+
set setter(int val) => throw '';
32+
String method() => throw '';
5433
}
5534

56-
@JS()
57-
@staticInterop
58-
class OverrideOneConflictButNotAll implements Override, Getter {}
59-
60-
@JS()
61-
@staticInterop
62-
class ConflictThroughInheritance implements OverrideOneConflictButNotAll {}
63-
64-
@JS()
65-
@staticInterop
66-
class ResolveThroughOverride implements ConflictThroughInheritance {}
67-
68-
extension on ResolveThroughOverride {
69-
external int member;
35+
@JSExport()
36+
class IncorrectDartSameKindConflict {
37+
bool getter = true;
38+
set setter(bool val) => throw '';
39+
bool method() => throw '';
7040
}
7141

72-
class ResolveThroughOverrideDart {
73-
int member = throw '';
42+
void testSameKindConflict() {
43+
// No error as one of the extension members are implemented for each export
44+
// name.
45+
createStaticInteropMock<SameKindConflict, DartSameKindConflict>(
46+
DartSameKindConflict());
47+
// Error as none of them are implemented for each export name.
48+
createStaticInteropMock<SameKindConflict, IncorrectDartSameKindConflict>(
49+
//^
50+
// [web] Dart class 'IncorrectDartSameKindConflict' does not have any members that implement any of the following extension member(s) with export name 'getter': E1.getter (FunctionType(int Function())), E2.getter (FunctionType(String Function())).
51+
// [web] Dart class 'IncorrectDartSameKindConflict' does not have any members that implement any of the following extension member(s) with export name 'method': E1.method (FunctionType(int Function())), E2.method (FunctionType(String Function())).
52+
// [web] Dart class 'IncorrectDartSameKindConflict' does not have any members that implement any of the following extension member(s) with export name 'setter': E1.setter= (FunctionType(void Function(int))), E2.setter= (FunctionType(void Function(int))).
53+
IncorrectDartSameKindConflict());
7454
}
7555

7656
@JS()
7757
@staticInterop
78-
class Setter {}
58+
class DifferentKindConflict {}
7959

80-
extension on Setter {
81-
external set member(int val);
60+
extension E3 on DifferentKindConflict {
61+
external int getSet;
62+
@JS('getSet')
63+
external void method();
8264
}
8365

84-
@JS()
85-
@staticInterop
86-
class NoConflictDueToSubtype implements Override, Method {}
87-
88-
class NoConflictDueToSubtypeDart {
89-
int member() => throw '';
66+
@JSExport()
67+
class ImplementGetter {
68+
int get getSet => throw '';
9069
}
9170

92-
@JS()
93-
@staticInterop
94-
class GetterSetterConflict implements Setter, Getter {}
95-
96-
@JS()
97-
@staticInterop
98-
class GetterSetterSameExtension {}
99-
100-
extension on GetterSetterSameExtension {
101-
external int get member;
102-
external set member(int val);
71+
@JSExport()
72+
class ImplementSetter {
73+
set getSet(int val) => throw '';
10374
}
10475

105-
class GetterSetterSameExtensionDart extends ResolveThroughOverrideDart {}
106-
107-
@JS()
108-
@staticInterop
109-
class GetterSetterMethodConflict implements GetterSetterSameExtension, Method {}
110-
111-
@JS()
112-
@staticInterop
113-
class NonExternal {}
114-
115-
extension on NonExternal {
116-
int get member => throw '';
76+
@JSExport()
77+
class ImplementBoth {
78+
int getSet = 0;
11779
}
11880

119-
@JS()
120-
@staticInterop
121-
class ExternalNonExternal implements NonExternal, GetterSetterSameExtension {}
122-
123-
class ExternalNonExternalDart extends ResolveThroughOverrideDart {}
124-
125-
void main() {
126-
// Test name conflicts between extended and implemented members.
127-
createStaticInteropMock<ExtendsImplementsConflict, EmptyDart>(
128-
//^
129-
// [web] External extension member with name 'member' is defined in the following extensions and none are more specific: 'Getter.NamedExtension', 'Method.unnamed'.
130-
EmptyDart());
131-
// Test name conflicts between implemented members.
132-
createStaticInteropMock<ImplementsConflict, EmptyDart>(EmptyDart());
133-
//^
134-
// [web] External extension member with name 'member' is defined in the following extensions and none are more specific: 'Getter.NamedExtension', 'Method.unnamed'.
135-
136-
// Test multiple name conflicts.
137-
createStaticInteropMock<ManyConflicts, EmptyDart>(EmptyDart());
138-
//^
139-
// [web] External extension member with name 'member' is defined in the following extensions and none are more specific: 'Field.unnamed', 'Getter.NamedExtension', 'Method.unnamed'.
81+
@JSExport()
82+
class ImplementMethod {
83+
void getSet() {}
84+
}
14085

141-
// Test name conflicts where one definition is overridden, but there is still
142-
// a name conflict between the other two.
143-
createStaticInteropMock<OverrideOneConflictButNotAll, EmptyDart>(
144-
//^
145-
// [web] External extension member with name 'member' is defined in the following extensions and none are more specific: 'Getter.NamedExtension', 'Override.unnamed'.
146-
EmptyDart());
147-
// Test case where if we inherit a class with a conflict, the conflict still
148-
// exists.
149-
createStaticInteropMock<ConflictThroughInheritance, EmptyDart>(
86+
void testDifferentKindConflict() {
87+
// Missing setter error.
88+
createStaticInteropMock<DifferentKindConflict, ImplementGetter>(
15089
//^
151-
// [web] External extension member with name 'member' is defined in the following extensions and none are more specific: 'Getter.NamedExtension', 'Override.unnamed'.
152-
EmptyDart());
153-
// Test case where name conflicts are resolved using derived class.
154-
createStaticInteropMock<ResolveThroughOverride, ResolveThroughOverrideDart>(
155-
ResolveThroughOverrideDart());
156-
// Test case where you inherit two classes with the same member name but they
157-
// have a subtype relation, so there is no conflict.
158-
createStaticInteropMock<NoConflictDueToSubtype, NoConflictDueToSubtypeDart>(
159-
NoConflictDueToSubtypeDart());
160-
// Test conflict where getter and setter collide when they are in different
161-
// extensions.
162-
createStaticInteropMock<GetterSetterConflict, EmptyDart>(EmptyDart());
90+
// [web] Dart class 'ImplementGetter' has a getter, but does not have a setter to implement any of the following extension member(s) with export name 'getSet': E3.getSet= (FunctionType(void Function(int))).
91+
ImplementGetter());
92+
// Missing getter error.
93+
createStaticInteropMock<DifferentKindConflict, ImplementSetter>(
16394
//^
164-
// [web] External extension member with name 'member' is defined in the following extensions and none are more specific: 'Getter.NamedExtension', 'Setter.unnamed'.
165-
166-
// Test no conflict where getter and setter are on the same extension.
167-
createStaticInteropMock<GetterSetterSameExtension,
168-
GetterSetterSameExtensionDart>(GetterSetterSameExtensionDart());
169-
// Test conflict where getter and setter are in one extension, but there is
170-
// a conflict with another extension.
171-
createStaticInteropMock<GetterSetterMethodConflict, EmptyDart>(EmptyDart());
172-
//^
173-
// [web] External extension member with name 'member' is defined in the following extensions and none are more specific: 'GetterSetterSameExtension.unnamed', 'Method.unnamed'.
174-
175-
// Test no conflict between external and non-external members.
176-
createStaticInteropMock<ExternalNonExternal, ExternalNonExternalDart>(
177-
ExternalNonExternalDart());
95+
// [web] Dart class 'ImplementSetter' has a setter, but does not have a getter to implement any of the following extension member(s) with export name 'getSet': E3.getSet (FunctionType(int Function())).
96+
ImplementSetter());
97+
// No error as both getter and setter are there, and we've satisfied an export
98+
// for `getSet`.
99+
createStaticInteropMock<DifferentKindConflict, ImplementBoth>(
100+
ImplementBoth());
101+
// No error as we've satisfied an export for `getSet`.
102+
createStaticInteropMock<DifferentKindConflict, ImplementMethod>(
103+
ImplementMethod());
178104
}

tests/lib/js/export/functional_test_lib.dart

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,11 @@ extension on Methods {
2323
external int optionalAdd(int a, int b, [int c = 0, int? d]);
2424
}
2525

26+
@JSExport()
2627
class MethodsDart {
2728
int add(int a, int b) => a + b;
2829
int nonExternal() => 1;
29-
int rename() => 1;
30+
int _rename() => 1;
3031
int optionalAdd(int a, int b, [int? c, int? d]) =>
3132
a + b + (c ?? 0) + (d ?? 0);
3233
}
@@ -44,11 +45,12 @@ extension on Fields {
4445
external final int renamedFinalField;
4546
}
4647

48+
@JSExport()
4749
class FieldsDart {
4850
int field = 1;
4951
int finalField = 1;
50-
int renamedField = 1;
51-
final int renamedFinalField = 1;
52+
int _renamedField = 1;
53+
final int _renamedFinalField = 1;
5254
}
5355

5456
@JS()
@@ -72,12 +74,13 @@ extension on GetSet {
7274
external set differentNameSameRenameSet(int val);
7375
}
7476

77+
@JSExport()
7578
class GetSetDart {
7679
int getSet = 1;
77-
int renamedGetSet = 1;
78-
int sameNameDifferentRename = 1;
79-
int differentNameSameRenameGet = 1;
80-
int differentNameSameRenameSet = 1;
80+
int _renamedGetSet = 1;
81+
int _sameNameDifferentRenameGet = 1;
82+
int _sameNameDifferentRenameSet = 1;
83+
int _differentNameSameRename = 1;
8184
}
8285

8386
void test([Object? proto]) {
@@ -99,11 +102,11 @@ void test([Object? proto]) {
99102
jsFields.field = 2;
100103
jsFields.renamedField = 2;
101104
expect(dartFields.field, 2);
102-
expect(dartFields.renamedField, 2);
105+
expect(dartFields._renamedField, 2);
103106
// Modify the Dart mock and check for updates in the JS mock.
104107
dartFields.field = 3;
105108
dartFields.finalField = 3;
106-
dartFields.renamedField = 3;
109+
dartFields._renamedField = 3;
107110
expect(jsFields.field, 3);
108111
expect(jsFields.finalField, 3);
109112
expect(jsFields.renamedField, 3);
@@ -119,17 +122,17 @@ void test([Object? proto]) {
119122
jsGetSet.sameNameDifferentRename = 2;
120123
jsGetSet.differentNameSameRenameSet = 2;
121124
expect(dartGetSet.getSet, 2);
122-
expect(dartGetSet.renamedGetSet, 2);
123-
expect(dartGetSet.sameNameDifferentRename, 2);
124-
expect(dartGetSet.differentNameSameRenameGet, 1);
125-
expect(dartGetSet.differentNameSameRenameSet, 2);
125+
expect(dartGetSet._renamedGetSet, 2);
126+
expect(dartGetSet._sameNameDifferentRenameGet, 1);
127+
expect(dartGetSet._sameNameDifferentRenameSet, 2);
128+
expect(dartGetSet._differentNameSameRename, 2);
126129
// Modify the Dart mock and check for updates in the JS mock.
127130
dartGetSet.getSet = 3;
128-
dartGetSet.renamedGetSet = 3;
129-
dartGetSet.sameNameDifferentRename = 3;
131+
dartGetSet._renamedGetSet = 3;
130132
// Use different values to disambiguate.
131-
dartGetSet.differentNameSameRenameGet = 3;
132-
dartGetSet.differentNameSameRenameSet = 4;
133+
dartGetSet._sameNameDifferentRenameGet = 3;
134+
dartGetSet._sameNameDifferentRenameSet = 4;
135+
dartGetSet._differentNameSameRename = 3;
133136
expect(jsGetSet.getSet, 3);
134137
expect(jsGetSet.renamedGetSet, 3);
135138
expect(jsGetSet.sameNameDifferentRename, 3);

tests/lib/js/export/incorrect_type_arguments_test.dart

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,26 +24,29 @@ class StaticInterop {
2424
external factory StaticInterop();
2525
}
2626

27-
class Dart {}
27+
@JSExport()
28+
class Dart {
29+
int _unused = 0;
30+
}
2831

2932
void main() {
3033
createStaticInteropMock<StaticInterop, Dart>(Dart());
3134
createStaticInteropMock<Dart, StaticInterop>(StaticInterop());
3235
//^
33-
// [web] First type argument 'Dart' is not a `@staticInterop` type.
34-
// [web] Second type argument 'StaticInterop' is not a Dart interface type.
36+
// [web] Type argument 'Dart' needs to be a `@staticInterop` type.
37+
// [web] Type argument 'StaticInterop' needs to be a non-JS interop type.
3538
createStaticInteropMock<Dart, Js>(Js());
3639
//^
37-
// [web] First type argument 'Dart' is not a `@staticInterop` type.
38-
// [web] Second type argument 'Js' is not a Dart interface type.
40+
// [web] Type argument 'Dart' needs to be a `@staticInterop` type.
41+
// [web] Type argument 'Js' needs to be a non-JS interop type.
3942
createStaticInteropMock<Dart, Anonymous>(Anonymous());
4043
//^
41-
// [web] First type argument 'Dart' is not a `@staticInterop` type.
42-
// [web] Second type argument 'Anonymous' is not a Dart interface type.
44+
// [web] Type argument 'Anonymous' needs to be a non-JS interop type.
45+
// [web] Type argument 'Dart' needs to be a `@staticInterop` type.
4346
createStaticInteropMock<StaticInterop, void Function()>(() {});
4447
//^
45-
// [web] Second type argument 'void Function()' is not a Dart interface type.
48+
// [web] Type argument 'void Function()' needs to be an interface type.
4649
createStaticInteropMock(Dart());
4750
//^
48-
// [web] First type argument 'dynamic' is not a `@staticInterop` type.
51+
// [web] Type argument 'Object' needs to be a `@staticInterop` type.
4952
}

0 commit comments

Comments
 (0)