From fa686fc48781b433304b7028f2da8bc0f1fb40a5 Mon Sep 17 00:00:00 2001 From: "Sergey G. Grekhov" Date: Fri, 31 Mar 2023 13:58:31 +0300 Subject: [PATCH] #1401. Additional test for the rest element of a list pattern (#1980) --- LanguageFeatures/Patterns/list_A04_t07.dart | 102 ++++++++++++++++++++ 1 file changed, 102 insertions(+) create mode 100644 LanguageFeatures/Patterns/list_A04_t07.dart diff --git a/LanguageFeatures/Patterns/list_A04_t07.dart b/LanguageFeatures/Patterns/list_A04_t07.dart new file mode 100644 index 0000000000..71914d16db --- /dev/null +++ b/LanguageFeatures/Patterns/list_A04_t07.dart @@ -0,0 +1,102 @@ +// 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 +/// listPattern ::= typeArguments? '[' listPatternElements? ']' +/// listPatternElements ::= listPatternElement ( ',' listPatternElement )* ','? +/// listPatternElement ::= pattern | restPattern +/// restPattern ::= '...' pattern? +/// +/// A list pattern may contain a rest element which allows matching lists of +/// arbitrary lengths. If a rest element is present and has a subpattern, all of +/// the elements not matched by other subpatterns are collected into a new list +/// and that list is matched against the rest subpattern. +/// +/// var [a, b, ...rest, c, d] = [1, 2, 3, 4, 5, 6, 7]; +/// print('$a $b $rest $c $d'); // Prints "1 2 [3, 4, 5] 6 7". +/// +/// @description Check that if a rest element is present and has a subpattern, +/// all of the elements not matched by other subpatterns are collected into a +/// new list and that list is matched against the rest subpattern. Test rest +/// pattern with a list subpattern +/// @author sgrekhov22@gmail.com + +// SharedOptions=--enable-experiment=patterns + +import "../../Utils/expect.dart"; + +String test1(List list) { + return switch (list) { + [1, 2, 3, 4, ...[5]] => "[1, 2, 3, 4, ...[5]]", + [2, ...[3], 5, 6] => "[2, ...[3], 5, 6]", + [... /* comment */[..., 5], 6, 7, 8] => "[...[..., 5], 6, 7, 8]", + _ => "default" + }; +} + +String test2(List list) { + switch (list) { + case [1, 2, 3, 4, ...[5]]: + return "[1, 2, 3, 4, ...[5]]"; + case [2, ...[3], 5, 6]: + return "[2, ...[3], 5, 6]"; + case [... /* comment */[..., 5], 6, 7, 8]: + return "[...[..., 5], 6, 7, 8]"; + default: + return "default"; + } +} + +String test3(List list) { + if (list case [1, 2, 3, 4, ...[5]]) { + return "[1, 2, 3, 4, ...[5]]"; + } + if (list case [2, ...[3], 5, 6]) { + return "[2, ...[3], 5, 6]"; + } + if (list case [... /* comment */[..., 5], 6, 7, 8]) { + return "[...[..., 5], 6, 7, 8]"; + } + return "default"; +} + +main() { + var [a1, b1, ...[c1, ...]] = [1, 2, 3, 4, 5]; + Expect.equals(1, a1); + Expect.equals(2, b1); + Expect.equals(3, c1); + + final [a2, ...[c2, d2], b2] = [2, 3, 4, 5]; + Expect.equals(2, a2); + Expect.equals(5, b2); + Expect.equals(3, c2); + Expect.equals(4, d2); + + var [... /* comment */[..., c3], a3, b3] = [1, 2, 3, 4, 5]; + Expect.equals(4, a3); + Expect.equals(5, b3); + Expect.equals(3, c3); + + Expect.equals("[1, 2, 3, 4, ...[5]]", test1([1, 2, 3, 4, 5])); + Expect.equals("[2, ...[3], 5, 6]", test1([2, 3, 5, 6])); + Expect.equals("[...[..., 5], 6, 7, 8]", test1([3, 4, 5, 6, 7, 8])); + Expect.equals("default", test1([1, 2])); + Expect.equals("default", test1([1, 2, 3, 4, 5, 6])); + Expect.equals("default", test1([2, 3, 4, 5, 6])); + Expect.equals("default", test1([1, 2, 3, 4, 5, 6])); + Expect.equals("[1, 2, 3, 4, ...[5]]", test2([1, 2, 3, 4, 5])); + Expect.equals("[2, ...[3], 5, 6]", test2([2, 3, 5, 6])); + Expect.equals("[...[..., 5], 6, 7, 8]", test2([3, 4, 5, 6, 7, 8])); + Expect.equals("default", test2([1, 2])); + Expect.equals("default", test2([1, 2, 3, 4, 5, 6])); + Expect.equals("default", test2([2, 3, 4, 5, 6])); + Expect.equals("default", test2([1, 2, 3, 4, 5, 6])); + Expect.equals("[1, 2, 3, 4, ...[5]]", test3([1, 2, 3, 4, 5])); + Expect.equals("[2, ...[3], 5, 6]", test3([2, 3, 5, 6])); + Expect.equals("[...[..., 5], 6, 7, 8]", test3([3, 4, 5, 6, 7, 8])); + Expect.equals("default", test3([1, 2])); + Expect.equals("default", test3([1, 2, 3, 4, 5, 6])); + Expect.equals("default", test3([2, 3, 4, 5, 6])); + Expect.equals("default", test3([1, 2, 3, 4, 5, 6])); +}