Skip to content

Commit

Permalink
Fixes #1620. Small fixes in patterns tests (#1623)
Browse files Browse the repository at this point in the history
* Fixes #1620. Missed experimental flag added

* Fixes #1620. Small fixes in patterns tests

* Variable pattern test fixed

* Fix spec_parser failure
  • Loading branch information
sgrekhov authored Dec 28, 2022
1 parent c1af030 commit 62778ca
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 134 deletions.
2 changes: 1 addition & 1 deletion LanguageFeatures/Patterns/logical_and_A04_t01.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
/// @description Checks a logical-and in a variable declaration pattern
/// @author [email protected]
// SharedOptions=--enable-experiment=patterns
// SharedOptions=--enable-experiment=patterns,records

import "../../Utils/expect.dart";

Expand Down
185 changes: 58 additions & 127 deletions LanguageFeatures/Patterns/variable_A02_t02.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,24 +23,20 @@
/// print('First field is int $x and second is String $s.');
/// }
/// ```
/// @description Check that if the type annotation is omitted then the
/// variable's type is inferred and the pattern matches all values. Test the
/// case when type, `var` and `final` are omitted
/// @description Check that if type, `var` and `final` are all omitted then it
/// is a compile-time error
/// @author [email protected]
// SharedOptions=--enable-experiment=patterns,records

import "../../Utils/static_type_helper.dart";
import "../../Utils/expect.dart";
import "patterns_lib.dart";

