OLD | NEW |
1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | 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 | 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. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 library MirrorsTest; | 5 library MirrorsTest; |
6 | 6 |
7 import 'dart:mirrors'; | 7 import 'dart:mirrors'; |
8 import 'package:expect/expect.dart'; | 8 import 'package:expect/expect.dart'; |
9 | 9 |
10 bool isNSMContainingFieldName(e, String fieldName, bool isSetter) { | 10 bool isNSMContainingFieldName(e, String fieldName, bool isSetter) { |
11 print(e); | 11 print(e); |
12 if (e is! NoSuchMethodError) return false; | 12 if (e is! NoSuchMethodError) return false; |
13 String needle = fieldName; | 13 String needle = fieldName; |
14 if (isSetter) needle += "="; | 14 if (isSetter) needle += "="; |
15 return "$e".contains(needle) && !"$e".contains(needle + "="); | 15 return "$e".contains(needle) && ! "$e".contains(needle + "="); |
16 } | 16 } |
17 | 17 |
18 final finalTopLevel = 0; | 18 final finalTopLevel = 0; |
19 | |
20 class A { | 19 class A { |
21 final finalInstance = 0; | 20 final finalInstance = 0; |
22 static final finalStatic = 0; | 21 static final finalStatic = 0; |
23 } | 22 } |
24 | |
25 class B { | 23 class B { |
26 B(a, b); | 24 B(a, b); |
27 factory B.fac(a, b) => new B(a, b); | 25 factory B.fac(a, b) => new B(a, b); |
28 } | 26 } |
29 | 27 |
30 testMessageContents() { | 28 testMessageContents() { |
31 var mirrors = currentMirrorSystem(); | 29 var mirrors = currentMirrorSystem(); |
32 var libMirror = mirrors.findLibrary(#MirrorsTest); | 30 var libMirror = mirrors.findLibrary(#MirrorsTest); |
33 Expect.throws(() => libMirror.invoke(#foo, []), | 31 Expect.throws(() => libMirror.invoke(#foo, []), |
34 (e) => isNSMContainingFieldName(e, "foo", false)); | 32 (e) => isNSMContainingFieldName(e, "foo", false)); |
35 Expect.throws(() => libMirror.getField(#foo), | 33 Expect.throws(() => libMirror.getField(#foo), |
36 (e) => isNSMContainingFieldName(e, "foo", false)); | 34 (e) => isNSMContainingFieldName(e, "foo", false)); |
37 Expect.throws(() => libMirror.setField(#foo, null), | 35 Expect.throws(() => libMirror.setField(#foo, null), |
38 (e) => isNSMContainingFieldName(e, "foo", true)); | 36 (e) => isNSMContainingFieldName(e, "foo", true)); |
39 Expect.throws(() => libMirror.setField(#finalTopLevel, null), | 37 Expect.throws(() => libMirror.setField(#finalTopLevel, null), |
40 (e) => isNSMContainingFieldName(e, "finalTopLevel", true)); | 38 (e) => isNSMContainingFieldName(e, "finalTopLevel", true)); |
41 | 39 |
42 var classMirror = reflectClass(A); | 40 var classMirror = reflectClass(A); |
43 Expect.throws(() => classMirror.invoke(#foo, []), | 41 Expect.throws(() => classMirror.invoke(#foo, []), |
44 (e) => isNSMContainingFieldName(e, "foo", false)); | 42 (e) => isNSMContainingFieldName(e, "foo", false)); |
45 Expect.throws(() => classMirror.getField(#foo), | 43 Expect.throws(() => classMirror.getField(#foo), |
46 (e) => isNSMContainingFieldName(e, "foo", false)); | 44 (e) => isNSMContainingFieldName(e, "foo", false)); |
47 Expect.throws(() => classMirror.setField(#foo, null), | 45 Expect.throws(() => classMirror.setField(#foo, null), |
48 (e) => isNSMContainingFieldName(e, "foo", true)); | 46 (e) => isNSMContainingFieldName(e, "foo", true)); |
49 Expect.throws(() => classMirror.setField(#finalStatic, null), | 47 Expect.throws(() => classMirror.setField(#finalStatic, null), |
50 (e) => isNSMContainingFieldName(e, "finalStatic", true)); | 48 (e) => isNSMContainingFieldName(e, "finalStatic", true)); |
51 | 49 |
52 var instanceMirror = reflect(new A()); | 50 var instanceMirror = reflect(new A()); |
53 Expect.throws(() => instanceMirror.invoke(#foo, []), | 51 Expect.throws(() => instanceMirror.invoke(#foo, []), |
54 (e) => isNSMContainingFieldName(e, "foo", false)); | 52 (e) => isNSMContainingFieldName(e, "foo", false)); |
55 Expect.throws(() => instanceMirror.getField(#foo), | 53 Expect.throws(() => instanceMirror.getField(#foo), |
56 (e) => isNSMContainingFieldName(e, "foo", false)); | 54 (e) => isNSMContainingFieldName(e, "foo", false)); |
57 Expect.throws(() => instanceMirror.setField(#foo, null), | 55 Expect.throws(() => instanceMirror.setField(#foo, null), |
58 (e) => isNSMContainingFieldName(e, "foo", true)); | 56 (e) => isNSMContainingFieldName(e, "foo", true)); |
59 Expect.throws(() => instanceMirror.setField(#finalInstance, null), | 57 Expect.throws(() => instanceMirror.setField(#finalInstance, null), |
60 (e) => isNSMContainingFieldName(e, "finalInstance", true)); | 58 (e) => isNSMContainingFieldName(e, "finalInstance", true)); |
61 } | 59 } |
62 | 60 |
63 expectMatchingErrors(reflectiveAction, baseAction) { | 61 expectMatchingErrors(reflectiveAction, baseAction) { |
64 var reflectiveError, baseError; | 62 var reflectiveError, baseError; |
65 try { | 63 try { |
66 reflectiveAction(); | 64 reflectiveAction(); |
67 } catch (e) { | 65 } catch(e) { |
68 reflectiveError = e; | 66 reflectiveError = e; |
69 } | 67 } |
70 | 68 |
71 try { | 69 try { |
72 baseAction(); | 70 baseAction(); |
73 } catch (e) { | 71 } catch(e) { |
74 baseError = e; | 72 baseError = e; |
75 } | 73 } |
76 | 74 |
77 if (baseError.toString() != reflectiveError.toString()) { | 75 if (baseError.toString() != reflectiveError.toString()) { |
78 print("\n==Base==\n $baseError"); | 76 print("\n==Base==\n $baseError"); |
79 print("\n==Reflective==\n $reflectiveError"); | 77 print("\n==Reflective==\n $reflectiveError"); |
80 throw "Expected matching errors"; | 78 throw "Expected matching errors"; |
81 } | 79 } |
82 } | 80 } |
83 | 81 |
84 testMatchingMessages() { | 82 testMatchingMessages() { |
85 var mirrors = currentMirrorSystem(); | 83 var mirrors = currentMirrorSystem(); |
86 var libMirror = mirrors.findLibrary(#MirrorsTest); | 84 var libMirror = mirrors.findLibrary(#MirrorsTest); |
87 expectMatchingErrors(() => libMirror.invoke(#foo, []), () => foo()); | 85 expectMatchingErrors(() => libMirror.invoke(#foo, []), |
88 expectMatchingErrors(() => libMirror.getField(#foo), () => foo); | 86 () => foo()); |
89 expectMatchingErrors(() => libMirror.setField(#foo, null), () => foo = null); | 87 expectMatchingErrors(() => libMirror.getField(#foo), |
| 88 () => foo); |
| 89 expectMatchingErrors(() => libMirror.setField(#foo, null), |
| 90 () => foo= null); |
90 expectMatchingErrors(() => libMirror.setField(#finalTopLevel, null), | 91 expectMatchingErrors(() => libMirror.setField(#finalTopLevel, null), |
91 () => finalTopLevel = null); | 92 () => finalTopLevel= null); |
92 | 93 |
93 var classMirror = reflectClass(A); | 94 var classMirror = reflectClass(A); |
94 expectMatchingErrors(() => classMirror.invoke(#foo, []), () => A.foo()); | 95 expectMatchingErrors(() => classMirror.invoke(#foo, []), |
95 expectMatchingErrors(() => classMirror.getField(#foo), () => A.foo); | 96 () => A.foo()); |
96 expectMatchingErrors( | 97 expectMatchingErrors(() => classMirror.getField(#foo), |
97 () => classMirror.setField(#foo, null), () => A.foo = null); | 98 () => A.foo); |
| 99 expectMatchingErrors(() => classMirror.setField(#foo, null), |
| 100 () => A.foo= null); |
98 expectMatchingErrors(() => classMirror.setField(#finalStatic, null), | 101 expectMatchingErrors(() => classMirror.setField(#finalStatic, null), |
99 () => A.finalStatic = null); | 102 () => A.finalStatic= null); |
100 expectMatchingErrors(() => classMirror.newInstance(#constructor, [1, 2, 3]), | 103 expectMatchingErrors(() => classMirror.newInstance(#constructor, [1, 2, 3]), |
101 () => new A.constructor(1, 2, 3)); | 104 () => new A.constructor(1, 2, 3)); |
102 | 105 |
103 var instanceMirror = reflect(new A()); | 106 var instanceMirror = reflect(new A()); |
104 expectMatchingErrors( | 107 expectMatchingErrors(() => instanceMirror.invoke(#foo, []), |
105 () => instanceMirror.invoke(#foo, []), () => new A().foo()); | 108 () => new A().foo()); |
106 expectMatchingErrors(() => instanceMirror.getField(#foo), () => new A().foo); | 109 expectMatchingErrors(() => instanceMirror.getField(#foo), |
107 expectMatchingErrors( | 110 () => new A().foo); |
108 () => instanceMirror.setField(#foo, null), () => new A().foo = null); | 111 expectMatchingErrors(() => instanceMirror.setField(#foo, null), |
| 112 () => new A().foo= null); |
109 expectMatchingErrors(() => instanceMirror.setField(#finalInstance, null), | 113 expectMatchingErrors(() => instanceMirror.setField(#finalInstance, null), |
110 () => new A().finalInstance = null); | 114 () => new A().finalInstance= null); |
111 } | 115 } |
112 | 116 |
113 main() { | 117 main() { |
114 testMessageContents(); | 118 testMessageContents(); |
115 testMatchingMessages(); //# dart2js: ok | 119 testMatchingMessages(); //# dart2js: ok |
116 } | 120 } |
OLD | NEW |