-
Notifications
You must be signed in to change notification settings - Fork 28
#1401. [Patterns] Logical-or tests added #1520
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
bc58e06
#1401. [Patterns] Logical-or tests added
sgrekhov 811da27
#1401. More test cases added
sgrekhov c4bf30a
#1401. Logical-or tests fixed
sgrekhov 27f5593
#1401. [Patterns] Missing log added
sgrekhov 037c1a5
#1401. [Patterns] Logical-or tests improved
sgrekhov 208055c
#1401. Logical-or tests fixed
sgrekhov ce14909
#1401. logical_or_A04_t02.dart test fixed
sgrekhov File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,48 @@ | ||
// 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 logicalOrPattern ::= ( logicalOrPattern '|' )? logicalAndPattern | ||
/// | ||
/// A pair of patterns separated by | matches if either of the branches match | ||
/// | ||
/// A logical-or pattern may match even if one of its branches does not. That | ||
/// means that any variables in the non-matching branch would not be | ||
/// initialized. To avoid problems stemming from that, the following | ||
/// restrictions apply: | ||
/// | ||
/// The two branches must define the same set of variables. | ||
/// | ||
/// If the left branch matches, the right branch is not evaluated. This | ||
/// determines which value the variable gets if both branches would have | ||
/// matched. In that case, it will always be the value from the left branch. | ||
/// | ||
/// @description Checks logical-or 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.red | Color.yellow | Color.blue => 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 hidden or 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,40 @@ | ||
// 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 logicalOrPattern ::= ( logicalOrPattern '|' )? logicalAndPattern | ||
/// | ||
/// A pair of patterns separated by | matches if either of the branches match | ||
/// | ||
/// A logical-or pattern may match even if one of its branches does not. That | ||
/// means that any variables in the non-matching branch would not be | ||
/// initialized. To avoid problems stemming from that, the following | ||
/// restrictions apply: | ||
/// | ||
/// The two branches must define the same set of variables. | ||
/// | ||
/// If the left branch matches, the right branch is not evaluated. This | ||
/// determines which value the variable gets if both branches would have | ||
/// matched. In that case, it will always be the value from the left branch. | ||
/// | ||
/// @description Checks logical-or subpattern in a switch expression | ||
/// @author [email protected] | ||
|
||
// SharedOptions=--enable-experiment=patterns | ||
|
||
import "../../Utils/expect.dart"; | ||
|
||
bool matches(List list) => switch (list) { | ||
case [1 | 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 hidden or 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,46 @@ | ||
// 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 logicalOrPattern ::= ( logicalOrPattern '|' )? logicalAndPattern | ||
/// | ||
/// A pair of patterns separated by | matches if either of the branches match | ||
/// | ||
/// A logical-or pattern may match even if one of its branches does not. That | ||
/// means that any variables in the non-matching branch would not be | ||
/// initialized. To avoid problems stemming from that, the following | ||
/// restrictions apply: | ||
/// | ||
/// The two branches must define the same set of variables. | ||
/// | ||
/// If the left branch matches, the right branch is not evaluated. This | ||
/// determines which value the variable gets if both branches would have | ||
/// matched. In that case, it will always be the value from the left branch. | ||
/// | ||
/// @description Checks logical-or pattern in a switch statement | ||
/// @author [email protected] | ||
|
||
// SharedOptions=--enable-experiment=patterns | ||
|
||
import "patterns_lib.dart"; | ||
import "../../Utils/expect.dart"; | ||
|
||
void test(Shape shape, double expectedArea, Type expectedType, bool match) { | ||
switch (shape) { | ||
case Square(area: var s) | Circle(area: var s): | ||
Expect.equals(s, expectedArea); | ||
Expect.equals(expectedType, shape.runtimeType); | ||
Expect.isTrue(match); | ||
break; | ||
default: | ||
Expect.equals(s, expectedArea); | ||
Expect.equals(expectedType, shape.runtimeType); | ||
Expect.isFalse(match); | ||
} | ||
} | ||
|
||
main() { | ||
test(Circle(1), 3.14, Circle, true); | ||
test(Square(2), 4, Square, true); | ||
test(Rectangle(2, 1), 2, Rectangle, false); | ||
} |
This file contains hidden or 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 logicalOrPattern ::= ( logicalOrPattern '|' )? logicalAndPattern | ||
/// | ||
/// A pair of patterns separated by | matches if either of the branches match | ||
/// | ||
/// A logical-or pattern may match even if one of its branches does not. That | ||
/// means that any variables in the non-matching branch would not be | ||
/// initialized. To avoid problems stemming from that, the following | ||
/// restrictions apply: | ||
/// | ||
/// The two branches must define the same set of variables. | ||
/// | ||
/// If the left branch matches, the right branch is not evaluated. This | ||
/// determines which value the variable gets if both branches would have | ||
/// matched. In that case, it will always be the value from the left branch. | ||
/// | ||
/// @description Checks logical-or 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 [1 | 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 hidden or 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,46 @@ | ||
// 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 logicalOrPattern ::= ( logicalOrPattern '|' )? logicalAndPattern | ||
/// | ||
/// A pair of patterns separated by | matches if either of the branches match | ||
/// | ||
/// A logical-or pattern may match even if one of its branches does not. That | ||
/// means that any variables in the non-matching branch would not be | ||
/// initialized. To avoid problems stemming from that, the following | ||
/// restrictions apply: | ||
/// | ||
/// The two branches must define the same set of variables. | ||
/// | ||
/// If the left branch matches, the right branch is not evaluated. This | ||
/// determines which value the variable gets if both branches would have | ||
/// matched. In that case, it will always be the value from the left branch. | ||
/// | ||
/// @description Checks that it is a compile-time error if two branches of | ||
/// logical-or pattern define different sets of variables | ||
/// @author [email protected] | ||
|
||
// SharedOptions=--enable-experiment=patterns | ||
|
||
import "patterns_lib.dart"; | ||
|
||
main() { | ||
Shape shape = Circle(1); | ||
switch (shape) { | ||
case Square(area: var s1) | Circle(area: var s2): | ||
// ^^ | ||
// [analyzer] unspecified | ||
// [cfe] unspecified | ||
print("Square or Circle"); | ||
break; | ||
case Rectangle(x: var x, y: var width) | Rectangle(:var x, :var y): | ||
// ^ | ||
// [analyzer] unspecified | ||
// [cfe] unspecified | ||
print("Rectangle"); | ||
break; | ||
default: | ||
print("Other"); | ||
} | ||
} |
This file contains hidden or 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,56 @@ | ||
// 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 logicalOrPattern ::= ( logicalOrPattern '|' )? logicalAndPattern | ||
/// | ||
/// A pair of patterns separated by | matches if either of the branches match | ||
/// | ||
/// A logical-or pattern may match even if one of its branches does not. That | ||
/// means that any variables in the non-matching branch would not be | ||
/// initialized. To avoid problems stemming from that, the following | ||
/// restrictions apply: | ||
/// | ||
/// The two branches must define the same set of variables. | ||
/// | ||
/// If the left branch matches, the right branch is not evaluated. This | ||
/// determines which value the variable gets if both branches would have | ||
/// matched. In that case, it will always be the value from the left branch. | ||
/// | ||
/// @description Checks that if the left branch matches, the right branch is not | ||
/// evaluated | ||
eernstg marked this conversation as resolved.
Show resolved
Hide resolved
|
||
/// @author [email protected] | ||
|
||
// SharedOptions=--enable-experiment=patterns | ||
|
||
import "patterns_lib.dart"; | ||
import "../../Utils/expect.dart"; | ||
|
||
main() { | ||
Shape shape1 = Square(1); | ||
switch (shape1) { | ||
case Square(area: var s) | Shape(area: var s): | ||
Expect.equals("Square.area", shape1.log); | ||
break; | ||
default: | ||
print("Other"); | ||
} | ||
|
||
Shape shape2 = Square(2); | ||
switch (shape2) { | ||
case Square(area: 2) | Square(area: 4) | Square(area: 4): | ||
Expect.equals("Circle.area=2;Circle.area=4;", shape2.log); | ||
break; | ||
default: | ||
print("Other"); | ||
} | ||
|
||
Shape shape3 = Shape(); | ||
switch (shape3) { | ||
case Circle(area: 0) | Square(area: 1) | Shape(area: 0): | ||
Expect.equals("Shape.area=0;", shape3.log); | ||
break; | ||
default: | ||
print("Other"); | ||
} | ||
} |
This file contains hidden or 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,72 @@ | ||
// 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 logicalOrPattern ::= ( logicalOrPattern '|' )? logicalAndPattern | ||
/// | ||
/// A pair of patterns separated by | matches if either of the branches match | ||
/// | ||
/// A logical-or pattern may match even if one of its branches does not. That | ||
/// means that any variables in the non-matching branch would not be | ||
/// initialized. To avoid problems stemming from that, the following | ||
/// restrictions apply: | ||
/// | ||
/// The two branches must define the same set of variables. | ||
/// | ||
/// If the left branch matches, the right branch is not evaluated. This | ||
/// determines which value the variable gets if both branches would have | ||
/// matched. In that case, it will always be the value from the left branch. | ||
/// | ||
/// @description Checks that if the left branch doesn't match, then the right | ||
/// branch is evaluated | ||
/// @author [email protected] | ||
|
||
// SharedOptions=--enable-experiment=patterns | ||
|
||
import "patterns_lib.dart"; | ||
import "../../Utils/expect.dart"; | ||
|
||
main() { | ||
Shape shape1 = Square(1); | ||
switch (shape1) { | ||
case Square(area: 2) | Square(area: 1): | ||
Expect.equals("Square.area=2;Square.area=1;", shape1.log); | ||
break; | ||
default: | ||
print("Other"); | ||
} | ||
|
||
Shape shape2 = Shape(); | ||
switch (shape2) { | ||
case Square(area: 2) | Rectangle(area: 1) | Shape(area: 0): | ||
Expect.equals("Shape.area=0;", shape2.log); | ||
break; | ||
default: | ||
print("Other"); | ||
} | ||
|
||
Shape shape3 = Circle(1); | ||
switch (shape2) { | ||
case Circle(area: 2) | Circle(area: 1) | Circle(area: 0) | ||
| Circle(area: 3.14): | ||
Expect.equals( | ||
"Circle.area=2;Circle.area=1;Circle.area=0;Circle.area=3.14;", | ||
shape3.log); | ||
break; | ||
default: | ||
print("Other"); | ||
} | ||
|
||
Shape shape4 = Rectangle(1, 2); | ||
bool other = false; | ||
switch (shape4) { | ||
case Rectangle(area: 3) | Rectangle(area: 1) | Rectangle(area: 42): | ||
Expect.fail("No branches should match"); | ||
break; | ||
default: | ||
other = true; | ||
Expect.equals("Rectangle.area=3;Rectangle.area=1;Rectangle.area=42;", | ||
shape4.log); | ||
} | ||
Expect.isTrue(other); | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.