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