|
| 1 | +// Copyright (c) 2023, the Dart project authors. Please see the AUTHORS file |
| 2 | +// for details. All rights reserved. Use of this source code is governed by a |
| 3 | +// BSD-style license that can be found in the LICENSE file. |
| 4 | + |
| 5 | +/// @assertion Switch expression with a list as a matched type can be exhaustive |
| 6 | +/// |
| 7 | +/// @description Check that it is no compile-time error if a matched type of a |
| 8 | +/// switch expression is an extension type with a `List` as a representation |
| 9 | +/// type and all cases are exhaustive. Test rest element at the beginning of |
| 10 | +/// the list pattern |
| 11 | + |
| 12 | +
|
| 13 | +// SharedOptions=--enable-experiment=inline-class |
| 14 | + |
| 15 | +import "../../Utils/expect.dart"; |
| 16 | + |
| 17 | +extension type ET1<T>(List<T> _) {} |
| 18 | +extension type ET2<T>(List<T> _) implements List<T> {} |
| 19 | + |
| 20 | +String test1_1(ET1<int> l) => |
| 21 | + switch (l) { |
| 22 | + [] => "0", |
| 23 | + [_] => "1", |
| 24 | + [_, _] => "2", |
| 25 | + <int?>[..., _, _] => "2+" |
| 26 | + }; |
| 27 | + |
| 28 | +String test1_2(ET2<int> l) => |
| 29 | + switch (l) { |
| 30 | + [] => "0", |
| 31 | + [_] => "1", |
| 32 | + [_, _] => "2", |
| 33 | + <int?>[..., _, _] => "2+" |
| 34 | + }; |
| 35 | + |
| 36 | +String test2_1(ET1<bool> l) => |
| 37 | + switch (l) { |
| 38 | + [] => "0", |
| 39 | + [true] => "1_1", |
| 40 | + [false] => "1_2", |
| 41 | + [_, true] => "2_1", |
| 42 | + [_, false] => "2_2", |
| 43 | + [... var r1, true] => "3_1", |
| 44 | + [... final r2, false] => "3_2" |
| 45 | + }; |
| 46 | + |
| 47 | +String test2_2(ET2<bool> l) => |
| 48 | + switch (l) { |
| 49 | + [] => "0", |
| 50 | + [true] => "1_1", |
| 51 | + [false] => "1_2", |
| 52 | + [_, true] => "2_1", |
| 53 | + [_, false] => "2_2", |
| 54 | + [... var r1, true] => "3_1", |
| 55 | + [... final r2, false] => "3_2" |
| 56 | + }; |
| 57 | + |
| 58 | +main() { |
| 59 | + Expect.equals("0", test1_1(ET1([]))); |
| 60 | + Expect.equals("1", test1_1(ET1([1]))); |
| 61 | + Expect.equals("2", test1_1(ET1([1, 2]))); |
| 62 | + Expect.equals("2+", test1_1(ET1([1, 2, 3]))); |
| 63 | + |
| 64 | + Expect.equals("0", test1_2(ET2([]))); |
| 65 | + Expect.equals("1", test1_2(ET2([1]))); |
| 66 | + Expect.equals("2", test1_2(ET2([1, 2]))); |
| 67 | + Expect.equals("2+", test1_2(ET2([1, 2, 3]))); |
| 68 | + |
| 69 | + Expect.equals("0", test2_1(ET1([]))); |
| 70 | + Expect.equals("1_1", test2_1(ET1([true]))); |
| 71 | + Expect.equals("1_2", test2_1(ET1([false]))); |
| 72 | + Expect.equals("2_1", test2_1(ET1([true, true]))); |
| 73 | + Expect.equals("2_2", test2_1(ET1([true, false]))); |
| 74 | + Expect.equals("3_1", test2_1(ET1([true, true, true]))); |
| 75 | + Expect.equals("3_2", test2_1(ET1([true, false, false]))); |
| 76 | + |
| 77 | + Expect.equals("0", test2_2(ET2([]))); |
| 78 | + Expect.equals("1_1", test2_2(ET2([true]))); |
| 79 | + Expect.equals("1_2", test2_2(ET2([false]))); |
| 80 | + Expect.equals("2_1", test2_2(ET2([true, true]))); |
| 81 | + Expect.equals("2_2", test2_2(ET2([true, false]))); |
| 82 | + Expect.equals("3_1", test2_2(ET2([true, true, true]))); |
| 83 | + Expect.equals("3_2", test2_2(ET2([true, false, false]))); |
| 84 | +} |
0 commit comments