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 class A {} | 18 final finalTopLevel = 0; |
| 19 class A { |
| 20 final finalInstance = 0; |
| 21 static final finalStatic = 0; |
| 22 } |
19 | 23 |
20 main() { | 24 testMessageContents() { |
21 var mirrors = currentMirrorSystem(); | 25 var mirrors = currentMirrorSystem(); |
22 var libMirror = mirrors.findLibrary(#MirrorsTest); | 26 var libMirror = mirrors.findLibrary(#MirrorsTest); |
23 Expect.throws(() => libMirror.invoke(#foo, []), | 27 Expect.throws(() => libMirror.invoke(#foo, []), |
24 (e) => isNSMContainingFieldName(e, "foo", false)); | 28 (e) => isNSMContainingFieldName(e, "foo", false)); |
25 Expect.throws(() => libMirror.getField(#foo), | 29 Expect.throws(() => libMirror.getField(#foo), |
26 (e) => isNSMContainingFieldName(e, "foo", false)); | 30 (e) => isNSMContainingFieldName(e, "foo", false)); |
27 Expect.throws(() => libMirror.setField(#foo, null), /// vm: ok | 31 Expect.throws(() => libMirror.setField(#foo, null), |
28 (e) => isNSMContainingFieldName(e, "foo", true)); /// vm: contin
ued | 32 (e) => isNSMContainingFieldName(e, "foo", true)); |
| 33 Expect.throws(() => libMirror.setField(#finalTopLevel, null), |
| 34 (e) => isNSMContainingFieldName(e, "finalTopLevel", true)); |
29 | 35 |
30 var classMirror = reflect(A); | 36 var classMirror = reflectClass(A); |
31 Expect.throws(() => classMirror.invoke(#foo, []), | 37 Expect.throws(() => classMirror.invoke(#foo, []), |
32 (e) => isNSMContainingFieldName(e, "foo", false)); | 38 (e) => isNSMContainingFieldName(e, "foo", false)); |
33 Expect.throws(() => classMirror.getField(#foo), | 39 Expect.throws(() => classMirror.getField(#foo), |
34 (e) => isNSMContainingFieldName(e, "foo", false)); | 40 (e) => isNSMContainingFieldName(e, "foo", false)); |
35 Expect.throws(() => classMirror.setField(#foo, null), | 41 Expect.throws(() => classMirror.setField(#foo, null), |
36 (e) => isNSMContainingFieldName(e, "foo", true)); | 42 (e) => isNSMContainingFieldName(e, "foo", true)); |
| 43 Expect.throws(() => classMirror.setField(#finalStatic, null), |
| 44 (e) => isNSMContainingFieldName(e, "finalStatic", true)); |
37 | 45 |
38 var instanceMirror = reflect(new A()); | 46 var instanceMirror = reflect(new A()); |
39 Expect.throws(() => instanceMirror.invoke(#foo, []), | 47 Expect.throws(() => instanceMirror.invoke(#foo, []), |
40 (e) => isNSMContainingFieldName(e, "foo", false)); | 48 (e) => isNSMContainingFieldName(e, "foo", false)); |
41 Expect.throws(() => instanceMirror.getField(#foo), | 49 Expect.throws(() => instanceMirror.getField(#foo), |
42 (e) => isNSMContainingFieldName(e, "foo", false)); | 50 (e) => isNSMContainingFieldName(e, "foo", false)); |
43 Expect.throws(() => instanceMirror.setField(#foo, null), | 51 Expect.throws(() => instanceMirror.setField(#foo, null), |
44 (e) => isNSMContainingFieldName(e, "foo", true)); | 52 (e) => isNSMContainingFieldName(e, "foo", true)); |
| 53 Expect.throws(() => instanceMirror.setField(#finalInstance, null), |
| 54 (e) => isNSMContainingFieldName(e, "finalInstance", true)); |
| 55 } |
45 | 56 |
| 57 expectMatchingErrors(reflectiveAction, baseAction) { |
| 58 var reflectiveError, baseError; |
| 59 try { |
| 60 reflectiveAction(); |
| 61 } catch(e) { |
| 62 reflectiveError = e; |
| 63 } |
| 64 try { |
| 65 baseAction(); |
| 66 } catch(e) { |
| 67 baseError = e; |
| 68 } |
| 69 print("\n==Base==\n $baseError"); |
| 70 print("\n==Reflective==\n $reflectiveError"); |
| 71 Expect.stringEquals(baseError.toString(), reflectiveError.toString()); |
46 } | 72 } |
| 73 |
| 74 testMatchingMessages() { |
| 75 var mirrors = currentMirrorSystem(); |
| 76 var libMirror = mirrors.findLibrary(#MirrorsTest); |
| 77 expectMatchingErrors(() => libMirror.invoke(#foo, []), |
| 78 () => foo()); |
| 79 expectMatchingErrors(() => libMirror.getField(#foo), |
| 80 () => foo); |
| 81 expectMatchingErrors(() => libMirror.setField(#foo, null), |
| 82 () => foo= null); |
| 83 expectMatchingErrors(() => libMirror.setField(#finalTopLevel, null), |
| 84 () => finalTopLevel= null); |
| 85 |
| 86 var classMirror = reflectClass(A); |
| 87 expectMatchingErrors(() => classMirror.invoke(#foo, []), |
| 88 () => A.foo()); |
| 89 expectMatchingErrors(() => classMirror.getField(#foo), |
| 90 () => A.foo); |
| 91 expectMatchingErrors(() => classMirror.setField(#foo, null), |
| 92 () => A.foo= null); |
| 93 expectMatchingErrors(() => classMirror.setField(#finalStatic, null), |
| 94 () => A.finalStatic= null); |
| 95 |
| 96 var instanceMirror = reflect(new A()); |
| 97 expectMatchingErrors(() => instanceMirror.invoke(#foo, []), |
| 98 () => new A().foo()); |
| 99 expectMatchingErrors(() => instanceMirror.getField(#foo), |
| 100 () => new A().foo); |
| 101 expectMatchingErrors(() => instanceMirror.setField(#foo, null), |
| 102 () => new A().foo= null); |
| 103 expectMatchingErrors(() => instanceMirror.setField(#finalInstance, null), |
| 104 () => new A().finalInstance= null); |
| 105 } |
| 106 |
| 107 main() { |
| 108 testMessageContents(); |
| 109 testMatchingMessages(); /// dart2js: ok |
| 110 } |
OLD | NEW |