From 62778cac71deeb2d028b679970295e67875f7684 Mon Sep 17 00:00:00 2001 From: "Sergey G. Grekhov" Date: Wed, 28 Dec 2022 11:05:59 +0200 Subject: [PATCH] Fixes #1620. Small fixes in patterns tests (#1623) * Fixes #1620. Missed experimental flag added * Fixes #1620. Small fixes in patterns tests * Variable pattern test fixed * Fix spec_parser failure --- .../Patterns/logical_and_A04_t01.dart | 2 +- .../Patterns/variable_A02_t02.dart | 185 ++++++------------ .../Patterns/wildcards_A01_t03.dart | 12 +- 3 files changed, 65 insertions(+), 134 deletions(-) diff --git a/LanguageFeatures/Patterns/logical_and_A04_t01.dart b/LanguageFeatures/Patterns/logical_and_A04_t01.dart index 147e7eacfc..d24b881b50 100644 --- a/LanguageFeatures/Patterns/logical_and_A04_t01.dart +++ b/LanguageFeatures/Patterns/logical_and_A04_t01.dart @@ -17,7 +17,7 @@ /// @description Checks a logical-and in a variable declaration pattern /// @author sgrekhov22@gmail.com -// SharedOptions=--enable-experiment=patterns +// SharedOptions=--enable-experiment=patterns,records import "../../Utils/expect.dart"; diff --git a/LanguageFeatures/Patterns/variable_A02_t02.dart b/LanguageFeatures/Patterns/variable_A02_t02.dart index 7a21edd912..a33dddef3d 100644 --- a/LanguageFeatures/Patterns/variable_A02_t02.dart +++ b/LanguageFeatures/Patterns/variable_A02_t02.dart @@ -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 sgrekhov22@gmail.com // 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>(); - b.expectStaticType>(); - } +// ^ +// [analyzer] unspecified +// [cfe] unspecified return "($a, $b)"; default: return "default"; @@ -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>(); - b.expectStaticType>(); - } +// ^ +// [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"; @@ -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"; @@ -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"; } @@ -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"; } @@ -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"; } @@ -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)); } diff --git a/LanguageFeatures/Patterns/wildcards_A01_t03.dart b/LanguageFeatures/Patterns/wildcards_A01_t03.dart index 49a7b9cdbd..206bcf38b1 100644 --- a/LanguageFeatures/Patterns/wildcards_A01_t03.dart +++ b/LanguageFeatures/Patterns/wildcards_A01_t03.dart @@ -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: @@ -38,7 +38,7 @@ 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" }; @@ -46,7 +46,7 @@ String test2(Record r) { String test3(Record r) { if (r case (_, 2, var x)) { - return "[_, 2, var x]"; + return "(_, 2, var x)"; } if (r case (String _, _, _)) { return "(String _, _, _)"; @@ -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)));