Skip to content

Commit 68b6bbc

Browse files
lrhncommit-bot@chromium.org
authored andcommitted
Add tests for new mixin declaration syntax.
The tests are not exhaustive, but they can hopefully be a good start when implementing the feature. The test are not *checked* since there is currently no implementation of the feature, or even the syntax. They should be expected to contain some errors. Bug: dart-lang/language#7 Change-Id: I4a5364566aeba9de036e56ae188205337575154c Reviewed-on: https://dart-review.googlesource.com/70509 Commit-Queue: Lasse R.H. Nielsen <[email protected]> Reviewed-by: Leaf Petersen <[email protected]>
1 parent f0172a4 commit 68b6bbc

14 files changed

+982
-0
lines changed

tests/language_2/language_2.status

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,21 +13,25 @@ deferred_inheritance_constraints_test/redirecting_constructor: Crash
1313

1414
[ $compiler == dart2analyzer ]
1515
double_literals/*: Skip # https://github.com/dart-lang/sdk/issues/34360
16+
mixin_declaration/*: Skip # See https://github.com/dart-lang/language/issues/7
1617

1718
[ $compiler != dart2analyzer ]
1819
switch_case_warn_test: Skip # Analyzer only, see language_analyzer2.status
1920

2021
[ $compiler == dart2js ]
2122
double_literals/*: Skip # https://github.com/dart-lang/sdk/issues/34356
2223
invalid_returns/*: Skip # https://github.com/dart-lang/sdk/issues/34011
24+
mixin_declaration/*: Skip # See https://github.com/dart-lang/language/issues/7
2325
void/*: Skip # https://github.com/dart-lang/sdk/issues/34011
2426

2527
[ $compiler == fasta ]
2628
double_literals/*: Skip # https://github.com/dart-lang/sdk/issues/34357
29+
mixin_declaration/*: Skip # See https://github.com/dart-lang/language/issues/7
2730

2831
[ $compiler == spec_parser ]
2932
double_literals/*: Skip # https://github.com/dart-lang/sdk/issues/34355
3033
invalid_returns/*: Skip # https://github.com/dart-lang/sdk/issues/34015
34+
mixin_declaration/*: Skip # See https://github.com/dart-lang/language/issues/7
3135
void/*: Skip # https://github.com/dart-lang/sdk/issues/34015
3236

3337
[ $mode == debug ]
@@ -222,6 +226,7 @@ type_parameter_test/05: Pass
222226

223227
[ $compiler == app_jit || $compiler == app_jitk || $compiler == dartk || $compiler == dartkb || $compiler == dartkp || $compiler == none || $compiler == precompiler ]
224228
double_literals/*: Skip # https://github.com/dart-lang/sdk/issues/34358
229+
mixin_declaration/*: Skip # See https://github.com/dart-lang/language/issues/7
225230

226231
[ $compiler == app_jit || $compiler == none ]
227232
invalid_returns/*: Skip # https://github.com/dart-lang/sdk/issues/34013
@@ -230,6 +235,7 @@ void/*: Skip # https://github.com/dart-lang/sdk/issues/34013
230235

231236
[ $compiler == dartdevc || $compiler == dartdevk ]
232237
double_literals/*: Skip # https://github.com/dart-lang/sdk/issues/34359
238+
mixin_declaration/*: Skip # See https://github.com/dart-lang/language/issues/7
233239

234240
[ $hot_reload || $hot_reload_rollback ]
235241
cha_deopt1_test: Crash # Requires deferred libraries
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// Copyright (c) 2018, 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 "package:expect/expect.dart";
6+
7+
// Test various invalid mixin applications where the supertype doesn't
8+
// implement the super-interfaces.
9+
10+
abstract class UnaryNum {
11+
num foo(num x) => x;
12+
}
13+
14+
abstract class UnaryInt {
15+
num foo(int x) => x;
16+
}
17+
18+
mixin M1 on UnaryNum {}
19+
20+
class _ = Object with M1; //# 01: compile-time error
21+
class _ = Null with M1; //# 02: compile-time error
22+
class _ = UnaryInt with M1; //# 03: compile-time error.
23+
24+
mixin M2 on UnaryNum, UnaryInt {}
25+
26+
class _ = UnaryInt with M2; //# 04: compile-time error.
27+
class _ = UnaryNum with M2; //# 05: compile-time error.
28+
29+
main() {
30+
// M1 and M2 are valid types.
31+
Expect.isNotType<M1>(null);
32+
Expect.isNotType<M2>(null);
33+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
// Copyright (c) 2018, 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 "package:expect/expect.dart";
6+
7+
// Test various invalid super-constraints for mixin declarations.
8+
9+
abstract class UnaryNum {
10+
num foo(num x);
11+
}
12+
13+
// Overides must still be valid, wrt. signatures and types.
14+
15+
mixin M3 on UnaryNum {
16+
// M3.foo is a valid override of UnaryNum.foo
17+
num foo(num x) => super.foo(x) * 2;
18+
}
19+
20+
// Invalid signature override (overriding optional parameter with required).
21+
class C3 implements UnaryNum {
22+
// C3.foo is a valid override of UnaryNum.foo
23+
num foo([num x]) => x ?? 17;
24+
}
25+
// M3.foo is not a valid override for C3.foo.
26+
class A3 extends C3 //
27+
with M3 //# 06: compile-time error
28+
{}
29+
30+
// Invalid type override (overriding `int` return with `num` return).
31+
class C4 implements UnaryNum {
32+
// C4.foo is a valid override of UnaryNum.foo
33+
int foo(num x) => x.toInt();
34+
}
35+
// M3.foo is not a valid override for C4.foo.
36+
class A4 extends C4 //
37+
with M3 //# 07: compile-time error
38+
{}
39+
40+
// It's not required to have an implementation of members which are not super-
41+
// invoked, if the application class is abstract.
42+
abstract class C5 {
43+
num foo(num x);
44+
num bar(num x);
45+
}
46+
mixin M5 on C5 {
47+
num baz(num x) => super.foo(x);
48+
}
49+
abstract class C5Foo implements C5 {
50+
num foo(num x) => x;
51+
}
52+
abstract class C5Bar implements C5 {
53+
num bar(num x) => x;
54+
}
55+
56+
// Valid abstract class, super-invocation of foo hits implementation,
57+
// even if bar is still abstract.
58+
abstract class A5Foo = C5Foo with M5;
59+
// Invalid since super-invocaton of foo does not hit concrete implementation.
60+
abstract class _ = C5Bar with M5; //# 08: compile-time error
61+
62+
main() {
63+
Expect.equals(42, A5Foo().baz(42));
64+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
// Copyright (c) 2018, 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+
// Test various invalid super-invocations for mixin declarations.
6+
7+
abstract class UnaryInt {
8+
num foo(int x);
9+
}
10+
11+
abstract class UnaryNum {
12+
num foo(num x);
13+
}
14+
15+
abstract class UnaryOptionalNum {
16+
num foo([num x]);
17+
}
18+
19+
// Mixins may contain super-invocations.
20+
// The super-invocation must be valid against the combined super-interfaces
21+
// (i.e., valid against the most specific of them for that method).
22+
23+
mixin M1 on UnaryNum {
24+
void bar() {
25+
super.foo(); //# 01: compile-time error
26+
super.foo(1, 2); //# 02: compile-time error
27+
super.foo("not num"); //# 03: compile-time error
28+
super.bar; //# 04: compile-time error
29+
super + 2; //# 05: compile-time error
30+
}
31+
}
32+
33+
mixin M2 on UnaryNum, UnaryInt {
34+
void bar() {
35+
super.foo(4.2); // Allows most specific type.
36+
super.foo(1, 2); //# 06: compile-time error
37+
super.foo("not num"); //# 07: compile-time error
38+
}
39+
}
40+
41+
mixin M3 on UnaryNum, UnaryOptionalNum {
42+
void bar() {
43+
super.foo(4.2);
44+
super.foo();
45+
super.foo(1, 2); //# 08: compile-time error
46+
super.foo("not num"); //# 09: compile-time error
47+
}
48+
}
49+
50+
class C1 implements UnaryNum, UnaryInt, UnaryOptionalNum {
51+
num foo([num x]) => x ?? 37.0;
52+
}
53+
54+
class A1 = C1 with M1;
55+
class A2 = C1 with M2;
56+
class A3 = C1 with M3;
57+
main() {
58+
A1().bar();
59+
A2().bar();
60+
A3().bar();
61+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
// Copyright (c) 2018, 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 "package:expect/expect.dart";
6+
7+
// Test various invalid syntax combinations.
8+
9+
// You cannot prefix "mixin" with anything.
10+
abstract //# 01: compile-time error
11+
static //# 02: compile-time error
12+
const //# 03: compile-time error
13+
mixin M0 {}
14+
15+
// Cannot use "extends".
16+
mixin M1 //
17+
extends A //# 04: compile-time error
18+
{}
19+
20+
// On-clause must be before implements clause.
21+
mixin M2
22+
implements B //# 05: compile-time error
23+
on A
24+
{}
25+
26+
// Cannot use "on" on class declarations.
27+
class C0 //
28+
on A //# 06: compile-time error
29+
{}
30+
31+
// Type parameters must not be empty.
32+
mixin M3 //
33+
<> //# 07: compile-time error
34+
{}
35+
36+
// Super-class restrictions and implements must be well-formed.
37+
mixin M4 on List
38+
<UndeclaredType> //# 08: compile-time error
39+
{}
40+
mixin M5 implements List
41+
<UndeclaredType> //# 09: compile-time error
42+
{}
43+
44+
mixin M6 {
45+
// Mixins cannot have constructors (or members with same name as mixin).
46+
factory M6() {} //# 10: compile-time error
47+
M6() {} //# 11: compile-time error
48+
M6.foo(); //# 12: compile-time error
49+
get M6 => 42; //# 13: compile-time error
50+
}
51+
52+
// Cannot declare local mixins.
53+
class C {
54+
static mixin M {}; //# 14: compile-time error
55+
mixin M {} //# 15: compile-time error
56+
}
57+
58+
// Just to have some types.
59+
class A {}
60+
class B {}
61+
62+
main() {
63+
new C();
64+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
// Copyright (c) 2018, 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 "package:expect/expect.dart";
6+
7+
// Test various invalid type-declaration combinations.
8+
9+
// Types must be class types.
10+
mixin M on void {} //# 01: compile-time error.
11+
mixin M on double {} //# 02: compile-time error.
12+
mixin M on FutureOr<int> {} //# 03: compile-time error.
13+
mixin M on FunType {} //# 04: compile-time error.
14+
15+
mixin M implements void {} //# 05: compile-time error.
16+
mixin M implements double {} //# 06: compile-time error.
17+
mixin M implements FutureOr<int> {} //# 07: compile-time error.
18+
mixin M implements FunType {} //# 08: compile-time error.
19+
20+
// Types must be extensible.
21+
mixin M on bool {} //# 09: compile-time error.
22+
mixin M on num {} //# 10: compile-time error.
23+
mixin M on int {} //# 11: compile-time error.
24+
mixin M on double {} //# 12: compile-time error.
25+
mixin M on Null {} //# 13: compile-time error.
26+
mixin M on String {} //# 14: compile-time error.
27+
mixin M implements bool {} //# 15: compile-time error.
28+
mixin M implements num {} //# 16: compile-time error.
29+
mixin M implements int {} //# 17: compile-time error.
30+
mixin M implements double {} //# 18: compile-time error.
31+
mixin M implements Null {} //# 19: compile-time error.
32+
mixin M implements String {} //# 20: compile-time error.
33+
34+
// Mixin type cannot depend on itself
35+
mixin M on M {} //# 21: compile-time error.
36+
mixin M implements M {} //# 22: compile-time error
37+
38+
// Types must exist and be valid
39+
mixin M on Undeclared {} //# 23: compile-time error
40+
mixin M on A<int> {} //# 24: compile-time error
41+
mixin M implements Undeclared {} //# 25: compile-time error
42+
mixin M implements A<int> {} //# 26: compile-time error
43+
44+
main() {}
45+
46+
// Just to have some types.
47+
class A {}
48+
class B {}
49+
typedef FuntType = int Function(int);
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Copyright (c) 2018, 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+
mixin Mixin // cannot `extend` anything.
6+
extends M //# 01: compile-time error
7+
extends Object //# 02: compile-time error
8+
{}
9+
10+
// You cannot extend a mixin.
11+
class Class // cannot extend a mixin
12+
extends Mixin //# 03: compile-time error
13+
{}
14+
15+
void main() {
16+
// Cannot instantiate a mixin.
17+
new Mixin(); //# 04: compile-time error
18+
new Class();
19+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// Copyright (c) 2018, 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 "package:expect/expect.dart";
6+
7+
abstract class Bar {
8+
String bar();
9+
}
10+
11+
abstract class Foo {
12+
String foo();
13+
}
14+
15+
mixin M implements Bar {
16+
dynamic noSuchMethod(i) => "M${i.memberName == #foo ? "foo" : "bar"}";
17+
}
18+
19+
abstract class C {
20+
dynamic noSuchMethod(i) => "C${i.memberName == #foo ? "foo" : "bar"}";
21+
}
22+
23+
abstract class D {
24+
String foo() => "D:foo";
25+
String bar() => "D:bar";
26+
}
27+
28+
class A1 extends Foo with M {}
29+
class A2 extends C with M implements Foo {}
30+
class A3 extends D with M implements Foo {}
31+
32+
main() {
33+
Expect.equals("M:foo", A1().foo());
34+
Expect.equals("M:bar", A1().bar());
35+
Expect.equals("M:foo", A2().foo());
36+
Expect.equals("M:bar", A2().bar());
37+
Expect.equals("D:foo", A3().foo());
38+
Expect.equals("D:bar", A3().bar());
39+
}

0 commit comments

Comments
 (0)