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 closures. |
| 6 |
| 7 import "package:expect/expect.dart"; |
| 8 import "tearoff_basic_lib.dart" as P; |
| 9 import "tearoff_basic_lib.dart" deferred as D; |
| 10 |
| 11 class C { |
| 12 var v = 99; |
| 13 final fv = 444; |
| 14 |
| 15 operator + (a) { return v + a; } |
| 16 get sugus => "sugus"; |
| 17 set frosch(a) { v = "ribbit $a"; } |
| 18 foo() => "kuh"; |
| 19 |
| 20 static var st; |
| 21 static final stfin = 1000; |
| 22 static stfoo([p1 = 100]) => p1 * 10; |
| 23 static get stg => "stg"; |
| 24 static set sts(x) { st = x; } |
| 25 } |
| 26 |
| 27 |
| 28 testStatic() { |
| 29 // Closurize static variable. |
| 30 var a = C#st=; |
| 31 a(100); |
| 32 Expect.equals(100, C.st); |
| 33 var b = C#st; |
| 34 Expect.equals(100, b()); |
| 35 |
| 36 // Closurize static final variable. |
| 37 a = C#stfin; |
| 38 Expect.equals(1000, a()); |
| 39 Expect.throws(() => C#stfin= ); // Final variable has no setter. |
| 40 |
| 41 // Closurize static method. |
| 42 a = C#stfoo; |
| 43 Expect.equals(1000, a()); |
| 44 Expect.equals(90, a(9)); |
| 45 |
| 46 // Closurize static getter. |
| 47 a = C#stg; |
| 48 Expect.equals("stg", a()); |
| 49 |
| 50 // Closurize static setter. |
| 51 Expect.throws(() => C#sts); // No setter/method named sts exists. |
| 52 a = C#sts=; |
| 53 a("pflug"); |
| 54 Expect.equals("pflug", C.st); |
| 55 |
| 56 // Can't closurize instance method via class literal. |
| 57 Expect.throws(() => C#foo); |
| 58 } |
| 59 |
| 60 testInstance() { |
| 61 var o = new C(); |
| 62 var p = new C(); |
| 63 var a, b; |
| 64 |
| 65 // Closurize instance variable. |
| 66 a = o#v; |
| 67 Expect.equals(99, a()); |
| 68 b = p#v=; |
| 69 b(999); |
| 70 Expect.equals(999, p.v); |
| 71 Expect.equals(99, a()); |
| 72 |
| 73 // Closurize final instance variable. |
| 74 Expect.throws(() => o#fv=); // Final variable has not setter. |
| 75 a = o#fv; |
| 76 Expect.equals(444, a()); |
| 77 |
| 78 // Closurize instance method. |
| 79 a = o#foo; |
| 80 Expect.equals("kuh", a()); |
| 81 |
| 82 // Closurize operator. |
| 83 a = o#+; |
| 84 Expect.equals(100, o + 1); |
| 85 Expect.equals(100, a(1)); |
| 86 |
| 87 // Closurize instance getter. |
| 88 a = o#sugus; |
| 89 Expect.equals("sugus", a()); |
| 90 Expect.throws(() => o#sugus=); |
| 91 |
| 92 // Closurize instance setter. |
| 93 a = o#frosch=; |
| 94 a("!"); |
| 95 Expect.equals("ribbit !", o.v); |
| 96 Expect.throws(() => o#frosch); |
| 97 } |
| 98 |
| 99 testPrefix() { |
| 100 // Closurize top-level variable. |
| 101 var a = P#cvar; |
| 102 Expect.equals(6, a()); |
| 103 var b = P#cvar=; |
| 104 b(7); |
| 105 Expect.equals(7, a()); |
| 106 |
| 107 // Closurize final top-level variable. |
| 108 a = P#cfinvar; |
| 109 Expect.equals("set in stone", a()); |
| 110 Expect.throws(() => P#cfinvar=); |
| 111 |
| 112 // Closurize top-level function. |
| 113 a = P#cfunc; |
| 114 Expect.equals("cfunc", a()); |
| 115 |
| 116 // Closurize top-level getter. |
| 117 a = P#cget; |
| 118 Expect.equals("cget", a()); |
| 119 |
| 120 // Closurize top-level getter. |
| 121 a = P#cset=; |
| 122 a(99); |
| 123 Expect.equals(99, P.cvar); |
| 124 |
| 125 Expect.throws(() => P#ZZ); // Cannot closurize class. |
| 126 } |
| 127 |
| 128 testDeferred() { |
| 129 Expect.throws(() => D#cfunc); |
| 130 D.loadLibrary().then((_) { |
| 131 var a = D#cfunc; |
| 132 Expect.equals("cfunc", a()); |
| 133 }); |
| 134 } |
| 135 |
| 136 main() { |
| 137 testStatic(); |
| 138 testInstance(); |
| 139 testPrefix(); |
| 140 testDeferred(); |
| 141 } |
OLD | NEW |