Skip to content

Commit

Permalink
#1401. [Patterns] logical-and pattern tests added (#1534)
Browse files Browse the repository at this point in the history
Authored by @sgrekhov.

Logical-and pattern tests added.
  • Loading branch information
sgrekhov authored Nov 7, 2022
1 parent b83c4e0 commit 31e395d
Show file tree
Hide file tree
Showing 9 changed files with 364 additions and 12 deletions.
45 changes: 45 additions & 0 deletions LanguageFeatures/Patterns/logical_and_A01_t01.dart
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));
}
37 changes: 37 additions & 0 deletions LanguageFeatures/Patterns/logical_and_A01_t02.dart
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]));
}
41 changes: 41 additions & 0 deletions LanguageFeatures/Patterns/logical_and_A02_t01.dart
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);
}
42 changes: 42 additions & 0 deletions LanguageFeatures/Patterns/logical_and_A02_t02.dart
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]));
}
34 changes: 34 additions & 0 deletions LanguageFeatures/Patterns/logical_and_A03_t01.dart
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);
}
45 changes: 45 additions & 0 deletions LanguageFeatures/Patterns/logical_and_A04_t01.dart
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");
}
}
28 changes: 28 additions & 0 deletions LanguageFeatures/Patterns/logical_and_A04_t02.dart
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
}
54 changes: 54 additions & 0 deletions LanguageFeatures/Patterns/logical_and_A05_t01.dart
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);
}
Loading

0 comments on commit 31e395d

Please sign in to comment.