OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2014, 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_call_through_implicit_getter; |
| 6 |
| 7 import 'dart:mirrors'; |
| 8 |
| 9 import 'package:expect/expect.dart'; |
| 10 |
| 11 class FakeFunctionCall { |
| 12 call(x, y) => '1 $x $y'; |
| 13 } |
| 14 class FakeFunctionNSM { |
| 15 noSuchMethod(msg) => msg.positionalArguments.join(', '); |
| 16 } |
| 17 |
| 18 class C { |
| 19 var fakeFunctionCall = new FakeFunctionCall(); |
| 20 var fakeFunctionNSM = new FakeFunctionNSM(); |
| 21 var closure; // = (x, y) => '2 $this $x $y'; |
| 22 var closureOpt; // = (x, y, [z, w]) => '3 $this $x $y $z $w'; |
| 23 var closureNamed; // = (x, y, {z, w}) => '4 $this $x $y $z $w'; |
| 24 var notAClosure = 'Not a closure'; |
| 25 noSuchMethod(msg) => 'DNU'; |
| 26 |
| 27 C() { |
| 28 closure = (x, y) => '2 $this $x $y'; |
| 29 closureOpt = (x, y, [z, w]) => '3 $this $x $y $z $w'; |
| 30 closureNamed = (x, y, {z, w}) => '4 $this $x $y $z $w'; |
| 31 } |
| 32 |
| 33 toString() => 'C'; |
| 34 } |
| 35 |
| 36 testInstanceBase() { |
| 37 var c = new C(); |
| 38 |
| 39 Expect.equals('1 5 6', c.fakeFunctionCall(5, 6)); |
| 40 Expect.equals('7, 8', c.fakeFunctionNSM(7, 8)); |
| 41 Expect.equals('2 C 9 10', c.closure(9, 10)); |
| 42 Expect.equals('3 C 11 12 13 null', c.closureOpt(11, 12, 13)); |
| 43 Expect.equals('4 C 14 15 null 16', c.closureNamed(14, 15, w: 16)); |
| 44 Expect.equals('DNU', c.doesNotExist(17, 18)); |
| 45 Expect.throws(() => c.closure('wrong arity'), (e) => e is NoSuchMethodError); |
| 46 Expect.throws(() => c.notAClosure(), (e) => e is NoSuchMethodError); |
| 47 } |
| 48 |
| 49 testInstanceReflective() { |
| 50 InstanceMirror im = reflect(new C()); |
| 51 |
| 52 Expect.equals('1 5 6', |
| 53 im.invoke(#fakeFunctionCall, [5, 6]).reflectee); |
| 54 Expect.equals('7, 8', |
| 55 im.invoke(#fakeFunctionNSM, [7, 8]).reflectee); |
| 56 Expect.equals('2 C 9 10', |
| 57 im.invoke(#closure, [9, 10]).reflectee); |
| 58 Expect.equals('3 C 11 12 13 null', |
| 59 im.invoke(#closureOpt, [11, 12, 13]).reflectee); |
| 60 Expect.equals('4 C 14 15 null 16', |
| 61 im.invoke(#closureNamed, [14, 15], {#w: 16}).reflectee); |
| 62 Expect.equals('DNU', |
| 63 im.invoke(#doesNotExist, [17, 18]).reflectee); |
| 64 Expect.throws(() => im.invoke(#closure, ['wrong arity']), |
| 65 (e) => e is NoSuchMethodError); |
| 66 Expect.throws(() => im.invoke(#notAClosure, []), |
| 67 (e) => e is NoSuchMethodError); |
| 68 } |
| 69 |
| 70 class D { |
| 71 static var fakeFunctionCall = new FakeFunctionCall(); |
| 72 static var fakeFunctionNSM = new FakeFunctionNSM(); |
| 73 static var closure = (x, y) => '2 $x $y'; |
| 74 static var closureOpt = (x, y, [z, w]) => '3 $x $y $z $w'; |
| 75 static var closureNamed = (x, y, {z, w}) => '4 $x $y $z $w'; |
| 76 static var notAClosure = 'Not a closure'; |
| 77 } |
| 78 |
| 79 testClassBase() { |
| 80 Expect.equals('1 5 6', D.fakeFunctionCall(5, 6)); |
| 81 Expect.equals('7, 8', D.fakeFunctionNSM(7, 8)); |
| 82 Expect.equals('2 9 10', D.closure(9, 10)); |
| 83 Expect.equals('3 11 12 13 null', D.closureOpt(11, 12, 13)); |
| 84 Expect.equals('4 14 15 null 16', D.closureNamed(14, 15, w: 16)); |
| 85 Expect.throws(() => D.closure('wrong arity'), (e) => e is NoSuchMethodError); |
| 86 } |
| 87 |
| 88 testClassReflective() { |
| 89 ClassMirror cm = reflectClass(D); |
| 90 |
| 91 Expect.equals('1 5 6', |
| 92 cm.invoke(#fakeFunctionCall, [5, 6]).reflectee); |
| 93 Expect.equals('7, 8', |
| 94 cm.invoke(#fakeFunctionNSM, [7, 8]).reflectee); |
| 95 Expect.equals('2 9 10', |
| 96 cm.invoke(#closure, [9, 10]).reflectee); |
| 97 Expect.equals('3 11 12 13 null', |
| 98 cm.invoke(#closureOpt, [11, 12, 13]).reflectee); |
| 99 Expect.equals('4 14 15 null 16', |
| 100 cm.invoke(#closureNamed, [14, 15], {#w: 16}).reflectee); |
| 101 Expect.throws(() => cm.invoke(#closure, ['wrong arity']), |
| 102 (e) => e is NoSuchMethodError); |
| 103 } |
| 104 |
| 105 var fakeFunctionCall = new FakeFunctionCall(); |
| 106 var fakeFunctionNSM = new FakeFunctionNSM(); |
| 107 var closure = (x, y) => '2 $x $y'; |
| 108 var closureOpt = (x, y, [z, w]) => '3 $x $y $z $w'; |
| 109 var closureNamed = (x, y, {z, w}) => '4 $x $y $z $w'; |
| 110 var notAClosure = 'Not a closure'; |
| 111 |
| 112 testLibraryBase() { |
| 113 Expect.equals('1 5 6', fakeFunctionCall(5, 6)); |
| 114 Expect.equals('7, 8', fakeFunctionNSM(7, 8)); |
| 115 Expect.equals('2 9 10', closure(9, 10)); |
| 116 Expect.equals('3 11 12 13 null', closureOpt(11, 12, 13)); |
| 117 Expect.equals('4 14 15 null 16', closureNamed(14, 15, w: 16)); |
| 118 Expect.throws(() => closure('wrong arity'), (e) => e is NoSuchMethodError); |
| 119 } |
| 120 |
| 121 testLibraryReflective() { |
| 122 LibraryMirror lm = reflectClass(D).owner; |
| 123 |
| 124 Expect.equals('1 5 6', |
| 125 lm.invoke(#fakeFunctionCall, [5, 6]).reflectee); |
| 126 Expect.equals('7, 8', |
| 127 lm.invoke(#fakeFunctionNSM, [7, 8]).reflectee); |
| 128 Expect.equals('2 9 10', |
| 129 lm.invoke(#closure, [9, 10]).reflectee); |
| 130 Expect.equals('3 11 12 13 null', |
| 131 lm.invoke(#closureOpt, [11, 12, 13]).reflectee); |
| 132 Expect.equals('4 14 15 null 16', |
| 133 lm.invoke(#closureNamed, [14, 15], {#w: 16}).reflectee); |
| 134 Expect.throws(() => lm.invoke(#closure, ['wrong arity']), |
| 135 (e) => e is NoSuchMethodError); |
| 136 } |
| 137 |
| 138 main() { |
| 139 // Do not access the getters/closures at the base level in this variant. |
| 140 //testInstanceBase(); |
| 141 testInstanceReflective(); |
| 142 //testClassBase(); |
| 143 testClassReflective(); |
| 144 //testLibraryBase(); |
| 145 testLibraryReflective(); |
| 146 } |
OLD | NEW |