OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2016, 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 // Check that both `=` and `:` are allowed for named parameters. |
| 6 |
| 7 import "package:expect/expect.dart"; |
| 8 |
| 9 // Default values are not allowed on typedefs. |
| 10 typedef int F1({x = 3, y}); /// 01: compile-time error |
| 11 |
| 12 typedef int functype({x, y, z}); |
| 13 |
| 14 int topF({x = 3, y : 5, z}) => x * y * (z ?? 2); |
| 15 |
| 16 class A { |
| 17 int x; |
| 18 int y; |
| 19 int z; |
| 20 A({this.x = 3, this.y : 5, z }) : z = z ?? 2; |
| 21 A.redirect({x = 3, y : 5, z}) : this(x: x, y: y, z: z); |
| 22 factory A.factory({x = 3, y : 5, z}) => new A(x: x, y: y, z: z ?? 2); |
| 23 factory A.redirectFactory({x, y, z}) = A; |
| 24 |
| 25 // Default values are not allowed on redirecting factory constructors. |
| 26 factory A.badRedirectFactory({x = 3, y}) = A; /// 02: compile-time error |
| 27 |
| 28 int get value => x * y * z; |
| 29 |
| 30 static int staticF({x = 3, y : 5, z}) => x * y * (z ?? 2); |
| 31 int instanceF({x = 3, y : 5, z}) => x * y * (z ?? 2); |
| 32 } |
| 33 |
| 34 main() { |
| 35 var a = new A(); |
| 36 |
| 37 int local({x = 3, y : 5, z}) => x * y * (z ?? 2); |
| 38 var expr = ({x = 3, y : 5, z}) => x * y * (z ?? 2); |
| 39 var tearOff = a.instanceF; |
| 40 |
| 41 test(function) { |
| 42 Expect.equals(30, function()); |
| 43 Expect.equals(70, function(x: 7)); |
| 44 Expect.equals(42, function(y: 7)); |
| 45 Expect.equals(28, function(x: 7, y: 2)); |
| 46 Expect.equals(15, function(z: 1)); |
| 47 Expect.equals(21, function(y: 7, z: 1)); |
| 48 Expect.equals(35, function(x: 7, z: 1)); |
| 49 Expect.equals(14, function(x: 7, y: 2, z: 1)); |
| 50 Expect.isTrue(function is functype); |
| 51 } |
| 52 |
| 53 test(topF); |
| 54 test(A.staticF); |
| 55 test(a.instanceF); |
| 56 test(local); |
| 57 test(expr); |
| 58 test(tearOff); |
| 59 |
| 60 // Can't tear off constructors. |
| 61 Expect.equals(30, new A().value); |
| 62 Expect.equals(70, new A(x: 7).value); |
| 63 Expect.equals(42, new A(y: 7).value); |
| 64 Expect.equals(28, new A(x: 7, y: 2).value); |
| 65 Expect.equals(15, new A(z: 1).value); |
| 66 Expect.equals(21, new A(y: 7, z: 1).value); |
| 67 Expect.equals(35, new A(x: 7, z: 1).value); |
| 68 Expect.equals(14, new A(x: 7, y: 2, z: 1).value); |
| 69 |
| 70 Expect.equals(30, new A.redirect().value); |
| 71 Expect.equals(70, new A.redirect(x: 7).value); |
| 72 Expect.equals(42, new A.redirect(y: 7).value); |
| 73 Expect.equals(28, new A.redirect(x: 7, y: 2).value); |
| 74 Expect.equals(15, new A.redirect(z: 1).value); |
| 75 Expect.equals(21, new A.redirect(y: 7, z: 1).value); |
| 76 Expect.equals(35, new A.redirect(x: 7, z: 1).value); |
| 77 Expect.equals(14, new A.redirect(x: 7, y: 2, z: 1).value); |
| 78 |
| 79 Expect.equals(30, new A.factory().value); |
| 80 Expect.equals(70, new A.factory(x: 7).value); |
| 81 Expect.equals(42, new A.factory(y: 7).value); |
| 82 Expect.equals(28, new A.factory(x: 7, y: 2).value); |
| 83 Expect.equals(15, new A.factory(z: 1).value); |
| 84 Expect.equals(21, new A.factory(y: 7, z: 1).value); |
| 85 Expect.equals(35, new A.factory(x: 7, z: 1).value); |
| 86 Expect.equals(14, new A.factory(x: 7, y: 2, z: 1).value); |
| 87 |
| 88 Expect.equals(30, new A.redirectFactory().value); |
| 89 Expect.equals(70, new A.redirectFactory(x: 7).value); |
| 90 Expect.equals(42, new A.redirectFactory(y: 7).value); |
| 91 Expect.equals(28, new A.redirectFactory(x: 7, y: 2).value); |
| 92 Expect.equals(15, new A.redirectFactory(z: 1).value); |
| 93 Expect.equals(21, new A.redirectFactory(y: 7, z: 1).value); |
| 94 Expect.equals(35, new A.redirectFactory(x: 7, z: 1).value); |
| 95 Expect.equals(14, new A.redirectFactory(x: 7, y: 2, z: 1).value); |
| 96 } |
OLD | NEW |