String testRecord1(Record r, [bool doTypeTest = false]) {
String testRecord1(Record r) {
switch (r) {
case (a, b):
if (doTypeTest) {
a.expectStaticType<Exactly<Object?>>();
b.expectStaticType<Exactly<Object?>>();
}
// ^
// [analyzer] unspecified
// [cfe] unspecified
return "($a, $b)";
default:
return "default";
Expand All @@ -50,31 +46,30 @@ String testRecord1(Record r, [bool doTypeTest = false]) {
String testRecord2(Record r) {
return switch (r) {
(a, b) => "($a, $b)",
// ^
// [analyzer] unspecified
// [cfe] unspecified
_ => "default"
};
}

String testRecord3(Record r, [bool doTypeTest = false]) {
String testRecord3(Record r) {
if (r case (a, b)) {
if (doTypeTest) {
a.expectStaticType<Exactly<Object?>>();
b.expectStaticType<Exactly<Object?>>();
}
// ^
// [analyzer] unspecified
// [cfe] unspecified
return "($a, $b)";
} else {
return "default";
}
}

String testList1(List l, [bool doTypeTest = false]) {
String testList1(List l) {
switch (l) {
case [a, b]:
if (doTypeTest) {
a.isOdd;
b.substring(0);
Expect.throws(() {a.whatever;});
Expect.throws(() {b.whatever;});
}
// ^
// [analyzer] unspecified
// [cfe] unspecified
return "[$a, $b]";
default:
return "default";
Expand All @@ -83,19 +78,19 @@ String testList1(List l, [bool doTypeTest = false]) {

String testList2(List l) {
return switch (l) {
[a, b] => "[$a, $b]",
[a, b] => "[$a, $b]",
// ^
// [analyzer] unspecified
// [cfe] unspecified
_ => "default"
};
}

String testList3(List l, [bool doTypeTest = false]) {
String testList3(List l) {
if (l case [a, b]) {
if (doTypeTest) {
a.isOdd;
b.substring(0);
Expect.throws(() {a.whatever;});
Expect.throws(() {b.whatever;});
}
// ^
// [analyzer] unspecified
// [cfe] unspecified
return "[$a, $b]";
} else {
return "default";
Expand All @@ -105,13 +100,10 @@ String testList3(List l, [bool doTypeTest = false]) {
String testMap1(Map m) {
switch (m) {
case {1: a}:
a.isOdd;
Expect.throws(() {a.whatever;});
// ^
// [analyzer] unspecified
// [cfe] unspecified
return "{1: $a}";
case {2: b}:
b.substring(0);
Expect.throws(() {b.whatever;});
return "{2: $b}";
default:
return "default";
}
Expand All @@ -120,21 +112,19 @@ String testMap1(Map m) {
String testMap2(Map m) {
return switch (m) {
{1: a} => "{1: $a}",
{2: b} => "{2: $b}",
// ^
// [analyzer] unspecified
// [cfe] unspecified
_ => "default"
};
}

String testMap3(Map m) {
if (m case {1: a}) {
a.isOdd;
Expect.throws(() {a.whatever;});
// ^
// [analyzer] unspecified
// [cfe] unspecified
return "{1: $a}";
}
if (m case {2: final b}) {
b.substring(0);
Expect.throws(() {b.whatever;});
return "{2: $b}";
} else {
return "default";
}
Expand All @@ -143,9 +133,10 @@ String testMap3(Map m) {
String testObject1(Shape shape) {
switch (shape) {
case Square(area: a):
// ^
// [analyzer] unspecified
// [cfe] unspecified
return "a=$a";
case Rectangle(area: b):
return "b=$b";
default:
return "default";
}
Expand All @@ -154,98 +145,38 @@ String testObject1(Shape shape) {
String testObject2(Shape shape) {
return switch (shape) {
Square(area: a) => "a=$a",
Rectangle(area: b) => "b=$b",
// ^
// [analyzer] unspecified
// [cfe] unspecified
_ => "default"
};
}

String testObject3(Shape shape) {
if (shape case Square(area: a)) {
// ^
// [analyzer] unspecified
// [cfe] unspecified
return "a=$a";
}
if (shape case Rectangle(area: b)) {
return "b=$b";
} else {
return "default";
}
}

main() {
Expect.equals("(1, x)", testRecord1((1, "x"), true));
Expect.equals("(1, 2)", testRecord1((1, 2)));
Expect.equals("(true, false)", testRecord1((true, false)));
Expect.equals("default", testRecord1((1, 2, 3)));
Expect.equals("default", testRecord1(("x",)));

Expect.equals("(1, x)", testRecord2((1, "x")));
Expect.equals("(1, 2)", testRecord2((1, 2)));
Expect.equals("(true, false)", testRecord2((true, false)));
Expect.equals("default", testRecord2((1, 2, 3)));
Expect.equals("default", testRecord2(("x",)));

Expect.equals("(1, x)", testRecord3((1, "x"), true));
Expect.equals("(1, 2)", testRecord3((1, 2)));
Expect.equals("(true, false)", testRecord3((true, false)));
Expect.equals("default", testRecord3((1, 2, 3)));
Expect.equals("default", testRecord3(("x",)));

Expect.equals("[1, x]", testList1([1, "x"], true));
Expect.equals("[1, 2]", testList1([1, 2]));
Expect.equals("[true, false]", testList1([true, false]));
Expect.equals("default", testList1([1, 2, 3]));
Expect.equals("default", testList1(["x"]));

Expect.equals("[1, x]", testList2([1, "x"]));
Expect.equals("[1, 2]", testList2([1, 2]));
Expect.equals("[true, false]", testList2([true, false]));
Expect.equals("default", testList2([1, 2, 3]));
Expect.equals("default", testList2(["x"]));

Expect.equals("[1, x]", testList3([1, "x"]));
Expect.equals("[1, 2]", testList3([1, 2]));
Expect.equals("[true, false]", testList3([true, false]));
Expect.equals("default", testList3([1, 2, 3]));
Expect.equals("default", testList3(["x"]));

Expect.equals("{1: 2}", testMap1({1: 2}));
Expect.equals("{1: 3}", testMap1({1: 3, 3: 4}));
Expect.equals("{2: x}", testMap1({2: "x"}));
Expect.equals("{2: true}", testMap1({2: true}));
Expect.equals("default", testMap1({3: 3}));
Expect.equals("default", testMap1({}));

Expect.equals("{1: 2}", testMap2({1: 2}));
Expect.equals("{1: 3}", testMap2({1: 3, 3: 4}));
Expect.equals("{2: x}", testMap2({2: "x"}));
Expect.equals("{2: true}", testMap2({2: true}));
Expect.equals("default", testMap2({3: 3}));
Expect.equals("default", testMap2({}));

Expect.equals("{1: 2}", testMap3({1: 2}));
Expect.equals("{1: 3}", testMap3({1: 3, 3: 4}));
Expect.equals("{2: x}", testMap3({2: "x"}));
Expect.equals("{2: true}", testMap3({2: true}));
Expect.equals("default", testMap3({3: 3}));
Expect.equals("default", testMap3({}));

Expect.equals("a=1.00", testObject1(Square(1)));
Expect.equals("a=4.00", testObject1(Square(2)));
Expect.equals("b=2.00", testObject1(Rectangle(1, 2)));
Expect.equals("b=4.00", testObject1(Rectangle(2, 2)));
Expect.equals("default", testObject1(Circle(1)));
Expect.equals("default", testObject1(Shape()));

Expect.equals("a=1.00", testObject2(Square(1)));
Expect.equals("a=4.00", testObject2(Square(2)));
Expect.equals("b=2.00", testObject2(Rectangle(1, 2)));
Expect.equals("b=4.00", testObject2(Rectangle(2, 2)));
Expect.equals("default", testObject2(Circle(1)));
Expect.equals("default", testObject2(Shape()));

Expect.equals("a=1.00", testObject3(Square(1)));
Expect.equals("a=4.00", testObject3(Square(2)));
Expect.equals("b=2.00", testObject3(Rectangle(1, 2)));
Expect.equals("b=4.00", testObject3(Rectangle(2, 2)));
Expect.equals("default", testObject3(Circle(1)));
Expect.equals("default", testObject3(Shape()));
testRecord1((1, "x"));
testRecord2((1, "x"));
testRecord3((1, "x"));

testList1([1, "x"]);
testList2([1, "x"]);
testList3([1, "x"]);

testMap1({1: 2});
testMap2({1: 2});
testMap3({1: 2});

testObject1(Square(1));
testObject2(Square(1));
testObject3(Square(1));
}
12 changes: 6 additions & 6 deletions LanguageFeatures/Patterns/wildcards_A01_t03.dart
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import "../../Utils/expect.dart";
String test1(Record r) {
switch (r) {
case (_, 2, var x):
return "[_, 2, var x]";
return "(_, 2, var x)";
case (String _, _, _):
return "(String _, _, _)";
default:
Expand All @@ -38,15 +38,15 @@ String test1(Record r) {

String test2(Record r) {
return switch (r) {
(_, 2, var x) => "[_, 2, var x]",
(_, 2, var x) => "(_, 2, var x)",
(String _, _, _) => "(String _, _, _)",
_ => "default"
};
}

String test3(Record r) {
if (r case (_, 2, var x)) {
return "[_, 2, var x]";
return "(_, 2, var x)";
}
if (r case (String _, _, _)) {
return "(String _, _, _)";
Expand All @@ -72,17 +72,17 @@ main() {
Expect.throws(() {
var (_, String _, _, num _) = r2;
});
Expect.equals("[_, 2, var x]", test1((1, 2, "3")));
Expect.equals("(_, 2, var x)", test1((1, 2, "3")));
Expect.equals("(String _, _, _)", test1(("0", 1, 2)));
Expect.equals("default", test1((1, 1, 3)));
Expect.equals("default", test1(((1, 2), 3, 4)));

Expect.equals("[_, 2, var x]", test2((1, 2, "3")));
Expect.equals("(_, 2, var x)", test2((1, 2, "3")));
Expect.equals("(String _, _, _)", test2(("0", 1, 2)));
Expect.equals("default", test2((1, 1, 3)));
Expect.equals("default", test2(((1, 2), 3, 4)));

Expect.equals("[_, 2, var x]", test3((1, 2, "3")));
Expect.equals("(_, 2, var x)", test3((1, 2, "3")));
Expect.equals("(String _, _, _)", test3(("0", 1, 2)));
Expect.equals("default", test3((1, 1, 3)));
Expect.equals("default", test3(((1, 2), 3, 4)));
Expand Down

0 comments on commit 62778ca

Please sign in to comment.