| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2011, 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 // Dart test program for testing named parameters. | |
| 5 | |
| 6 class A { | |
| 7 static Function sfn; | |
| 8 Function fn; | |
| 9 | |
| 10 factory A.make(int a, [int b, int c=3]) { | |
| 11 return new A(a, b, c); | |
| 12 } | |
| 13 | |
| 14 A(int a, [int b, int c=3]) { } | |
| 15 A.named_ctor(int a, [int b, int c=3]) { } | |
| 16 | |
| 17 static int static_method(int a, [int b, int c=3]) { | |
| 18 return (a + b) * c; | |
| 19 } | |
| 20 | |
| 21 static int _private_static_method(int a, [int b, int c=3]) { | |
| 22 return (a + b) * c; | |
| 23 } | |
| 24 | |
| 25 int instance_method(int x, [int y, int z=3]) { | |
| 26 return (x + y) * z; | |
| 27 } | |
| 28 | |
| 29 int _private_instance_method(int x, [int y, int z=3]) { | |
| 30 return (x + y) * z; | |
| 31 } | |
| 32 } | |
| 33 | |
| 34 int global_function(int x, int y, [int z]) { | |
| 35 return (x + y) * z; | |
| 36 } | |
| 37 | |
| 38 Function gfn; | |
| 39 | |
| 40 class C { | |
| 41 int i; | |
| 42 Function fn; | |
| 43 | |
| 44 C(this.i) {} | |
| 45 } | |
| 46 | |
| 47 testCtors() { | |
| 48 A a = new A(1, 2); | |
| 49 A a0 = new A(1, b:2); | |
| 50 A a1 = new A(1, c:3, b:2); | |
| 51 | |
| 52 A a2 = new A.make(1, 2); | |
| 53 A a3 = new A.make(1, 2, 3); | |
| 54 A a4 = new A.make(1, 2, c:3); | |
| 55 A a5 = new A.make(1, b:2, c:3); | |
| 56 } | |
| 57 | |
| 58 testCalls() { | |
| 59 A a = new A(1, 2); | |
| 60 | |
| 61 // global calls | |
| 62 Expect.equals(9, global_function(1, 2, 3)); | |
| 63 Expect.equals(9, global_function(1, 2, z:3)); | |
| 64 | |
| 65 // static calls | |
| 66 Expect.equals(9, A.static_method(1, 2)); | |
| 67 Expect.equals(9, A.static_method(1, 2, 3)); | |
| 68 Expect.equals(9, A.static_method(1, 2, c:3)); | |
| 69 Expect.equals(9, A.static_method(1, c:3, b:2)); | |
| 70 | |
| 71 // private static calls | |
| 72 Expect.equals(9, A._private_static_method(1, 2)); | |
| 73 Expect.equals(9, A._private_static_method(1, 2, 3)); | |
| 74 Expect.equals(9, A._private_static_method(1, 2, c:3)); | |
| 75 Expect.equals(9, A._private_static_method(1, c:3, b:2)); | |
| 76 | |
| 77 // instance calls | |
| 78 Expect.equals(9, a.instance_method(1, 2)); | |
| 79 Expect.equals(9, a.instance_method(1, 2, 3)); | |
| 80 Expect.equals(9, a.instance_method(1, z:3, y:2)); | |
| 81 | |
| 82 // private instance calls | |
| 83 Expect.equals(9, a._private_instance_method(1, 2)); | |
| 84 Expect.equals(9, a._private_instance_method(1, 2, 3)); | |
| 85 Expect.equals(9, a._private_instance_method(1, z:3, y:2)); | |
| 86 } | |
| 87 | |
| 88 testCallsThroughVars() { | |
| 89 A a = new A(1, 2); | |
| 90 | |
| 91 // bound global calls | |
| 92 Function fn0 = global_function; | |
| 93 Expect.equals(9, fn0(1, 2, 3)); | |
| 94 Expect.equals(9, fn0(1, 2, z:3)); | |
| 95 | |
| 96 // bound static calls | |
| 97 Function fn1 = A.static_method; | |
| 98 Expect.equals(9, fn1(1, 2)); | |
| 99 Expect.equals(9, fn1(1, 2, 3)); | |
| 100 Expect.equals(9, fn1(1, 2, c:3)); | |
| 101 Expect.equals(9, fn1(1, c:3, b:2)); | |
| 102 | |
| 103 // bound instance calls | |
| 104 Function fn2 = a.instance_method; | |
| 105 Expect.equals(9, fn2(1, 2)); | |
| 106 Expect.equals(9, fn2(1, 2, 3)); | |
| 107 Expect.equals(9, fn2(1, z:3, y:2)); | |
| 108 | |
| 109 // call to bound method through instance field | |
| 110 a.fn = global_function; | |
| 111 Expect.equals(9, a.fn(1, 2, 3)); | |
| 112 Expect.equals(9, a.fn(1, 2, z:3)); | |
| 113 | |
| 114 // call to bound method through static field | |
| 115 A.sfn = global_function; | |
| 116 Expect.equals(9, A.sfn(1, 2, 3)); | |
| 117 Expect.equals(9, A.sfn(1, 2, z:3)); | |
| 118 | |
| 119 // call to bound method through global field | |
| 120 gfn = global_function; | |
| 121 Expect.equals(9, gfn(1, 2, 3)); | |
| 122 Expect.equals(9, gfn(1, 2, z:3)); | |
| 123 } | |
| 124 | |
| 125 // --------------------------------------------------------------------------- | |
| 126 testClosures() { | |
| 127 // call to hoisted closure | |
| 128 var cfn = int foo(int x, int y, [int z]) { return (x + y) * z; }; | |
| 129 Expect.equals(9, cfn(1, 2, 3)); | |
| 130 Expect.equals(9, cfn(1, 2, z:3)); | |
| 131 | |
| 132 // call to local function | |
| 133 int lfn(int x, int y, [int z]) { return (x + y) * z; } | |
| 134 Expect.equals(9, lfn(1, 2, 3)); | |
| 135 Expect.equals(9, lfn(1, 2, z:3)); | |
| 136 | |
| 137 // fun case with local and this binding | |
| 138 C c = new C(20); | |
| 139 Function refc = () => c.i; | |
| 140 c.fn = refc; | |
| 141 Expect.equals(20, c.fn()); | |
| 142 } | |
| 143 | |
| 144 testMultipleClosureScopes() { | |
| 145 for (int x = 0; x < 1; ++x) { | |
| 146 int i = 6; | |
| 147 for (int y = 0; y < 1; ++y) { | |
| 148 int j = 9; | |
| 149 | |
| 150 var a = new List<Function>(1); | |
| 151 a[0] = () => i * j; | |
| 152 Expect.equals(54, a[0]()); | |
| 153 } | |
| 154 } | |
| 155 } | |
| 156 | |
| 157 // --------------------------------------------------------------------------- | |
| 158 class Sup { | |
| 159 Sup() { } | |
| 160 int foo() { return 54; } | |
| 161 } | |
| 162 | |
| 163 class Sub extends Sup { | |
| 164 Sub(): super() { } | |
| 165 int foo() { return 42; } | |
| 166 Function getSuperFoo() { return super.foo; } | |
| 167 } | |
| 168 | |
| 169 testSuperMethodGetter() { | |
| 170 var sup = new Sup(); | |
| 171 var sub = new Sub(); | |
| 172 Expect.equals(sup.foo(), sub.getSuperFoo()()); | |
| 173 } | |
| 174 | |
| 175 // --------------------------------------------------------------------------- | |
| 176 class HasGetter { | |
| 177 HasGetter() { } | |
| 178 var field; | |
| 179 get getter() { return field; } | |
| 180 method() { return field; } | |
| 181 } | |
| 182 | |
| 183 void testGetter() { | |
| 184 HasGetter a = new HasGetter(); | |
| 185 a.field = () => 42; | |
| 186 Expect.equals(42, a.getter()); | |
| 187 Expect.equals(42, (a.getter)()); | |
| 188 | |
| 189 a.field = () => 87; | |
| 190 Expect.equals(87, a.getter()); | |
| 191 Expect.equals(87, (a.getter)()); | |
| 192 } | |
| 193 | |
| 194 // --------------------------------------------------------------------------- | |
| 195 expectNSME(Function fn) { | |
| 196 try { | |
| 197 fn(); | |
| 198 Expect.fail("Expected NoSuchMethodException"); | |
| 199 } catch (NoSuchMethodException e) { | |
| 200 } | |
| 201 } | |
| 202 | |
| 203 int takesNoArgs() => 42; | |
| 204 int takesOneArg(int x) => 42; | |
| 205 int takesOneNamedArg([int x]) => 42; | |
| 206 | |
| 207 testStaticNSM() { | |
| 208 expectNSME(() => takesNoArgs("I'm ignoring static errors!")); | |
| 209 expectNSME(() => takesOneArg()); | |
| 210 expectNSME(() => takesOneNamedArg(y:54)); | |
| 211 } | |
| 212 | |
| 213 // --------------------------------------------------------------------------- | |
| 214 class Tintin { | |
| 215 Tintin() { } | |
| 216 | |
| 217 int possibleNameConflict([int $n]) { | |
| 218 return $n + 1; | |
| 219 } | |
| 220 } | |
| 221 | |
| 222 testPossibleNameConflict() { | |
| 223 var tintin = new Tintin(); | |
| 224 Expect.equals(43, tintin.possibleNameConflict($n:42)); | |
| 225 } | |
| 226 | |
| 227 // --------------------------------------------------------------------------- | |
| 228 main() { | |
| 229 testCtors(); | |
| 230 testCalls(); | |
| 231 testCallsThroughVars(); | |
| 232 testMultipleClosureScopes(); | |
| 233 testGetter(); | |
| 234 testStaticNSM(); | |
| 235 testPossibleNameConflict(); | |
| 236 } | |
| OLD | NEW |