OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2012, 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 // Test indirect use of InvocationMirror.invokeOn and Function.apply in | |
6 // noSuchMethod. | |
7 | |
bakster
2012/10/24 13:43:04
My comments from tests/language/invocation_mirror
Johnni Winther
2012/10/25 13:34:56
Done.
| |
8 class PlusMinus { | |
9 noSuchMethod(var invocation) { | |
10 var invok = invocation.invokeOn; | |
11 var funcApp = Function.apply; | |
12 if (invocation.memberName == 'mul') { | |
13 return invok(mulObject); | |
14 } else if (invocation.memberName == 'div') { | |
15 return invok(divObject); | |
16 } | |
17 return funcApp(plus, | |
18 invocation.positionalArguments, | |
19 invocation.namedArguments); | |
20 } | |
21 | |
22 int plus(int a, int b) => a+b; | |
23 | |
24 get mulObject => new Mul(); | |
25 | |
26 get divObject => new Div(); | |
27 } | |
28 | |
29 class Mul { | |
30 int mul(int a, int b, [int c]) { | |
31 if (?c) { | |
32 return a*b*c; | |
33 } else { | |
34 return a*b; | |
35 } | |
36 } | |
37 } | |
38 | |
39 class Div { | |
40 int div({int num, int denom}) { | |
41 return num/denom; | |
42 } | |
43 } | |
44 | |
45 void main() { | |
46 var pm = new PlusMinus(); | |
47 Expect.equals(7, pm.plus(2, 5)); | |
48 Expect.equals(5, pm.minus(2, 3)); // Calls plus. | |
49 Expect.equals(6, pm.mul(2, 3)); | |
50 Expect.equals(24, pm.mul(2, 3, 4)); | |
51 Expect.equals(5, pm.div(num:10, denom:2)); | |
52 Expect.equals(5, pm.div(denom:2, num:10)); | |
53 } | |
OLD | NEW |