Skip to content

Commit 9461df2

Browse files
kallentucommit-bot@chromium.org
authored andcommitted
Language tests for variance modifiers.
These test basic correct usages of variance modifiers in fields and methods. Erroneous usages will come in a future CL, as well as more complex examples + subclassing. Change-Id: I05d547ac1db9e22235a0bf64b05fcb3e119e3157 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/119169 Commit-Queue: Kallen Tu <kallentu@google.com> Reviewed-by: Leaf Petersen <leafp@google.com> Reviewed-by: Erik Ernst <eernst@google.com>
1 parent 418b646 commit 9461df2

File tree

6 files changed

+621
-0
lines changed

6 files changed

+621
-0
lines changed
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
// Copyright (c) 2019, 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 various fields for the `in` variance modifier.
6+
7+
// SharedOptions=--enable-experiment=variance
8+
9+
import "package:expect/expect.dart";
10+
11+
typedef Int2Void = void Function(int);
12+
13+
class A<in T> {
14+
void set a(T value) => value;
15+
final void Function(T) b = (T val) {
16+
Expect.equals(2, val);
17+
};
18+
A<T> get c => this;
19+
}
20+
21+
mixin BMixin<in T> {
22+
void set a(T value) => value;
23+
final void Function(T) b = (T val) {
24+
Expect.equals(2, val);
25+
};
26+
BMixin<T> get c => this;
27+
}
28+
29+
class B with BMixin<int> {}
30+
31+
void testClass() {
32+
A<int> a = new A();
33+
34+
a.a = 2;
35+
36+
Expect.type<Int2Void>(a.b);
37+
a.b(2);
38+
39+
a.c.a = 2;
40+
}
41+
42+
void testMixin() {
43+
B b = new B();
44+
45+
b.a = 2;
46+
47+
Expect.type<Int2Void>(b.b);
48+
b.b(2);
49+
50+
b.c.a = 2;
51+
}
52+
53+
main() {
54+
testClass();
55+
testMixin();
56+
}
Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
// Copyright (c) 2019, 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 method signatures and return types for the `in` variance modifier.
6+
7+
// SharedOptions=--enable-experiment=variance
8+
9+
import "package:expect/expect.dart";
10+
11+
typedef Cov<T> = T Function();
12+
typedef Contra<T> = void Function(T);
13+
14+
class A<in T> {
15+
void method1(T x) {}
16+
void method2(Cov<T> x) {}
17+
Contra<T> method3() {
18+
return (T val) {
19+
Expect.equals(2, val);
20+
};
21+
}
22+
23+
void method4(Cov<Cov<T>> x) {}
24+
Contra<Cov<T>> method5() {
25+
return (Cov<T> method) {
26+
Expect.type<Cov<T>>(method);
27+
};
28+
}
29+
Cov<Contra<T>> method6() {
30+
return () {
31+
return (T x) {
32+
Expect.equals(2, x);
33+
};
34+
};
35+
}
36+
void method7(Contra<Contra<T>> x) {}
37+
}
38+
39+
mixin BMixin<in T> {
40+
void method1(T x) {}
41+
void method2(Cov<T> x) {}
42+
Contra<T> method3() {
43+
return (T val) {
44+
Expect.equals(2, val);
45+
};
46+
}
47+
48+
void method4(Cov<Cov<T>> x) {}
49+
Contra<Cov<T>> method5() {
50+
return (Cov<T> method) {
51+
Expect.type<Cov<T>>(method);
52+
};
53+
}
54+
Cov<Contra<T>> method6() {
55+
return () {
56+
return (T x) {
57+
Expect.equals(2, x);
58+
};
59+
};
60+
}
61+
void method7(Contra<Contra<T>> x) {}
62+
}
63+
64+
class B with BMixin<int> {}
65+
66+
class C<in T> {
67+
void method1(Contra<A<T>> x) {}
68+
A<T> method2() {
69+
return A<T>();
70+
}
71+
}
72+
73+
void testClass() {
74+
A<int> a = new A();
75+
76+
a.method1(2);
77+
78+
a.method2(() => 2);
79+
80+
Expect.type<Contra<int>>(a.method3());
81+
Contra<int> method3Function = a.method3();
82+
method3Function(2);
83+
84+
a.method4(() {
85+
return () => 2;
86+
});
87+
88+
Expect.type<Contra<Cov<int>>>(a.method5());
89+
Contra<Cov<int>> method5Function = a.method5();
90+
method5Function(() => 2);
91+
92+
Expect.type<Cov<Contra<int>>>(a.method6());
93+
Cov<Contra<int>> method6Function = a.method6();
94+
Expect.type<Contra<int>>(method6Function());
95+
Contra<int> method6NestedFunction = method6Function();
96+
method6NestedFunction(2);
97+
98+
a.method7((Contra<int> x) {});
99+
}
100+
101+
void testMixin() {
102+
B b = new B();
103+
104+
b.method1(2);
105+
106+
b.method2(() => 2);
107+
108+
Expect.type<Contra<int>>(b.method3());
109+
Contra<int> method3Return = b.method3();
110+
method3Return(2);
111+
112+
b.method4(() {
113+
return () => 2;
114+
});
115+
116+
Expect.type<Contra<Cov<int>>>(b.method5());
117+
Contra<Cov<int>> method5Return = b.method5();
118+
method5Return(() => 2);
119+
120+
Expect.type<Cov<Contra<int>>>(b.method6());
121+
Cov<Contra<int>> method6Function = b.method6();
122+
Expect.type<Contra<int>>(method6Function());
123+
Contra<int> method6NestedFunction = method6Function();
124+
method6NestedFunction(2);
125+
126+
b.method7((Contra<int> x) {});
127+
}
128+
129+
void testClassInMethods() {
130+
C<int> c = new C();
131+
132+
c.method1((A<int> x) {});
133+
134+
Expect.type<A<int>>(c.method2());
135+
}
136+
137+
main() {
138+
testClass();
139+
testMixin();
140+
testClassInMethods();
141+
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
// Copyright (c) 2019, 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 various fields for the `inout` variance modifier.
6+
7+
// SharedOptions=--enable-experiment=variance
8+
9+
import "package:expect/expect.dart";
10+
11+
typedef Void2Int = int Function();
12+
typedef Int2Void = void Function(int);
13+
14+
class A<inout T> {
15+
T a;
16+
final T b = null;
17+
final T Function() c = () => null;
18+
final void Function(T) d = (T val) {
19+
Expect.equals(2, val);
20+
};
21+
A<T> get e => this;
22+
}
23+
24+
mixin BMixin<inout T> {
25+
T a;
26+
final T b = null;
27+
final T Function() c = () => null;
28+
final void Function(T) d = (T val) {
29+
Expect.equals(2, val);
30+
};
31+
BMixin<T> get e => this;
32+
}
33+
34+
class B with BMixin<int> {}
35+
36+
void testClass() {
37+
A<int> a = new A();
38+
39+
a.a = 2;
40+
Expect.equals(2, a.a);
41+
42+
Expect.isNull(a.b);
43+
44+
Expect.type<Void2Int>(a.c);
45+
Expect.isNull(a.c());
46+
47+
Expect.type<Int2Void>(a.d);
48+
a.d(2);
49+
50+
a.e.a = 3;
51+
}
52+
53+
void testMixin() {
54+
B b = new B();
55+
56+
b.a = 2;
57+
Expect.equals(2, b.a);
58+
59+
Expect.isNull(b.b);
60+
61+
Expect.type<Void2Int>(b.c);
62+
Expect.isNull(b.c());
63+
64+
Expect.type<Int2Void>(b.d);
65+
b.d(2);
66+
67+
b.e.a = 3;
68+
}
69+
70+
main() {
71+
testClass();
72+
testMixin();
73+
}

0 commit comments

Comments
 (0)