Skip to content

Commit 4245f45

Browse files
committed
dart-lang#1401. [Patterns] relational pattern tests added
1 parent f5fa463 commit 4245f45

12 files changed

+767
-0
lines changed
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
// Copyright (c) 2022, 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 relationalPattern ::=
6+
/// ( equalityOperator | relationalOperator) relationalExpression
7+
///
8+
/// A relational pattern lets you compare the matched value to a given constant
9+
/// using any of the equality or relational operators: ==, !=, <, >, <=, and >=.
10+
/// The pattern matches when calling the appropriate operator on the matched
11+
/// value with the constant as an argument returns true. It is a compile-time
12+
/// error if relationalExpression is not a valid constant expression.
13+
///
14+
/// @description Checks a relational pattern in a switch expression
15+
/// @author [email protected]
16+
17+
// SharedOptions=--enable-experiment=patterns
18+
19+
import "../../Utils/expect.dart";
20+
21+
String test1(num num) {
22+
return switch (num) {
23+
case < 0 => "negative";
24+
case == 0 => "zero";
25+
case > 0 => "positive";
26+
default => "Impossible!";
27+
};
28+
}
29+
30+
String test2(double num) {
31+
return switch (num) {
32+
case != 0 => "non-zero";
33+
case == 0 => "zero";
34+
default => "Impossible!";
35+
};
36+
}
37+
38+
String test3(int num) {
39+
return switch (num) {
40+
case >= 1 => "positive";
41+
case <= 0 => "zero or negative";
42+
default => "Impossible!";
43+
};
44+
}
45+
46+
main() {
47+
Expect.equals("negative", test1(-1.1));
48+
Expect.equals("positive", test1(42));
49+
Expect.equals("zero", test1(0));
50+
Expect.equals("non-zero", test2(3.14));
51+
Expect.equals("zero", test2(0));
52+
Expect.equals("zero or negative", test3(0));
53+
Expect.equals("zero or negative", test3(-1));
54+
Expect.equals("positive", test3(1));
55+
Expect.equals("positive", test3(42));
56+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// Copyright (c) 2022, 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 relationalPattern ::=
6+
/// ( equalityOperator | relationalOperator) relationalExpression
7+
///
8+
/// A relational pattern lets you compare the matched value to a given constant
9+
/// using any of the equality or relational operators: ==, !=, <, >, <=, and >=.
10+
/// The pattern matches when calling the appropriate operator on the matched
11+
/// value with the constant as an argument returns true. It is a compile-time
12+
/// error if relationalExpression is not a valid constant expression.
13+
///
14+
/// @description Checks a relational subpattern in a switch expression
15+
/// @author [email protected]
16+
17+
// SharedOptions=--enable-experiment=patterns
18+
19+
import "../../Utils/expect.dart";
20+
21+
String test(List<num> list) => switch (list) {
22+
case [> 0 & <= 2] => "case 1";
23+
case [== 42] => "case 2";
24+
case [>= 10 & < 20] => "case 3";
25+
case [!= 100] => "case 4";
26+
default => "default";
27+
};
28+
29+
main() {
30+
Expect.equals("case 1", test([1.1]));
31+
Expect.equals("case 1", test([2]));
32+
Expect.equals("case 2", test([42]));
33+
Expect.equals("case 3", test([10]));
34+
Expect.equals("case 3", test([11.1]));
35+
Expect.equals("case 4", test([20]));
36+
Expect.equals("default", test([100]));
37+
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
// Copyright (c) 2022, 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 relationalPattern ::=
6+
/// ( equalityOperator | relationalOperator) relationalExpression
7+
///
8+
/// A relational pattern lets you compare the matched value to a given constant
9+
/// using any of the equality or relational operators: ==, !=, <, >, <=, and >=.
10+
/// The pattern matches when calling the appropriate operator on the matched
11+
/// value with the constant as an argument returns true. It is a compile-time
12+
/// error if relationalExpression is not a valid constant expression.
13+
///
14+
/// @description Checks a relational pattern in a switch statement
15+
/// @author [email protected]
16+
17+
// SharedOptions=--enable-experiment=patterns
18+
19+
import "../../Utils/expect.dart";
20+
21+
void test1(double value, String expected) {
22+
switch (value) {
23+
case < 0:
24+
Expect.equals("negative", expected);
25+
break;
26+
case == 0:
27+
Expect.equals("zero", expected);
28+
break;
29+
case > 0:
30+
Expect.equals("positive", expected);
31+
break;
32+
default:
33+
Expect.fail("One of the cases above should match");
34+
}
35+
}
36+
37+
void test2(num value, String expected) {
38+
switch (value) {
39+
case != 0:
40+
Expect.equals("non-zero", expected);
41+
break;
42+
case == 0:
43+
Expect.equals("zero", expected);
44+
break;
45+
default:
46+
Expect.fail("One of the cases above should match");
47+
}
48+
}
49+
50+
void test3(int value, String expected) {
51+
switch (value) {
52+
case >= 1:
53+
Expect.equals("positive", expected);
54+
break;
55+
case <= 0:
56+
Expect.equals("zero or negative", expected);
57+
break;
58+
default:
59+
Expect.fail("One of the cases above should match");
60+
}
61+
}
62+
63+
main() {
64+
test1(-1.1, "negative");
65+
test1(42.42, "positive");
66+
test1(0, "zero");
67+
test2(3.14, "non-zero");
68+
test2(0, "zero");
69+
test3(0, "zero or negative");
70+
test3(-100, "zero or negative");
71+
test3(1, "positive");
72+
test3(42, "positive");
73+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// Copyright (c) 2022, 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 relationalPattern ::=
6+
/// ( equalityOperator | relationalOperator) relationalExpression
7+
///
8+
/// A relational pattern lets you compare the matched value to a given constant
9+
/// using any of the equality or relational operators: ==, !=, <, >, <=, and >=.
10+
/// The pattern matches when calling the appropriate operator on the matched
11+
/// value with the constant as an argument returns true. It is a compile-time
12+
/// error if relationalExpression is not a valid constant expression.
13+
///
14+
/// @description Checks a relational subpattern in a switch statement
15+
/// @author [email protected]
16+
17+
// SharedOptions=--enable-experiment=patterns
18+
19+
import "../../Utils/expect.dart";
20+
21+
void test(List<num> list, String expected) {
22+
switch (list) {
23+
case [> 0 & <= 2]:
24+
Expect.equals("case 1", expected);
25+
break;
26+
case [== 42]:
27+
Expect.equals("case 2", expected);
28+
break;
29+
case [>= 10 & < 20]:
30+
Expect.equals("case 3", expected);
31+
break;
32+
case [!= 100]:
33+
Expect.equals("case 4", expected);
34+
break;
35+
default:
36+
Expect.equals("default", expected);
37+
}
38+
}
39+
40+
main() {
41+
test([1], "case 1");
42+
test([2], "case 1");
43+
test([2.1], "case 2");
44+
test([10], "case 3");
45+
test([10.1], "case 3");
46+
test([20], "case 4");
47+
test([100], "default");
48+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
// Copyright (c) 2022, 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 relationalPattern ::=
6+
/// ( equalityOperator | relationalOperator) relationalExpression
7+
///
8+
/// A relational pattern lets you compare the matched value to a given constant
9+
/// using any of the equality or relational operators: ==, !=, <, >, <=, and >=.
10+
/// The pattern matches when calling the appropriate operator on the matched
11+
/// value with the constant as an argument returns true. It is a compile-time
12+
/// error if relationalExpression is not a valid constant expression.
13+
///
14+
/// @description Check that it is a compile-time error if relationalExpression
15+
/// is not a valid constant expression. Test switch statement
16+
/// @author [email protected]
17+
18+
// SharedOptions=--enable-experiment=patterns
19+
20+
main() {
21+
int i = 0;
22+
int value = 42;
23+
24+
switch (value) {
25+
case < i:
26+
// ^
27+
// [analyzer] unspecified
28+
// [cfe] unspecified
29+
break;
30+
case == 0 + i:
31+
// ^^^^^
32+
// [analyzer] unspecified
33+
// [cfe] unspecified
34+
break;
35+
case > i++:
36+
// ^^^
37+
// [analyzer] unspecified
38+
// [cfe] unspecified
39+
break;
40+
default:
41+
}
42+
43+
final j = 0;
44+
switch (value) {
45+
case != j:
46+
// ^
47+
// [analyzer] unspecified
48+
// [cfe] unspecified
49+
break;
50+
case == j:
51+
// ^
52+
// [analyzer] unspecified
53+
// [cfe] unspecified
54+
break;
55+
default:
56+
}
57+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
// Copyright (c) 2022, 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 relationalPattern ::=
6+
/// ( equalityOperator | relationalOperator) relationalExpression
7+
///
8+
/// A relational pattern lets you compare the matched value to a given constant
9+
/// using any of the equality or relational operators: ==, !=, <, >, <=, and >=.
10+
/// The pattern matches when calling the appropriate operator on the matched
11+
/// value with the constant as an argument returns true. It is a compile-time
12+
/// error if relationalExpression is not a valid constant expression.
13+
///
14+
/// @description Check that it is a compile-time error if relationalExpression
15+
/// is not a valid constant expression. Test switch expression
16+
/// @author [email protected]
17+
18+
// SharedOptions=--enable-experiment=patterns
19+
20+
String test1(int num) {
21+
int i = 0;
22+
return switch (num) {
23+
case < i => "negative";
24+
// ^
25+
// [analyzer] unspecified
26+
// [cfe] unspecified
27+
case == i => "zero";
28+
// ^
29+
// [analyzer] unspecified
30+
// [cfe] unspecified
31+
case > i => "positive";
32+
// ^
33+
// [analyzer] unspecified
34+
// [cfe] unspecified
35+
default => "Impossible!";
36+
};
37+
}
38+
39+
String test2(int num) {
40+
final j = 0;
41+
return switch (num) {
42+
case != j => "non-zero";
43+
// ^
44+
// [analyzer] unspecified
45+
// [cfe] unspecified
46+
case == j => "zero";
47+
// ^
48+
// [analyzer] unspecified
49+
// [cfe] unspecified
50+
default => "Impossible!";
51+
};
52+
}
53+
54+
main() {
55+
test1(1);
56+
test2(2);
57+
}

0 commit comments

Comments
 (0)