Skip to content

Commit 465518d

Browse files
committed
dart-lang#2275. Add more noSuchMethod tests
1 parent 1f1ff13 commit 465518d

File tree

9 files changed

+562
-0
lines changed

9 files changed

+562
-0
lines changed
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
// Copyright (c) 2023, 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+
/// @assertion We say that a class C has a non-trivial `noSuchMethod` if C has a
6+
/// concrete member named noSuchMethod which is distinct from the one declared
7+
/// in the built-in class [Object]
8+
///
9+
/// Note that it must be a method that accepts one positional argument, in order
10+
/// to correctly override noSuchMethod in Object. For instance, it can have
11+
/// signature noSuchMethod(Invocation i) or
12+
/// noSuchMethod(Object i, [String s = ”]), but not
13+
/// noSuchMethod(Invocation i, String s). This implies that the situation where
14+
/// noSuchMethod is invoked (explicitly or implicitly) with one actual argument
15+
/// cannot fail for the reason that “there is no such method”, such that we
16+
/// would enter an infinite loop trying to invoke noSuchMethod. It is possible,
17+
/// however, to encounter a dynamic error during an invocation of noSuchMethod
18+
/// because the actual argument fails to satisfy a type check, but that
19+
/// situation will give rise to a dynamic type error rather than a repeated
20+
/// attempt to invoke noSuchMethod
21+
///
22+
/// @description Checks that it is possible to define a `noSuchMethod` which
23+
/// is a correct override of `noSuchMethod` in `Object`.
24+
/// @author [email protected]
25+
26+
import "../../../../Utils/expect.dart";
27+
28+
class C1 {
29+
int m();
30+
void noSuchMethod(Invocation i) {}
31+
}
32+
33+
class C2 {
34+
int m();
35+
Object? noSuchMethod(Invocation i) => 2;
36+
}
37+
38+
class C3 {
39+
int m();
40+
Null noSuchMethod(Invocation i) => null;
41+
}
42+
43+
class C4 {
44+
int m();
45+
Never noSuchMethod(Invocation i) => throw 42;
46+
}
47+
48+
class C5 {
49+
int m();
50+
int noSuchMethod(Invocation i) => 5;
51+
}
52+
53+
main() {
54+
Expect.throws(() {
55+
C1().m();
56+
});
57+
Expect.equals(2, C2().m());
58+
Expect.throws(() {
59+
C3().m();
60+
});
61+
Expect.throws(() {
62+
C4().m();
63+
});
64+
Expect.equals(5, C5().m());
65+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// Copyright (c) 2023, 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+
/// @assertion We say that a class C has a non-trivial `noSuchMethod` if C has a
6+
/// concrete member named noSuchMethod which is distinct from the one declared
7+
/// in the built-in class [Object]
8+
///
9+
/// Note that it must be a method that accepts one positional argument, in order
10+
/// to correctly override noSuchMethod in Object. For instance, it can have
11+
/// signature noSuchMethod(Invocation i) or
12+
/// noSuchMethod(Object i, [String s = ”]), but not
13+
/// noSuchMethod(Invocation i, String s). This implies that the situation where
14+
/// noSuchMethod is invoked (explicitly or implicitly) with one actual argument
15+
/// cannot fail for the reason that “there is no such method”, such that we
16+
/// would enter an infinite loop trying to invoke noSuchMethod. It is possible,
17+
/// however, to encounter a dynamic error during an invocation of noSuchMethod
18+
/// because the actual argument fails to satisfy a type check, but that
19+
/// situation will give rise to a dynamic type error rather than a repeated
20+
/// attempt to invoke noSuchMethod
21+
///
22+
/// @description Checks that it is possible to define a `noSuchMethod` which
23+
/// is a correct override of `noSuchMethod` in `Object`.
24+
/// @author [email protected]
25+
26+
import "../../../../Utils/expect.dart";
27+
28+
class C1 {
29+
int m();
30+
dynamic noSuchMethod(Invocation i, [int x = 1]) {
31+
return x;
32+
}
33+
}
34+
35+
class C2 {
36+
int m();
37+
dynamic noSuchMethod(Invocation i, {int x = 2}) => x;
38+
}
39+
40+
main() {
41+
Expect.equals(1, C1().m());
42+
Expect.equals(2, C2().m());
43+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// Copyright (c) 2023, 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+
/// @assertion We say that a class C has a non-trivial `noSuchMethod` if C has a
6+
/// concrete member named noSuchMethod which is distinct from the one declared
7+
/// in the built-in class [Object]
8+
///
9+
/// Note that it must be a method that accepts one positional argument, in order
10+
/// to correctly override noSuchMethod in Object. For instance, it can have
11+
/// signature noSuchMethod(Invocation i) or
12+
/// noSuchMethod(Object i, [String s = ”]), but not
13+
/// noSuchMethod(Invocation i, String s). This implies that the situation where
14+
/// noSuchMethod is invoked (explicitly or implicitly) with one actual argument
15+
/// cannot fail for the reason that “there is no such method”, such that we
16+
/// would enter an infinite loop trying to invoke noSuchMethod. It is possible,
17+
/// however, to encounter a dynamic error during an invocation of noSuchMethod
18+
/// because the actual argument fails to satisfy a type check, but that
19+
/// situation will give rise to a dynamic type error rather than a repeated
20+
/// attempt to invoke noSuchMethod
21+
///
22+
/// @description Checks that it is possible to define a `noSuchMethod` which
23+
/// is a correct override of `noSuchMethod` in `Object`.
24+
/// @author [email protected]
25+
26+
import "../../../../Utils/expect.dart";
27+
28+
class C1 {
29+
int m();
30+
external dynamic noSuchMethod(Invocation i);
31+
}
32+
33+
class C2 {
34+
int m();
35+
dynamic noSuchMethod(covariant Invocation i) => 2;
36+
}
37+
38+
main() {
39+
Expect.throws(() {
40+
C1().m();
41+
});
42+
Expect.equals(2, C2().m());
43+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// Copyright (c) 2023, 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+
/// @assertion We say that a class C has a non-trivial `noSuchMethod` if C has a
6+
/// concrete member named noSuchMethod which is distinct from the one declared
7+
/// in the built-in class [Object]
8+
///
9+
/// Note that it must be a method that accepts one positional argument, in order
10+
/// to correctly override noSuchMethod in Object. For instance, it can have
11+
/// signature noSuchMethod(Invocation i) or
12+
/// noSuchMethod(Object i, [String s = ”]), but not
13+
/// noSuchMethod(Invocation i, String s). This implies that the situation where
14+
/// noSuchMethod is invoked (explicitly or implicitly) with one actual argument
15+
/// cannot fail for the reason that “there is no such method”, such that we
16+
/// would enter an infinite loop trying to invoke noSuchMethod. It is possible,
17+
/// however, to encounter a dynamic error during an invocation of noSuchMethod
18+
/// because the actual argument fails to satisfy a type check, but that
19+
/// situation will give rise to a dynamic type error rather than a repeated
20+
/// attempt to invoke noSuchMethod
21+
///
22+
/// @description Checks that it is possible to define a `noSuchMethod` which
23+
/// is a correct override of `noSuchMethod` in `Object`.
24+
/// @author [email protected]
25+
26+
import "../../../../Utils/expect.dart";
27+
28+
class C1 {
29+
int m();
30+
dynamic noSuchMethod(covariant Object o) => o;
31+
}
32+
33+
main() {
34+
Expect.throws(() {
35+
C1().m();
36+
});
37+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
// Copyright (c) 2023, 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+
/// @assertion We say that a class C has a non-trivial `noSuchMethod` if C has a
6+
/// concrete member named noSuchMethod which is distinct from the one declared
7+
/// in the built-in class [Object]
8+
///
9+
/// Note that it must be a method that accepts one positional argument, in order
10+
/// to correctly override noSuchMethod in Object. For instance, it can have
11+
/// signature noSuchMethod(Invocation i) or
12+
/// noSuchMethod(Object i, [String s = ”]), but not
13+
/// noSuchMethod(Invocation i, String s). This implies that the situation where
14+
/// noSuchMethod is invoked (explicitly or implicitly) with one actual argument
15+
/// cannot fail for the reason that “there is no such method”, such that we
16+
/// would enter an infinite loop trying to invoke noSuchMethod. It is possible,
17+
/// however, to encounter a dynamic error during an invocation of noSuchMethod
18+
/// because the actual argument fails to satisfy a type check, but that
19+
/// situation will give rise to a dynamic type error rather than a repeated
20+
/// attempt to invoke noSuchMethod
21+
///
22+
/// @description Checks that it is a compile-time error to define a
23+
/// `noSuchMethod` which is not a correct override of `noSuchMethod` in `Object`
24+
/// @author [email protected]
25+
26+
class C1 {
27+
void noSuchMethod() {}
28+
// ^^^^^^^^^^^^
29+
// [analyzer] unspecified
30+
// [cfe] unspecified
31+
}
32+
33+
class C2 {
34+
void noSuchMethod(Invocation i, String s) {}
35+
// ^^^^^^^^^^^^
36+
// [analyzer] unspecified
37+
// [cfe] unspecified
38+
}
39+
40+
class C3 {
41+
void noSuchMethod(Invocation i, {required String s}) {}
42+
// ^^^^^^^^^^^^
43+
// [analyzer] unspecified
44+
// [cfe] unspecified
45+
}
46+
47+
class C4 {
48+
dynamic noSuchMethod(int i) => i;
49+
// ^^^^^^^^^^^^
50+
// [analyzer] unspecified
51+
// [cfe] unspecified
52+
}
53+
54+
main() {
55+
print(C1);
56+
print(C2);
57+
print(C3);
58+
print(C4);
59+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
// Copyright (c) 2023, 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+
/// @assertion We say that a class C has a non-trivial `noSuchMethod` if C has a
6+
/// concrete member named noSuchMethod which is distinct from the one declared
7+
/// in the built-in class [Object]
8+
///
9+
/// Note that it must be a method that accepts one positional argument, in order
10+
/// to correctly override noSuchMethod in Object. For instance, it can have
11+
/// signature noSuchMethod(Invocation i) or
12+
/// noSuchMethod(Object i, [String s = ”]), but not
13+
/// noSuchMethod(Invocation i, String s). This implies that the situation where
14+
/// noSuchMethod is invoked (explicitly or implicitly) with one actual argument
15+
/// cannot fail for the reason that “there is no such method”, such that we
16+
/// would enter an infinite loop trying to invoke noSuchMethod. It is possible,
17+
/// however, to encounter a dynamic error during an invocation of noSuchMethod
18+
/// because the actual argument fails to satisfy a type check, but that
19+
/// situation will give rise to a dynamic type error rather than a repeated
20+
/// attempt to invoke noSuchMethod
21+
///
22+
/// @description Checks that it is a compile-time error to define a
23+
/// `noSuchMethod` which is not a correct override of `noSuchMethod` in `Object`
24+
/// @author [email protected]
25+
26+
class C1 {
27+
external void noSuchMethod();
28+
// ^^^^^^^^^^^^
29+
// [analyzer] unspecified
30+
// [cfe] unspecified
31+
}
32+
33+
class C2 {
34+
external void noSuchMethod(Invocation i, String s);
35+
// ^^^^^^^^^^^^
36+
// [analyzer] unspecified
37+
// [cfe] unspecified
38+
}
39+
40+
class C3 {
41+
external void noSuchMethod(Invocation i, {required String s});
42+
// ^^^^^^^^^^^^
43+
// [analyzer] unspecified
44+
// [cfe] unspecified
45+
}
46+
47+
class C4 {
48+
external dynamic noSuchMethod(int i);
49+
// ^^^^^^^^^^^^
50+
// [analyzer] unspecified
51+
// [cfe] unspecified
52+
}
53+
54+
main() {
55+
print(C1);
56+
print(C2);
57+
print(C3);
58+
print(C4);
59+
}

0 commit comments

Comments
 (0)