Skip to content

Commit bc58e06

Browse files
committed
dart-lang#1401. [Patterns] Logical-or tests added
1 parent 113255e commit bc58e06

8 files changed

+371
-0
lines changed
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 logicalOrPattern ::= ( logicalOrPattern '|' )? logicalAndPattern
6+
///
7+
/// A pair of patterns separated by | matches if either of the branches match
8+
///
9+
/// A logical-or pattern may match even if one of its branches does not. That
10+
/// means that any variables in the non-matching branch would not be
11+
/// initialized. To avoid problems stemming from that, the following
12+
/// restrictions apply:
13+
///
14+
/// The two branches must define the same set of variables.
15+
///
16+
/// If the left branch matches, the right branch is not evaluated. This
17+
/// determines which value the variable gets if both branches would have
18+
/// matched. In that case, it will always be the value from the left branch.
19+
///
20+
/// @description Checks logical-or pattern in a switch expression
21+
/// @author [email protected]
22+
23+
// SharedOptions=--enable-experiment=patterns
24+
25+
import "../../Utils/expect.dart";
26+
27+
enum Color {
28+
white,
29+
red,
30+
yellow,
31+
blue,
32+
black;
33+
}
34+
35+
bool isPrimary(Color color) {
36+
return switch (color) {
37+
case Color.red | Color.yellow | Color.blue => true;
38+
default => false;
39+
};
40+
}
41+
42+
main() {
43+
Expect.isFalse(Color.black);
44+
Expect.isFalse(Color.white);
45+
Expect.isTrue(Color.red);
46+
Expect.isTrue(Color.yellow);
47+
Expect.isTrue(Color.blue);
48+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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 logicalOrPattern ::= ( logicalOrPattern '|' )? logicalAndPattern
6+
///
7+
/// A pair of patterns separated by | matches if either of the branches match
8+
///
9+
/// A logical-or pattern may match even if one of its branches does not. That
10+
/// means that any variables in the non-matching branch would not be
11+
/// initialized. To avoid problems stemming from that, the following
12+
/// restrictions apply:
13+
///
14+
/// The two branches must define the same set of variables.
15+
///
16+
/// If the left branch matches, the right branch is not evaluated. This
17+
/// determines which value the variable gets if both branches would have
18+
/// matched. In that case, it will always be the value from the left branch.
19+
///
20+
/// @description Checks logical-or subpattern in a switch expression
21+
/// @author [email protected]
22+
23+
// SharedOptions=--enable-experiment=patterns
24+
25+
import "../../Utils/expect.dart";
26+
27+
bool matches(List list) => switch (list) {
28+
case [1 | 2, 3] => true;
29+
default => false;
30+
};
31+
32+
main() {
33+
Expect.isFalse([0, 3]);
34+
Expect.isFalse([2, 2]);
35+
Expect.isFalse([2, 3, 4]);
36+
Expect.isTrue([1, 3]);
37+
Expect.isTrue([2, 3]);
38+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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 logicalOrPattern ::= ( logicalOrPattern '|' )? logicalAndPattern
6+
///
7+
/// A pair of patterns separated by | matches if either of the branches match
8+
///
9+
/// A logical-or pattern may match even if one of its branches does not. That
10+
/// means that any variables in the non-matching branch would not be
11+
/// initialized. To avoid problems stemming from that, the following
12+
/// restrictions apply:
13+
///
14+
/// The two branches must define the same set of variables.
15+
///
16+
/// If the left branch matches, the right branch is not evaluated. This
17+
/// determines which value the variable gets if both branches would have
18+
/// matched. In that case, it will always be the value from the left branch.
19+
///
20+
/// @description Checks logical-or pattern in a switch statements
21+
/// @author [email protected]
22+
23+
// SharedOptions=--enable-experiment=patterns
24+
25+
import "patterns_lib.dart";
26+
import "../../Utils/expect.dart";
27+
28+
void test(Shape shape, double expectedArea, Type expectedType, bool match) {
29+
switch (shape) {
30+
case Square(area: var s) | Circle(area: var s):
31+
Expect.equals(expectedArea, s);
32+
Expect.equals(expectedType, shape.runtimeType);
33+
Expect.isTrue(match);
34+
break;
35+
default:
36+
Expect.equals(expectedArea, s);
37+
Expect.equals(expectedType, shape.runtimeType);
38+
Expect.isFalse(match);
39+
}
40+
}
41+
42+
main() {
43+
test(Circle(1), 3.14, Circle, true);
44+
test(Square(2), 4, Square, true);
45+
test(Rectangle(2, 1), 2, Rectangle, false);
46+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
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 logicalOrPattern ::= ( logicalOrPattern '|' )? logicalAndPattern
6+
///
7+
/// A pair of patterns separated by | matches if either of the branches match
8+
///
9+
/// A logical-or pattern may match even if one of its branches does not. That
10+
/// means that any variables in the non-matching branch would not be
11+
/// initialized. To avoid problems stemming from that, the following
12+
/// restrictions apply:
13+
///
14+
/// The two branches must define the same set of variables.
15+
///
16+
/// If the left branch matches, the right branch is not evaluated. This
17+
/// determines which value the variable gets if both branches would have
18+
/// matched. In that case, it will always be the value from the left branch.
19+
///
20+
/// @description Checks logical-or subpattern in a switch statement
21+
/// @author [email protected]
22+
23+
// SharedOptions=--enable-experiment=patterns
24+
25+
import "../../Utils/expect.dart";
26+
27+
bool matches(List list) {
28+
bool result = false;
29+
switch (list) {
30+
case [1 | 2, 3]:
31+
result = true;
32+
default:
33+
};
34+
return result;
35+
}
36+
37+
main() {
38+
Expect.isFalse([0, 3]);
39+
Expect.isFalse([2, 2]);
40+
Expect.isFalse([2, 3, 4]);
41+
Expect.isTrue([1, 3]);
42+
Expect.isTrue([2, 3]);
43+
}
44+
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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 logicalOrPattern ::= ( logicalOrPattern '|' )? logicalAndPattern
6+
///
7+
/// A pair of patterns separated by | matches if either of the branches match
8+
///
9+
/// A logical-or pattern may match even if one of its branches does not. That
10+
/// means that any variables in the non-matching branch would not be
11+
/// initialized. To avoid problems stemming from that, the following
12+
/// restrictions apply:
13+
///
14+
/// The two branches must define the same set of variables.
15+
///
16+
/// If the left branch matches, the right branch is not evaluated. This
17+
/// determines which value the variable gets if both branches would have
18+
/// matched. In that case, it will always be the value from the left branch.
19+
///
20+
/// @description Checks that it is a compile-time error if two branches of
21+
/// logical-or pattern define different sets of variables
22+
/// @author [email protected]
23+
24+
// SharedOptions=--enable-experiment=patterns
25+
26+
import "patterns_lib.dart";
27+
28+
main() {
29+
Shape shape = Circle(1);
30+
switch (shape) {
31+
case Square(area: var s1) | Circle(area: var s2):
32+
// ^^
33+
// [analyzer] unspecified
34+
// [cfe] unspecified
35+
print("Square or Circle");
36+
break;
37+
case Rectangle(x: var x, y: var width) | Rectangle(:var x, :var y):
38+
// ^
39+
// [analyzer] unspecified
40+
// [cfe] unspecified
41+
print("Rectangle");
42+
break;
43+
default:
44+
print("Other");
45+
}
46+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
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 logicalOrPattern ::= ( logicalOrPattern '|' )? logicalAndPattern
6+
///
7+
/// A pair of patterns separated by | matches if either of the branches match
8+
///
9+
/// A logical-or pattern may match even if one of its branches does not. That
10+
/// means that any variables in the non-matching branch would not be
11+
/// initialized. To avoid problems stemming from that, the following
12+
/// restrictions apply:
13+
///
14+
/// The two branches must define the same set of variables.
15+
///
16+
/// If the left branch matches, the right branch is not evaluated. This
17+
/// determines which value the variable gets if both branches would have
18+
/// matched. In that case, it will always be the value from the left branch.
19+
///
20+
/// @description Checks that if the left branch matches, the right branch is not
21+
/// evaluated
22+
/// @author [email protected]
23+
24+
// SharedOptions=--enable-experiment=patterns
25+
26+
import "patterns_lib.dart";
27+
import "../../Utils/expect.dart";
28+
29+
main() {
30+
Shape shape1 = Square(1);
31+
switch (shape1) {
32+
case Square(area: var s) | Point(area: var s):
33+
Expect.equals(1, s);
34+
break;
35+
default:
36+
print("Other");
37+
}
38+
39+
Shape shape2 = Circle(1);
40+
switch (shape2) {
41+
case Square(area: var s) | Circle(area: var s) | Point(area: var s):
42+
Expect.equals(3.14, s);
43+
break;
44+
default:
45+
print("Other");
46+
}
47+
48+
Shape shape3 = Circle(1);
49+
switch (shape3) {
50+
case Circle(area: var s) | Point(area: var s) | Square(area: var s):
51+
Expect.equals(3.14, s);
52+
break;
53+
default:
54+
print("Other");
55+
}
56+
57+
Expect.throws(() {
58+
Shape shape4 = Point();
59+
switch (shape4) {
60+
case Square(area: var s) | Point(area: var s):
61+
print("Square or Point");
62+
break;
63+
default:
64+
print("Other");
65+
}
66+
});
67+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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 logicalOrPattern ::= ( logicalOrPattern '|' )? logicalAndPattern
6+
///
7+
/// A pair of patterns separated by | matches if either of the branches match
8+
///
9+
/// A logical-or pattern may match even if one of its branches does not. That
10+
/// means that any variables in the non-matching branch would not be
11+
/// initialized. To avoid problems stemming from that, the following
12+
/// restrictions apply:
13+
///
14+
/// The two branches must define the same set of variables.
15+
///
16+
/// If the left branch matches, the right branch is not evaluated. This
17+
/// determines which value the variable gets if both branches would have
18+
/// matched. In that case, it will always be the value from the left branch.
19+
///
20+
/// @description Checks that if both branches matches then value from the left
21+
/// branch is taken
22+
/// @author [email protected]
23+
24+
// SharedOptions=--enable-experiment=patterns
25+
26+
import "../../Utils/expect.dart";
27+
28+
class Shape {
29+
int counter = 0;
30+
double get area => counter++;
31+
}
32+
33+
main() {
34+
Shape shape = Shape();
35+
switch (shape) {
36+
case Shape(area: var s) | Shape(area: var s) | Shape(area: var s):
37+
Expect.equals(0, s);
38+
break;
39+
default:
40+
print("Other");
41+
}
42+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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+
/// @description Defines data commonly used by Patterns tests
6+
/// @author [email protected]
7+
8+
// SharedOptions=--enable-experiment=patterns
9+
10+
library patterns_lib;
11+
12+
abstract class Shape {
13+
double get area;
14+
}
15+
16+
class Square implements Shape {
17+
final double length;
18+
Square(this.length);
19+
20+
double get area => length * length;
21+
}
22+
23+
class Circle implements Shape {
24+
final double radius;
25+
Circle(this.radius);
26+
27+
double get area => 3.14 * radius * radius;
28+
}
29+
30+
class Rectangle implements Shape {
31+
final double x, y;
32+
Rectangle(this.x, this.y);
33+
34+
double get area => x * y;
35+
}
36+
37+
class Point implements Shape {
38+
Point();
39+
Never get area => throw Exception("Point has no area");
40+
}

0 commit comments

Comments
 (0)