-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Authored by @sgrekhov. Logical-and pattern tests added.
- Loading branch information
Showing
9 changed files
with
364 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
// Copyright (c) 2022, the Dart project authors. Please see the AUTHORS file | ||
// for details. All rights reserved. Use of this source code is governed by a | ||
// BSD-style license that can be found in the LICENSE file. | ||
|
||
/// @assertion | ||
/// logicalAndPattern ::= ( logicalAndPattern '&' )? relationalPattern | ||
/// | ||
/// A pair of patterns separated by & matches only if both subpatterns match. | ||
/// Unlike logical-or patterns, the variables defined in each branch must not | ||
/// overlap, since the logical-and pattern only matches if both branches do and | ||
/// the variables in both branches will be bound. | ||
/// | ||
/// If the left branch does not match, the right branch is not evaluated. This | ||
/// only matters because patterns may invoke user-defined methods with visible | ||
/// side effects. | ||
/// | ||
/// @description Checks a logical-and pattern in a switch expression | ||
/// @author [email protected] | ||
// SharedOptions=--enable-experiment=patterns | ||
|
||
import "../../Utils/expect.dart"; | ||
|
||
enum Color { | ||
white, | ||
red, | ||
yellow, | ||
blue, | ||
black; | ||
} | ||
|
||
bool isPrimary(Color color) { | ||
return switch (color) { | ||
case != Color.black & != Color.white => true; | ||
default => false; | ||
}; | ||
} | ||
|
||
main() { | ||
Expect.isFalse(isPrimary(Color.black)); | ||
Expect.isFalse(isPrimary(Color.white)); | ||
Expect.isTrue(isPrimary(Color.red)); | ||
Expect.isTrue(isPrimary(Color.yellow)); | ||
Expect.isTrue(isPrimary(Color.blue)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
// Copyright (c) 2022, the Dart project authors. Please see the AUTHORS file | ||
// for details. All rights reserved. Use of this source code is governed by a | ||
// BSD-style license that can be found in the LICENSE file. | ||
|
||
/// @assertion | ||
/// logicalAndPattern ::= ( logicalAndPattern '&' )? relationalPattern | ||
/// | ||
/// A pair of patterns separated by & matches only if both subpatterns match. | ||
/// Unlike logical-or patterns, the variables defined in each branch must not | ||
/// overlap, since the logical-and pattern only matches if both branches do and | ||
/// the variables in both branches will be bound. | ||
/// | ||
/// If the left branch does not match, the right branch is not evaluated. This | ||
/// only matters because patterns may invoke user-defined methods with visible | ||
/// side effects. | ||
/// | ||
/// @description Checks a logical-and subpattern in a switch expression | ||
/// @author [email protected] | ||
// SharedOptions=--enable-experiment=patterns | ||
|
||
import "../../Utils/expect.dart"; | ||
|
||
bool matches(List list) => switch (list) { | ||
case [> 0 & <= 2, 3] => true; | ||
default => false; | ||
}; | ||
|
||
main() { | ||
Expect.isFalse(matches([0, 3])); | ||
Expect.isFalse(matches([2, 2])); | ||
Expect.isFalse(matches([2, 3, 4])); | ||
Expect.isFalse(matches([2, "3"])); | ||
Expect.isFalse(matches(["1", 3])); | ||
Expect.isTrue(matches([1, 3])); | ||
Expect.isTrue(matches([2, 3])); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
// Copyright (c) 2022, the Dart project authors. Please see the AUTHORS file | ||
// for details. All rights reserved. Use of this source code is governed by a | ||
// BSD-style license that can be found in the LICENSE file. | ||
|
||
/// @assertion | ||
/// logicalAndPattern ::= ( logicalAndPattern '&' )? relationalPattern | ||
/// | ||
/// A pair of patterns separated by & matches only if both subpatterns match. | ||
/// Unlike logical-or patterns, the variables defined in each branch must not | ||
/// overlap, since the logical-and pattern only matches if both branches do and | ||
/// the variables in both branches will be bound. | ||
/// | ||
/// If the left branch does not match, the right branch is not evaluated. This | ||
/// only matters because patterns may invoke user-defined methods with visible | ||
/// side effects. | ||
/// | ||
/// @description Checks a logical-and pattern in a switch statement | ||
/// @author [email protected] | ||
// SharedOptions=--enable-experiment=patterns | ||
|
||
import "patterns_lib.dart"; | ||
import "../../Utils/expect.dart"; | ||
|
||
void test(int value, bool match) { | ||
switch (value) { | ||
case > 0 & <= 3: | ||
Expect.isTrue(match); | ||
break; | ||
default: | ||
Expect.isFalse(match); | ||
} | ||
} | ||
|
||
main() { | ||
test(1, true); | ||
test(2, true); | ||
test(3, true); | ||
test(0, false); | ||
test(4, false); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
// Copyright (c) 2022, the Dart project authors. Please see the AUTHORS file | ||
// for details. All rights reserved. Use of this source code is governed by a | ||
// BSD-style license that can be found in the LICENSE file. | ||
|
||
/// @assertion | ||
/// logicalAndPattern ::= ( logicalAndPattern '&' )? relationalPattern | ||
/// | ||
/// A pair of patterns separated by & matches only if both subpatterns match. | ||
/// Unlike logical-or patterns, the variables defined in each branch must not | ||
/// overlap, since the logical-and pattern only matches if both branches do and | ||
/// the variables in both branches will be bound. | ||
/// | ||
/// If the left branch does not match, the right branch is not evaluated. This | ||
/// only matters because patterns may invoke user-defined methods with visible | ||
/// side effects. | ||
/// | ||
/// @description Checks a logical-and subpattern in a switch statement | ||
/// @author [email protected] | ||
// SharedOptions=--enable-experiment=patterns | ||
|
||
import "../../Utils/expect.dart"; | ||
|
||
bool matches(List list) { | ||
bool result = false; | ||
switch (list) { | ||
case [> 0 & <= 2, 3]: | ||
result = true; | ||
default: | ||
}; | ||
return result; | ||
} | ||
|
||
main() { | ||
Expect.isFalse(matches([0, 3])); | ||
Expect.isFalse(matches([2, 2])); | ||
Expect.isFalse(matches([2, 3, 4])); | ||
Expect.isFalse(matches([2, "3"])); | ||
Expect.isFalse(matches(["1", 3])); | ||
Expect.isTrue(matches([1, 3])); | ||
Expect.isTrue(matches([2, 3])); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
// Copyright (c) 2022, the Dart project authors. Please see the AUTHORS file | ||
// for details. All rights reserved. Use of this source code is governed by a | ||
// BSD-style license that can be found in the LICENSE file. | ||
|
||
/// @assertion | ||
/// logicalAndPattern ::= ( logicalAndPattern '&' )? relationalPattern | ||
/// | ||
/// A pair of patterns separated by & matches only if both subpatterns match. | ||
/// Unlike logical-or patterns, the variables defined in each branch must not | ||
/// overlap, since the logical-and pattern only matches if both branches do and | ||
/// the variables in both branches will be bound. | ||
/// | ||
/// If the left branch does not match, the right branch is not evaluated. This | ||
/// only matters because patterns may invoke user-defined methods with visible | ||
/// side effects. | ||
/// | ||
/// @description Checks a logical-and in a variable declaration pattern | ||
/// @author [email protected] | ||
// SharedOptions=--enable-experiment=patterns | ||
|
||
import "../../Utils/expect.dart"; | ||
|
||
main() { | ||
var r = (3.14, name: "pi"); | ||
var ((a, name: b) & record) = r; | ||
Expect.equals(3.14, a); | ||
Expect.equals("pi", b); | ||
Expect.equals(r, record); | ||
|
||
var (x & y) = 42; | ||
Expect.equals(42, x); | ||
Expect.equals(42, y); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
// Copyright (c) 2022, the Dart project authors. Please see the AUTHORS file | ||
// for details. All rights reserved. Use of this source code is governed by a | ||
// BSD-style license that can be found in the LICENSE file. | ||
|
||
/// @assertion | ||
/// logicalAndPattern ::= ( logicalAndPattern '&' )? relationalPattern | ||
/// | ||
/// A pair of patterns separated by & matches only if both subpatterns match. | ||
/// Unlike logical-or patterns, the variables defined in each branch must not | ||
/// overlap, since the logical-and pattern only matches if both branches do and | ||
/// the variables in both branches will be bound. | ||
/// | ||
/// If the left branch does not match, the right branch is not evaluated. This | ||
/// only matters because patterns may invoke user-defined methods with visible | ||
/// side effects. | ||
/// | ||
/// @description Checks that it is a compile-time error if two branches of | ||
/// logical-and pattern define overlapping sets of variables | ||
/// @author [email protected] | ||
// SharedOptions=--enable-experiment=patterns | ||
|
||
import "patterns_lib.dart"; | ||
|
||
main() { | ||
Shape shape = Circle(1); | ||
switch (shape) { | ||
case Circle(area: var s) & Circle(area: var s): | ||
// ^ | ||
// [analyzer] unspecified | ||
// [cfe] unspecified | ||
break; | ||
case Rectangle(x: var x, y: var width) & Rectangle(:var x, :var y): | ||
// ^ | ||
// [analyzer] unspecified | ||
// [cfe] unspecified | ||
break; | ||
case Circle(area: var s1) & Circle(area: var s2) & Circle(area: var s1): | ||
// ^^ | ||
// [analyzer] unspecified | ||
// [cfe] unspecified | ||
default: | ||
print("Other"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
// Copyright (c) 2022, the Dart project authors. Please see the AUTHORS file | ||
// for details. All rights reserved. Use of this source code is governed by a | ||
// BSD-style license that can be found in the LICENSE file. | ||
|
||
/// @assertion | ||
/// logicalAndPattern ::= ( logicalAndPattern '&' )? relationalPattern | ||
/// | ||
/// A pair of patterns separated by & matches only if both subpatterns match. | ||
/// Unlike logical-or patterns, the variables defined in each branch must not | ||
/// overlap, since the logical-and pattern only matches if both branches do and | ||
/// the variables in both branches will be bound. | ||
/// | ||
/// If the left branch does not match, the right branch is not evaluated. This | ||
/// only matters because patterns may invoke user-defined methods with visible | ||
/// side effects. | ||
/// | ||
/// @description Checks that it is a compile-time error if two branches of | ||
/// logical-and pattern define overlapping sets of variables | ||
/// @author [email protected] | ||
// SharedOptions=--enable-experiment=patterns | ||
|
||
main() { | ||
var ((a, name: b) & (a, name: n)) = (3.14, name: "pi"); | ||
// ^ | ||
// [analyzer] unspecified | ||
// [cfe] unspecified | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
// Copyright (c) 2022, the Dart project authors. Please see the AUTHORS file | ||
// for details. All rights reserved. Use of this source code is governed by a | ||
// BSD-style license that can be found in the LICENSE file. | ||
|
||
/// @assertion | ||
/// logicalAndPattern ::= ( logicalAndPattern '&' )? relationalPattern | ||
/// | ||
/// A pair of patterns separated by & matches only if both subpatterns match. | ||
/// Unlike logical-or patterns, the variables defined in each branch must not | ||
/// overlap, since the logical-and pattern only matches if both branches do and | ||
/// the variables in both branches will be bound. | ||
/// | ||
/// If the left branch does not match, the right branch is not evaluated. This | ||
/// only matters because patterns may invoke user-defined methods with visible | ||
/// side effects. | ||
/// | ||
/// @description Checks that if the left branch does not match, the right branch | ||
/// is not evaluated | ||
/// @author [email protected] | ||
// SharedOptions=--enable-experiment=patterns | ||
|
||
import "patterns_lib.dart"; | ||
import "../../Utils/expect.dart"; | ||
|
||
main() { | ||
Shape shape1 = Circle(1); | ||
switch (shape1) { | ||
case Circle(area: 2) & Circle(size: 1): | ||
Expect.fail("Pattern should not match"); | ||
break; | ||
default: | ||
} | ||
Expect.equals("Circle.area=2;", shape1.log); | ||
|
||
Shape shape2 = Square(1); | ||
switch (shape2) { | ||
case Square(area: 1) & Square(area: 2) & Square(size: 1): | ||
Expect.fail("Pattern should not match"); | ||
break; | ||
default: | ||
} | ||
Expect.equals("Square.area=1;Square.area=2;", shape2.log); | ||
|
||
Shape shape3 = Square(1); | ||
switch (shape3) { | ||
case Shape(area: 1) & Shape(size: 1) & Square(area: 2) | ||
& Square(size: 2): | ||
Expect.fail("Pattern should not match"); | ||
break; | ||
default: | ||
} | ||
Expect.equals("Square.area=1;Square.size=1;Square.area=2;", shape3.log); | ||
} |
Oops, something went wrong.