OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2013, 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 library test.invoke_named_test; |
| 6 |
| 7 import 'dart:mirrors'; |
| 8 |
| 9 import 'dart:async' show Future; |
| 10 |
| 11 import 'package:expect/expect.dart'; |
| 12 import 'invoke_test.dart'; |
| 13 |
| 14 // TODO(ahe): Remove this variable (http://dartbug.com/12863). |
| 15 bool isDart2js = false; |
| 16 |
| 17 class C { |
| 18 a(a, {b:'B', c}) => "$a-$b-$c"; |
| 19 b({a:'A', b, c}) => "$a-$b-$c"; |
| 20 c(a, [b, c='C']) => "$a-$b-$c"; |
| 21 d([a, b='B', c='C']) => "$a-$b-$c"; |
| 22 e(a, b, c) => "$a-$b-$c"; |
| 23 } |
| 24 |
| 25 class D { |
| 26 static a(a, {b:'B', c}) => "$a-$b-$c"; |
| 27 static b({a:'A', b, c}) => "$a-$b-$c"; |
| 28 static c(a, [b, c='C']) => "$a-$b-$c"; |
| 29 static d([a, b='B', c='C']) => "$a-$b-$c"; |
| 30 static e(a, b, c) => "$a-$b-$c"; |
| 31 } |
| 32 |
| 33 class E { |
| 34 var field; |
| 35 E(a, {b:'B', c}) : this.field = "$a-$b-$c"; |
| 36 E.b({a:'A', b, c}) : this.field = "$a-$b-$c"; |
| 37 E.c(a, [b, c='C']) : this.field = "$a-$b-$c"; |
| 38 E.d([a, b='B', c='C']) : this.field = "$a-$b-$c"; |
| 39 E.e(a, b, c) : this.field = "$a-$b-$c"; |
| 40 } |
| 41 |
| 42 a(a, {b:'B', c}) => "$a-$b-$c"; |
| 43 b({a:'A', b, c}) => "$a-$b-$c"; |
| 44 c(a, [b, c='C']) => "$a-$b-$c"; |
| 45 d([a, b='B', c='C']) => "$a-$b-$c"; |
| 46 e(a, b, c) => "$a-$b-$c"; |
| 47 |
| 48 testSyncInvoke(ObjectMirror om) { |
| 49 InstanceMirror result; |
| 50 |
| 51 result = om.invoke(const Symbol('a'), ['X']); |
| 52 Expect.equals('X-B-null', result.reflectee); |
| 53 result = om.invoke(const Symbol('a'), ['X'], {const Symbol('b') : 'Y'}); |
| 54 Expect.equals('X-Y-null', result.reflectee); |
| 55 result = om.invoke(const Symbol('a'), ['X'], {const Symbol('c') : 'Z', const S
ymbol('b') : 'Y'}); |
| 56 Expect.equals('X-Y-Z', result.reflectee); |
| 57 Expect.throws(() => om.invoke(const Symbol('a'), []), |
| 58 isNoSuchMethodError, |
| 59 'Insufficient positional arguments'); |
| 60 Expect.throws(() => om.invoke(const Symbol('a'), ['X', 'Y']), |
| 61 isNoSuchMethodError, |
| 62 'Extra positional arguments'); |
| 63 Expect.throws(() => om.invoke(const Symbol('a'), ['X'], {const Symbol('undef')
: 'Y'}), |
| 64 isNoSuchMethodError, |
| 65 'Unmatched named argument'); |
| 66 |
| 67 result = om.invoke(const Symbol('b'), []); |
| 68 Expect.equals('A-null-null', result.reflectee); |
| 69 result = om.invoke(const Symbol('b'), [], {const Symbol('a') : 'X'}); |
| 70 Expect.equals('X-null-null', result.reflectee); |
| 71 result = om.invoke(const Symbol('b'), [], {const Symbol('b') :'Y', const Symbo
l('c') :'Z', const Symbol('a') :'X'}); |
| 72 Expect.equals('X-Y-Z', result.reflectee); |
| 73 Expect.throws(() => om.invoke(const Symbol('b'), ['X']), |
| 74 isNoSuchMethodError, |
| 75 'Extra positional arguments'); |
| 76 Expect.throws(() => om.invoke(const Symbol('b'), ['X'], {const Symbol('undef')
: 'Y'}), |
| 77 isNoSuchMethodError, |
| 78 'Unmatched named argument'); |
| 79 |
| 80 result = om.invoke(const Symbol('c'), ['X']); |
| 81 Expect.equals('X-null-C', result.reflectee); |
| 82 result = om.invoke(const Symbol('c'), ['X', 'Y']); |
| 83 Expect.equals('X-Y-C', result.reflectee); |
| 84 result = om.invoke(const Symbol('c'), ['X', 'Y', 'Z']); |
| 85 Expect.equals('X-Y-Z', result.reflectee); |
| 86 Expect.throws(() => om.invoke(const Symbol('c'), []), |
| 87 isNoSuchMethodError, |
| 88 'Insufficient positional arguments'); |
| 89 Expect.throws(() => om.invoke(const Symbol('c'), ['X', 'Y', 'Z', 'W']), |
| 90 isNoSuchMethodError, |
| 91 'Extra positional arguments'); |
| 92 Expect.throws(() => om.invoke(const Symbol('c'), ['X'], {const Symbol('undef')
: 'Y'}), |
| 93 isNoSuchMethodError, |
| 94 'Unmatched named argument'); |
| 95 |
| 96 result = om.invoke(const Symbol('d'), []); |
| 97 Expect.equals('null-B-C', result.reflectee); |
| 98 result = om.invoke(const Symbol('d'), ['X']); |
| 99 Expect.equals('X-B-C', result.reflectee); |
| 100 result = om.invoke(const Symbol('d'), ['X', 'Y']); |
| 101 Expect.equals('X-Y-C', result.reflectee); |
| 102 result = om.invoke(const Symbol('d'), ['X', 'Y', 'Z']); |
| 103 Expect.equals('X-Y-Z', result.reflectee); |
| 104 Expect.throws(() => om.invoke(const Symbol('d'), ['X', 'Y', 'Z', 'W']), |
| 105 isNoSuchMethodError, |
| 106 'Extra positional arguments'); |
| 107 Expect.throws(() => om.invoke(const Symbol('d'), ['X'], {const Symbol('undef')
: 'Y'}), |
| 108 isNoSuchMethodError, |
| 109 'Unmatched named argument'); |
| 110 |
| 111 result = om.invoke(const Symbol('e'), ['X', 'Y', 'Z']); |
| 112 Expect.equals('X-Y-Z', result.reflectee); |
| 113 Expect.throws(() => om.invoke(const Symbol('e'), ['X']), |
| 114 isNoSuchMethodError, |
| 115 'Insufficient positional arguments'); |
| 116 Expect.throws(() => om.invoke(const Symbol('e'), ['X', 'Y', 'Z', 'W']), |
| 117 isNoSuchMethodError, |
| 118 'Extra positional arguments'); |
| 119 Expect.throws(() => om.invoke(const Symbol('e'), ['X'], {const Symbol('undef')
: 'Y'}), |
| 120 isNoSuchMethodError, |
| 121 'Unmatched named argument'); |
| 122 } |
| 123 |
| 124 testSyncNewInstance() { |
| 125 ClassMirror cm = reflectClass(E); |
| 126 InstanceMirror result; |
| 127 |
| 128 result = cm.newInstance(const Symbol(''), ['X']); |
| 129 Expect.equals('X-B-null', result.reflectee.field); |
| 130 result = cm.newInstance(const Symbol(''), ['X'], {const Symbol('b') : 'Y'}); |
| 131 Expect.equals('X-Y-null', result.reflectee.field); |
| 132 result = cm.newInstance(const Symbol(''), ['X'], {const Symbol('c') : 'Z', con
st Symbol('b') : 'Y'}); |
| 133 Expect.equals('X-Y-Z', result.reflectee.field); |
| 134 Expect.throws(() => cm.newInstance(const Symbol(''), []), |
| 135 isNoSuchMethodError, |
| 136 'Insufficient positional arguments'); |
| 137 Expect.throws(() => cm.newInstance(const Symbol(''), ['X', 'Y']), |
| 138 isNoSuchMethodError, |
| 139 'Extra positional arguments'); |
| 140 Expect.throws(() => cm.newInstance(const Symbol(''), ['X'], {const Symbol('und
ef') : 'Y'}), |
| 141 isNoSuchMethodError, |
| 142 'Unmatched named argument'); |
| 143 |
| 144 result = cm.newInstance(const Symbol('b'), []); |
| 145 Expect.equals('A-null-null', result.reflectee.field); |
| 146 result = cm.newInstance(const Symbol('b'), [], {const Symbol('a') : 'X'}); |
| 147 Expect.equals('X-null-null', result.reflectee.field); |
| 148 result = cm.newInstance(const Symbol('b'), [], {const Symbol('b') :'Y', const
Symbol('c') :'Z', const Symbol('a') :'X'}); |
| 149 Expect.equals('X-Y-Z', result.reflectee.field); |
| 150 Expect.throws(() => cm.newInstance(const Symbol('b'), ['X']), |
| 151 isNoSuchMethodError, |
| 152 'Extra positional arguments'); |
| 153 Expect.throws(() => cm.newInstance(const Symbol('b'), ['X'], {const Symbol('un
def'): 'Y'}), |
| 154 isNoSuchMethodError, |
| 155 'Unmatched named argument'); |
| 156 |
| 157 result = cm.newInstance(const Symbol('c'), ['X']); |
| 158 Expect.equals('X-null-C', result.reflectee.field); |
| 159 result = cm.newInstance(const Symbol('c'), ['X', 'Y']); |
| 160 Expect.equals('X-Y-C', result.reflectee.field); |
| 161 result = cm.newInstance(const Symbol('c'), ['X', 'Y', 'Z']); |
| 162 Expect.equals('X-Y-Z', result.reflectee.field); |
| 163 Expect.throws(() => cm.newInstance(const Symbol('c'), []), |
| 164 isNoSuchMethodError, |
| 165 'Insufficient positional arguments'); |
| 166 Expect.throws(() => cm.newInstance(const Symbol('c'), ['X', 'Y', 'Z', 'W']), |
| 167 isNoSuchMethodError, |
| 168 'Extra positional arguments'); |
| 169 Expect.throws(() => cm.newInstance(const Symbol('c'), ['X'], {const Symbol('un
def'): 'Y'}), |
| 170 isNoSuchMethodError, |
| 171 'Unmatched named argument'); |
| 172 |
| 173 result = cm.newInstance(const Symbol('d'), []); |
| 174 Expect.equals('null-B-C', result.reflectee.field); |
| 175 result = cm.newInstance(const Symbol('d'), ['X']); |
| 176 Expect.equals('X-B-C', result.reflectee.field); |
| 177 result = cm.newInstance(const Symbol('d'), ['X', 'Y']); |
| 178 Expect.equals('X-Y-C', result.reflectee.field); |
| 179 result = cm.newInstance(const Symbol('d'), ['X', 'Y', 'Z']); |
| 180 Expect.equals('X-Y-Z', result.reflectee.field); |
| 181 Expect.throws(() => cm.newInstance(const Symbol('d'), ['X', 'Y', 'Z', 'W']), |
| 182 isNoSuchMethodError, |
| 183 'Extra positional arguments'); |
| 184 Expect.throws(() => cm.newInstance(const Symbol('d'), ['X'], {const Symbol('un
def'): 'Y'}), |
| 185 isNoSuchMethodError, |
| 186 'Unmatched named argument'); |
| 187 |
| 188 result = cm.newInstance(const Symbol('e'), ['X', 'Y', 'Z']); |
| 189 Expect.equals('X-Y-Z', result.reflectee.field); |
| 190 Expect.throws(() => cm.newInstance(const Symbol('e'), ['X']), |
| 191 isNoSuchMethodError, |
| 192 'Insufficient positional arguments'); |
| 193 Expect.throws(() => cm.newInstance(const Symbol('e'), ['X', 'Y', 'Z', 'W']), |
| 194 isNoSuchMethodError, |
| 195 'Extra positional arguments'); |
| 196 Expect.throws(() => cm.newInstance(const Symbol('e'), ['X'], {const Symbol('un
def'): 'Y'}), |
| 197 isNoSuchMethodError, |
| 198 'Unmatched named argument'); |
| 199 } |
| 200 |
| 201 testSyncApply() { |
| 202 ClosureMirror cm; |
| 203 InstanceMirror result; |
| 204 |
| 205 cm = reflect(a); |
| 206 result = cm.apply(['X']); |
| 207 Expect.equals('X-B-null', result.reflectee); |
| 208 result = cm.apply(['X'], {const Symbol('b') : 'Y'}); |
| 209 Expect.equals('X-Y-null', result.reflectee); |
| 210 result = cm.apply(['X'], {const Symbol('c') : 'Z', const Symbol('b') : 'Y'}); |
| 211 Expect.equals('X-Y-Z', result.reflectee); |
| 212 Expect.throws(() => cm.apply([]), |
| 213 isNoSuchMethodError, |
| 214 'Insufficient positional arguments'); |
| 215 Expect.throws(() => cm.apply(['X', 'Y']), |
| 216 isNoSuchMethodError, |
| 217 'Extra positional arguments'); |
| 218 Expect.throws(() => cm.apply(['X'], {const Symbol('undef') : 'Y'}), |
| 219 isNoSuchMethodError, |
| 220 'Unmatched named argument'); |
| 221 |
| 222 cm = reflect(b); |
| 223 result = cm.apply([]); |
| 224 Expect.equals('A-null-null', result.reflectee); |
| 225 result = cm.apply([], {const Symbol('a') : 'X'}); |
| 226 Expect.equals('X-null-null', result.reflectee); |
| 227 result = cm.apply([], {const Symbol('b') :'Y', const Symbol('c') :'Z', const S
ymbol('a') :'X'}); |
| 228 Expect.equals('X-Y-Z', result.reflectee); |
| 229 Expect.throws(() => cm.apply(['X']), |
| 230 isNoSuchMethodError, |
| 231 'Extra positional arguments'); |
| 232 Expect.throws(() => cm.apply(['X'], {const Symbol('undef'): 'Y'}), |
| 233 isNoSuchMethodError, |
| 234 'Unmatched named argument'); |
| 235 |
| 236 cm = reflect(c); |
| 237 result = cm.apply(['X']); |
| 238 Expect.equals('X-null-C', result.reflectee); |
| 239 result = cm.apply(['X', 'Y']); |
| 240 Expect.equals('X-Y-C', result.reflectee); |
| 241 result = cm.apply(['X', 'Y', 'Z']); |
| 242 Expect.equals('X-Y-Z', result.reflectee); |
| 243 Expect.throws(() => cm.apply([]), |
| 244 isNoSuchMethodError, |
| 245 'Insufficient positional arguments'); |
| 246 Expect.throws(() => cm.apply(['X', 'Y', 'Z', 'W']), |
| 247 isNoSuchMethodError, |
| 248 'Extra positional arguments'); |
| 249 Expect.throws(() => cm.apply(['X'], {const Symbol('undef'): 'Y'}), |
| 250 isNoSuchMethodError, |
| 251 'Unmatched named argument'); |
| 252 |
| 253 cm = reflect(d); |
| 254 result = cm.apply([]); |
| 255 Expect.equals('null-B-C', result.reflectee); |
| 256 result = cm.apply(['X']); |
| 257 Expect.equals('X-B-C', result.reflectee); |
| 258 result = cm.apply(['X', 'Y']); |
| 259 Expect.equals('X-Y-C', result.reflectee); |
| 260 result = cm.apply(['X', 'Y', 'Z']); |
| 261 Expect.equals('X-Y-Z', result.reflectee); |
| 262 Expect.throws(() => cm.apply(['X', 'Y', 'Z', 'W']), |
| 263 isNoSuchMethodError, |
| 264 'Extra positional arguments'); |
| 265 Expect.throws(() => cm.apply(['X'], {const Symbol('undef'): 'Y'}), |
| 266 isNoSuchMethodError, |
| 267 'Unmatched named argument'); |
| 268 |
| 269 cm = reflect(e); |
| 270 result = cm.apply(['X', 'Y', 'Z']); |
| 271 Expect.equals('X-Y-Z', result.reflectee); |
| 272 Expect.throws(() => cm.apply(['X']), |
| 273 isNoSuchMethodError, |
| 274 'Insufficient positional arguments'); |
| 275 Expect.throws(() => cm.apply(['X', 'Y', 'Z', 'W']), |
| 276 isNoSuchMethodError, |
| 277 'Extra positional arguments'); |
| 278 Expect.throws(() => cm.apply(['X'], {const Symbol('undef'): 'Y'}), |
| 279 isNoSuchMethodError, |
| 280 'Unmatched named argument'); |
| 281 } |
| 282 |
| 283 main() { |
| 284 isDart2js = true; /// 01: ok |
| 285 |
| 286 testSyncInvoke(reflect(new C())); // InstanceMirror |
| 287 |
| 288 if (isDart2js) return; |
| 289 |
| 290 testSyncInvoke(reflectClass(D)); // ClassMirror |
| 291 LibraryMirror lib = reflectClass(D).owner; |
| 292 testSyncInvoke(lib); // LibraryMirror |
| 293 |
| 294 testSyncNewInstance(); |
| 295 |
| 296 testSyncApply(); |
| 297 } |
OLD | NEW |