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 // Extracted closures must be equal. |
| 60 Expect.isTrue(C#st == C#st); |
| 61 Expect.isTrue(C#st= == C#st=); |
| 62 Expect.isTrue(C#stfin == C#stfin); |
| 63 Expect.isTrue(C#stfoo == C#stfoo); |
| 64 Expect.isTrue(C#stg == C#stg); |
| 65 Expect.isTrue(C#sts= == C#sts=); |
| 66 } |
| 67 |
| 68 testInstance() { |
| 69 var o = new C(); |
| 70 var p = new C(); |
| 71 var a, b; |
| 72 |
| 73 // Closurize instance variable. |
| 74 a = o#v; |
| 75 Expect.equals(99, a()); |
| 76 b = p#v=; |
| 77 b(999); |
| 78 Expect.equals(999, p.v); |
| 79 Expect.equals(99, a()); |
| 80 |
| 81 // Closurize final instance variable. |
| 82 Expect.throws(() => o#fv=); // Final variable has not setter. |
| 83 a = o#fv; |
| 84 Expect.equals(444, a()); |
| 85 |
| 86 // Closurize instance method. |
| 87 a = o#foo; |
| 88 Expect.equals("kuh", a()); |
| 89 |
| 90 // Closurize operator. |
| 91 a = o#+; |
| 92 Expect.equals(100, o + 1); |
| 93 Expect.equals(100, a(1)); |
| 94 |
| 95 // Closurize instance getter. |
| 96 a = o#sugus; |
| 97 Expect.equals("sugus", a()); |
| 98 Expect.throws(() => o#sugus=); |
| 99 |
| 100 // Closurize instance setter. |
| 101 a = o#frosch=; |
| 102 a("!"); |
| 103 Expect.equals("ribbit !", o.v); |
| 104 Expect.throws(() => o#frosch); |
| 105 |
| 106 // Extracted closures must be equal. |
| 107 Expect.isTrue(o#v == o#v); |
| 108 Expect.isTrue(o#v= == o#v=); |
| 109 Expect.isTrue(o#fv == o#fv); |
| 110 Expect.isTrue(o#foo == o#foo); |
| 111 Expect.isTrue(o#+ == o#+); |
| 112 Expect.isTrue(o#sugus == o#sugus); |
| 113 Expect.isTrue(o#frosch= == o#frosch=); |
| 114 } |
| 115 |
| 116 testPrefix() { |
| 117 // Closurize top-level variable. |
| 118 var a = P#cvar; |
| 119 Expect.equals(6, a()); |
| 120 var b = P#cvar=; |
| 121 b(7); |
| 122 Expect.equals(7, a()); |
| 123 |
| 124 // Closurize final top-level variable. |
| 125 a = P#cfinvar; |
| 126 Expect.equals("set in stone", a()); |
| 127 Expect.throws(() => P#cfinvar=); |
| 128 |
| 129 // Closurize top-level function. |
| 130 a = P#cfunc; |
| 131 Expect.equals("cfunc", a()); |
| 132 |
| 133 // Closurize top-level getter. |
| 134 a = P#cget; |
| 135 Expect.equals("cget", a()); |
| 136 |
| 137 // Closurize top-level getter. |
| 138 a = P#cset=; |
| 139 a(99); |
| 140 Expect.equals(99, P.cvar); |
| 141 |
| 142 Expect.throws(() => P#ZZ); // Cannot closurize class. |
| 143 |
| 144 // Extracted closures must be equal. |
| 145 Expect.isTrue(P#cvar == P#cvar); |
| 146 Expect.isTrue(P#cvar= == P#cvar=); |
| 147 Expect.isTrue(P#cfinvar == P#cfinvar); |
| 148 Expect.isTrue(P#cfunc == P#cfunc); |
| 149 Expect.isTrue(P#cget == P#cget); |
| 150 Expect.isTrue(P#cset= == P#cset=); |
| 151 } |
| 152 |
| 153 testDeferred() { |
| 154 Expect.throws(() => D#cfunc); |
| 155 D.loadLibrary().then((_) { |
| 156 var a = D#cfunc; |
| 157 Expect.equals("cfunc", a()); |
| 158 }); |
| 159 } |
| 160 |
| 161 main() { |
| 162 testStatic(); |
| 163 testInstance(); |
| 164 testPrefix(); |
| 165 testDeferred(); |
| 166 } |
OLD | NEW |