| 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 // InvocationMirror and noSuchMethod testing. |
| 6 |
| 7 /** Class with noSuchMethod that returns the mirror */ |
| 8 class N { |
| 9 noSuchMethod(InvocationMirror m) => m; |
| 10 |
| 11 get wut => this; |
| 12 |
| 13 flif(int x) { Expect.fail("never get here"); } |
| 14 flaf([int x]) { Expect.fail("never get here"); } |
| 15 flof({int y}) { Expect.fail("never get here"); } |
| 16 |
| 17 final int plif = 99; |
| 18 int get plaf { Expect.fail("never get here"); return 0; } |
| 19 } |
| 20 |
| 21 /** As [N] but also implements 'call', so we can call it with wrong arguments.*/ |
| 22 class C extends N { |
| 23 call(int x) { Expect.fail("never get here"); } |
| 24 } |
| 25 |
| 26 /** |
| 27 * Checks the data of an InvocationMirror. |
| 28 * |
| 29 * Call without optionals for getters, with only positional for setters, |
| 30 * and with both optionals for everything else. |
| 31 */ |
| 32 testInvocationMirror(InvocationMirror im, String name, |
| 33 [List positional, Map named]) { |
| 34 Expect.isTrue(im is InvocationMirror); |
| 35 Expect.equals(name, im.methodName); |
| 36 if (named == null) { |
| 37 Expect.isTrue(im.isAccessor); |
| 38 Expect.isFalse(im.isMethod); |
| 39 if (positional == null) { |
| 40 Expect.isTrue(im.isGetter); |
| 41 Expect.isFalse(im.isSetter); |
| 42 Expect.equals(null, im.positionalArguments); |
| 43 Expect.equals(null, im.positionalArguments); |
| 44 return; |
| 45 } |
| 46 Expect.isTrue(im.isSetter); |
| 47 Expect.isFalse(im.isGetter); |
| 48 Expect.equals(1, im.positionalArguments.length); |
| 49 Expect.equals(positional[0], im.positionalArguments[0]); |
| 50 Expect.equals(null, im.namedArguments); |
| 51 return; |
| 52 } |
| 53 Expect.isTrue(im.isMethod); |
| 54 Expect.isFalse(im.isAccessor); |
| 55 Expect.isFalse(im.isSetter); |
| 56 Expect.isFalse(im.isGetter); |
| 57 |
| 58 Expect.equals(positional.length, im.positionalArguments.length); |
| 59 for (int i = 0; i < positional.length; i++) { |
| 60 Expect.equals(positional[i], im.positionalArguments[i]); |
| 61 } |
| 62 Expect.equals(named.length, im.namedArguments.length); |
| 63 named.forEach((k, v) { |
| 64 Expect.isTrue(im.namedArguments.containsKey(k)); |
| 65 Expect.equals(v, im.namedArguments[k]); |
| 66 }); |
| 67 } |
| 68 |
| 69 |
| 70 // Test different ways that noSuchMethod can be called. |
| 71 testInvocationMirrors() { |
| 72 var n = new N(); |
| 73 var c = new C(); |
| 74 |
| 75 // Missing property/method access. |
| 76 testInvocationMirror(n.bar, 'bar'); |
| 77 testInvocationMirror(n.bar = 42, 'bar=', [42]); |
| 78 testInvocationMirror(n.bar(), 'bar', [], {}); |
| 79 testInvocationMirror(n.bar(42), 'bar', [42], {}); |
| 80 testInvocationMirror(n.bar(x: 42), 'bar', [], {"x": 42}); |
| 81 testInvocationMirror(n.bar(37, x: 42), 'bar', [37], {"x": 42}); |
| 82 testInvocationMirror((n.bar)(), 'bar', [], {}); |
| 83 testInvocationMirror((n.bar)(42), 'bar', [42]); |
| 84 testInvocationMirror((n.bar)(x: 42), 'bar', [], {"x": 42}); |
| 85 testInvocationMirror((n.bar)(37, x: 42), 'bar', [37], {"x": 42}); |
| 86 |
| 87 // Missing operator access. |
| 88 testInvocationMirror(n + 4, '+', [4], {}); |
| 89 testInvocationMirror(n - 4, '-', [4], {}); |
| 90 testInvocationMirror(-n, '+', [], {}); |
| 91 testInvocationMirror(n[42], '[]', [42], {}); |
| 92 testInvocationMirror(n[37] = 42, '[]=', [37, 42], {}); |
| 93 |
| 94 // Calling as function when it's not. |
| 95 testInvocationMirror(n(), 'call', [], {}); |
| 96 testInvocationMirror(n(42), 'call', [42], {}); |
| 97 testInvocationMirror(n(x: 42), 'call', [], {"x": 42}); |
| 98 testInvocationMirror(n(37, x: 42), 'call', [37], {"x": 42});is |
| 99 |
| 100 // Calling with arguments not matching existing call method. |
| 101 testInvocationMirror(c(), 'call', [], {}); |
| 102 testInvocationMirror(c(37, 42), 'call', [37, 42], {}); |
| 103 testInvocationMirror(c(x: 42), 'call', [], {"x": 42}); |
| 104 testInvocationMirror(c(37, x: 42), 'call', [37], {"x": 42}); |
| 105 |
| 106 // Wrong arguments to existing function. |
| 107 testInvocationMirror(n.flif(), "flif", [], {}); |
| 108 testInvocationMirror(n.flif(37, 42), "flif", [37, 42], {}); |
| 109 testInvocationMirror(n.flif(x: 42), "flif", [], {"x": 42}); |
| 110 testInvocationMirror(n.flif(37, x: 42), "flif", [37], {"x": 42}); |
| 111 testInvocationMirror(n.flif = 42, "flif=", [42]); |
| 112 |
| 113 testInvocationMirror(n.flaf(37, 42), "flaf", [37, 42], {}); |
| 114 testInvocationMirror(n.flaf(x: 42), "flaf", [], {"x": 42}); |
| 115 testInvocationMirror(n.flaf(37, x: 42), "flaf", [37], {"x": 42}); |
| 116 testInvocationMirror(n.flaf = 42, "flaf=", [42]); |
| 117 |
| 118 testInvocationMirror(n.flof(37, 42), "flof", [37, 42], {}); |
| 119 testInvocationMirror(n.flof(x: 42), "flof", [], {"x": 42}); |
| 120 testInvocationMirror(n.flof(37, y: 42), "flof", [37], {"y": 42}); |
| 121 testInvocationMirror(n.flof = 42, "flof=", [42]); |
| 122 |
| 123 // Reading works. |
| 124 Expect.isTrue(n.flif is Function); |
| 125 Expect.isTrue(n.flaf is Function); |
| 126 Expect.isTrue(n.flof is Function); |
| 127 |
| 128 // Writing to read-only fields. |
| 129 testInvocationMirror(n.wut = 42, "wut=", [42]); |
| 130 testInvocationMirror(n.plif = 42, "plif=", [42]); |
| 131 testInvocationMirror(n.plaf = 42, "plaf=", [42]); |
| 132 |
| 133 // Trick call to n.call - wut is a getter returning n again. |
| 134 testInvocationMirror(n.wut(42), "call", [42]); |
| 135 |
| 136 // Closurizing a method means that calling it badly will not hit the |
| 137 // original receivers noSuchMethod, only the one inherited from Object |
| 138 // by the closure object. |
| 139 Expect.throws(() { var x = n.flif; x(37, 42); }, |
| 140 (e) => e is noSuchMethodError); |
| 141 Expect.throws(() { var x = c.call; x(37, 42); }, |
| 142 (e) => e is noSuchMethodError); |
| 143 } |
| 144 |
| 145 // Test the NoSuchMethodError thrown by different incorrect calls. |
| 146 testNoSuchMethodErrors() { |
| 147 test(Function block) { |
| 148 Expect.throws(block, (e) => e is noSuchMethodError); |
| 149 } |
| 150 |
| 151 var o = new Object(); |
| 152 test(() => o.bar); |
| 153 test(() => o.bar = 42); |
| 154 test(() => o.bar()); |
| 155 test(() => o.toString = 42); |
| 156 test(() => o.toString(42)); |
| 157 test(() => o.toString(x: 37)); |
| 158 test(() => o.hashCode = 42); |
| 159 test(() => o.hashCode()); // Thrown by int.noSuchMethod. |
| 160 test(() => o()); |
| 161 } |
| 162 |
| 163 main() { |
| 164 testInvocationMirrors(); |
| 165 testNoSuchMethodErrors(); |
| 166 } |
| OLD | NEW |