Skip to content

Commit 6f4bc6a

Browse files
lrhncommit-bot@chromium.org
authored andcommitted
Add test for mixin application constructor forwarding of default values.
Bug: http://drtbug.com/31767 Change-Id: I25a877244d588b642c1c027caee381cfa847c07d Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/120644 Commit-Queue: Lasse R.H. Nielsen <[email protected]> Reviewed-by: Johnni Winther <[email protected]>
1 parent 9be1160 commit 6f4bc6a

File tree

4 files changed

+200
-0
lines changed

4 files changed

+200
-0
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
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+
import "mixin_constructor_parameter_forwarding_test.dart";
6+
7+
// A private class that the mixin application cannot access syntactically,
8+
// yet it needs an instance of it for the default value.
9+
class _Private {
10+
const _Private();
11+
}
12+
13+
class B2<T> implements B<T> {
14+
final T x;
15+
final Object y;
16+
const B2(T x, [Object y = const _Private()])
17+
: x = x,
18+
y = y;
19+
}
20+
21+
// Leaking the constant value in a non-constant way which cannot be used
22+
// for the default value of the mixin application.
23+
Object get privateValue => const _Private();
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
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 that mixin application forwarding constructors correctly forward
6+
// optional parameter default values.
7+
8+
import "package:expect/expect.dart";
9+
10+
import "mixin_constructor_parameter_forwarding_helper.dart"
11+
show B2, privateValue;
12+
13+
class B<T> {
14+
final T x;
15+
final Object y;
16+
const B(T x, [Object y = 0])
17+
: x = x,
18+
y = y;
19+
const B.b1(T x, {Object y = 0})
20+
: x = x,
21+
y = y;
22+
const B.b2(T x, [Object y = const <int>[0]])
23+
: x = x,
24+
y = y;
25+
}
26+
27+
mixin M1 on B<int> {
28+
void check(int x, Object y) {
29+
Expect.identical(x, this.x);
30+
Expect.identical(y, this.y);
31+
}
32+
}
33+
34+
mixin M2<T> on B<T> {
35+
void check(T x, Object y) {
36+
Expect.identical(x, this.x);
37+
Expect.identical(y, this.y);
38+
}
39+
}
40+
41+
class A1 = B<int> with M1;
42+
class A2 = B<int> with M2<int>;
43+
class P1 = B2<int> with M2<int>;
44+
45+
main() {
46+
A1(1, 2).check(1, 2);
47+
A1.b1(1, y: 2).check(1, 2);
48+
A1.b2(1, 2).check(1, 2);
49+
A2(1, 2).check(1, 2);
50+
A2.b1(1, y: 2).check(1, 2);
51+
A2.b2(1, 2).check(1, 2);
52+
P1(1, 2).check(1, 2);
53+
54+
A1(1).check(1, 0);
55+
A1.b1(1).check(1, 0);
56+
A1.b2(1).check(1, const <int>[0]);
57+
A2(1).check(1, 0);
58+
A2.b1(1).check(1, 0);
59+
A2.b2(1).check(1, const <int>[0]);
60+
P1(1).check(1, privateValue);
61+
62+
const A1(1, 2).check(1, 2);
63+
const A1.b1(1, y: 2).check(1, 2);
64+
const A1.b2(1, 2).check(1, 2);
65+
const A2(1, 2).check(1, 2);
66+
const A2.b1(1, y: 2).check(1, 2);
67+
const A2.b2(1, 2).check(1, 2);
68+
const P1(1, 2).check(1, 2);
69+
70+
const A1(1).check(1, 0);
71+
const A1.b1(1).check(1, 0);
72+
const A1.b2(1).check(1, const <int>[0]);
73+
const A2(1).check(1, 0);
74+
const A2.b1(1).check(1, 0);
75+
const A2.b2(1).check(1, const <int>[0]);
76+
const P1(1).check(1, privateValue);
77+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
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+
import "mixin_constructor_parameter_forwarding_test.dart";
6+
7+
// A private class that the mixin application cannot access syntactically,
8+
// yet it needs an instance of it for the default value.
9+
class _Private {
10+
const _Private();
11+
}
12+
13+
class B2<T> implements B<T> {
14+
final T x;
15+
final Object y;
16+
const B2(T x, [Object y = const _Private()])
17+
: x = x,
18+
y = y;
19+
}
20+
21+
// Leaking the constant value in a non-constant way which cannot be used
22+
// for the default value of the mixin application.
23+
Object get privateValue => const _Private();
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
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 that mixin application forwarding constructors correctly forward
6+
// optional parameter default values.
7+
8+
import "package:expect/expect.dart";
9+
10+
import "mixin_constructor_parameter_forwarding_helper.dart"
11+
show B2, privateValue;
12+
13+
class B<T> {
14+
final T x;
15+
final Object y;
16+
const B(T x, [Object y = 0])
17+
: x = x,
18+
y = y;
19+
const B.b1(T x, {Object y = 0})
20+
: x = x,
21+
y = y;
22+
const B.b2(T x, [Object y = const <int>[0]])
23+
: x = x,
24+
y = y;
25+
}
26+
27+
mixin M1 on B<int> {
28+
void check(int x, Object y) {
29+
Expect.identical(x, this.x);
30+
Expect.identical(y, this.y);
31+
}
32+
}
33+
34+
mixin M2<T> on B<T> {
35+
void check(T x, Object y) {
36+
Expect.identical(x, this.x);
37+
Expect.identical(y, this.y);
38+
}
39+
}
40+
41+
class A1 = B<int> with M1;
42+
class A2 = B<int> with M2<int>;
43+
class P1 = B2<int> with M2<int>;
44+
45+
main() {
46+
A1(1, 2).check(1, 2);
47+
A1.b1(1, y: 2).check(1, 2);
48+
A1.b2(1, 2).check(1, 2);
49+
A2(1, 2).check(1, 2);
50+
A2.b1(1, y: 2).check(1, 2);
51+
A2.b2(1, 2).check(1, 2);
52+
P1(1, 2).check(1, 2);
53+
54+
A1(1).check(1, 0);
55+
A1.b1(1).check(1, 0);
56+
A1.b2(1).check(1, const <int>[0]);
57+
A2(1).check(1, 0);
58+
A2.b1(1).check(1, 0);
59+
A2.b2(1).check(1, const <int>[0]);
60+
P1(1).check(1, privateValue);
61+
62+
const A1(1, 2).check(1, 2);
63+
const A1.b1(1, y: 2).check(1, 2);
64+
const A1.b2(1, 2).check(1, 2);
65+
const A2(1, 2).check(1, 2);
66+
const A2.b1(1, y: 2).check(1, 2);
67+
const A2.b2(1, 2).check(1, 2);
68+
const P1(1, 2).check(1, 2);
69+
70+
const A1(1).check(1, 0);
71+
const A1.b1(1).check(1, 0);
72+
const A1.b2(1).check(1, const <int>[0]);
73+
const A2(1).check(1, 0);
74+
const A2.b1(1).check(1, 0);
75+
const A2.b2(1).check(1, const <int>[0]);
76+
const P1(1).check(1, privateValue);
77+
}

0 commit comments

Comments
 (0)