OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2015, 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 // Basic test for tear-off constructor closures. |
| 6 |
| 7 import "package:expect/expect.dart"; |
| 8 |
| 9 class A { |
| 10 // Implicit constructor A(); |
| 11 var f1 = "A.f1"; |
| 12 } |
| 13 |
| 14 class P { |
| 15 var x, y; |
| 16 P(this.x, this.y); |
| 17 factory P.origin() { return new P(0,0); } |
| 18 factory P.ursprung() = P.origin; |
| 19 P.onXAxis(x) : this(x, 0); |
| 20 } |
| 21 |
| 22 class C<T> { |
| 23 T f1; |
| 24 C(T p) : f1 = p; |
| 25 C.n([T p]) : f1 = p; |
| 26 listMaker() { return new List<T>#; } // Closurize type parameter. |
| 27 } |
| 28 |
| 29 |
| 30 |
| 31 testMalformed() { |
| 32 Expect.throws(() => new NoSuchClass#); |
| 33 Expect.throws(() => new A#noSuchContstructor); |
| 34 } |
| 35 |
| 36 testA() { |
| 37 var cc = new A#; // Closurize implicit constructor. |
| 38 var o = cc(); |
| 39 Expect.equals("A.f1", o.f1); |
| 40 Expect.equals("A.f1", (new A#)().f1); |
| 41 Expect.throws(() => new A#foo); |
| 42 } |
| 43 |
| 44 testP() { |
| 45 var cc = new P#origin; |
| 46 var o = cc(); |
| 47 Expect.equals(0, o.x); |
| 48 cc = new P#ursprung; |
| 49 o = cc(); |
| 50 Expect.equals(0, o.x); |
| 51 cc = new P#onXAxis; |
| 52 o = cc(5); |
| 53 Expect.equals(0, o.y); |
| 54 Expect.equals(5, o.x); |
| 55 Expect.throws(() => cc(1, 1)); // Too many arguments. |
| 56 } |
| 57 |
| 58 testC() { |
| 59 var cc = new C<int>#; |
| 60 var o = cc(5); |
| 61 Expect.equals("int", "${o.f1.runtimeType}"); |
| 62 Expect.throws(() => cc()); // Missing constructor parameter. |
| 63 |
| 64 cc = new C<String>#n; |
| 65 o = cc("foo"); |
| 66 Expect.equals("String", "${o.f1.runtimeType}"); |
| 67 o = cc(); |
| 68 Expect.equals(null, o.f1); |
| 69 |
| 70 cc = o.listMaker(); |
| 71 Expect.isTrue(cc is Function); |
| 72 var l = cc(); |
| 73 Expect.equals("List<String>", "${l.runtimeType}"); |
| 74 } |
| 75 |
| 76 main() { |
| 77 testA(); |
| 78 testC(); |
| 79 testP(); |
| 80 testMalformed(); |
| 81 } |
OLD | NEW |