Robert Nystrom | 6de7277 | 2023-03-17 23:04:25 +0000 | [diff] [blame] | 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 | |
Robert Nystrom | 6de7277 | 2023-03-17 23:04:25 +0000 | [diff] [blame] | 5 | /// Test that implicit call tear-offs are inserted based on the pattern's |
| 6 | /// context type, but not when destructuring. |
| 7 | |
| 8 | import "package:expect/expect.dart"; |
| 9 | |
| 10 | main() { |
| 11 | testValueExpression(); |
| 12 | testDestructureRefutable(); |
| 13 | } |
| 14 | |
| 15 | class C { |
| 16 | const C(); |
| 17 | |
| 18 | int call(int x) => x; |
| 19 | } |
| 20 | |
| 21 | class Box<T> { |
| 22 | final T value; |
| 23 | Box(this.value); |
| 24 | } |
| 25 | |
| 26 | typedef IntFn = int Function(int); |
| 27 | |
| 28 | void testValueExpression() { |
| 29 | // Inserts tear-off in value expression. |
| 30 | var (IntFn a) = C(); |
| 31 | Expect.isTrue(a is IntFn); |
| 32 | Expect.isFalse(a is C); |
| 33 | |
| 34 | var (IntFn b,) = (C(),); |
| 35 | Expect.isTrue(b is IntFn); |
| 36 | Expect.isFalse(b is C); |
| 37 | |
| 38 | var [IntFn c] = [C()]; |
| 39 | Expect.isTrue(c is IntFn); |
| 40 | Expect.isFalse(c is C); |
| 41 | |
| 42 | var {'x': IntFn d} = {'x': C()}; |
| 43 | Expect.isTrue(d is IntFn); |
| 44 | Expect.isFalse(d is C); |
| 45 | |
| 46 | var Box<IntFn>(value: e) = Box(C()); |
| 47 | Expect.isTrue(e is IntFn); |
| 48 | Expect.isFalse(e is C); |
| 49 | } |
| 50 | |
| 51 | void testDestructureRefutable() { |
| 52 | // Does not tear-off during destructuring. In a refutable pattern, this means |
| 53 | // the value doesn't match the tested type. |
| 54 | (C,) record = (C(),); |
| 55 | if (record case (IntFn b,)) { |
| 56 | Expect.fail('Should not match.'); |
| 57 | } |
| 58 | |
| 59 | List<C> list = [C()]; |
| 60 | if (list case [IntFn c]) { |
| 61 | Expect.fail('Should not match.'); |
| 62 | } |
| 63 | |
| 64 | Map<String, C> map = {'x': C()}; |
| 65 | if (map case {'x': IntFn d}) { |
| 66 | Expect.fail('Should not match.'); |
| 67 | } |
| 68 | |
| 69 | Box<C> box = Box(C()); |
| 70 | if (box case Box(value: IntFn e)) { |
| 71 | Expect.fail('Should not match.'); |
| 72 | } |
| 73 | } |