| 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 class A { | 19 class A { |
| 20 final finalInstance = 0; | 20 final finalInstance = 0; |
| 21 static final finalStatic = 0; | 21 static final finalStatic = 0; |
| 22 } | 22 } |
| 23 class B { |
| 24 B(a, b); |
| 25 factory B.fac(a, b) => new B(a, b); |
| 26 } |
| 23 | 27 |
| 24 testMessageContents() { | 28 testMessageContents() { |
| 25 var mirrors = currentMirrorSystem(); | 29 var mirrors = currentMirrorSystem(); |
| 26 var libMirror = mirrors.findLibrary(#MirrorsTest); | 30 var libMirror = mirrors.findLibrary(#MirrorsTest); |
| 27 Expect.throws(() => libMirror.invoke(#foo, []), | 31 Expect.throws(() => libMirror.invoke(#foo, []), |
| 28 (e) => isNSMContainingFieldName(e, "foo", false)); | 32 (e) => isNSMContainingFieldName(e, "foo", false)); |
| 29 Expect.throws(() => libMirror.getField(#foo), | 33 Expect.throws(() => libMirror.getField(#foo), |
| 30 (e) => isNSMContainingFieldName(e, "foo", false)); | 34 (e) => isNSMContainingFieldName(e, "foo", false)); |
| 31 Expect.throws(() => libMirror.setField(#foo, null), | 35 Expect.throws(() => libMirror.setField(#foo, null), |
| 32 (e) => isNSMContainingFieldName(e, "foo", true)); | 36 (e) => isNSMContainingFieldName(e, "foo", true)); |
| (...skipping 21 matching lines...) Expand all Loading... |
| 54 (e) => isNSMContainingFieldName(e, "finalInstance", true)); | 58 (e) => isNSMContainingFieldName(e, "finalInstance", true)); |
| 55 } | 59 } |
| 56 | 60 |
| 57 expectMatchingErrors(reflectiveAction, baseAction) { | 61 expectMatchingErrors(reflectiveAction, baseAction) { |
| 58 var reflectiveError, baseError; | 62 var reflectiveError, baseError; |
| 59 try { | 63 try { |
| 60 reflectiveAction(); | 64 reflectiveAction(); |
| 61 } catch(e) { | 65 } catch(e) { |
| 62 reflectiveError = e; | 66 reflectiveError = e; |
| 63 } | 67 } |
| 68 |
| 64 try { | 69 try { |
| 65 baseAction(); | 70 baseAction(); |
| 66 } catch(e) { | 71 } catch(e) { |
| 67 baseError = e; | 72 baseError = e; |
| 68 } | 73 } |
| 69 print("\n==Base==\n $baseError"); | 74 |
| 70 print("\n==Reflective==\n $reflectiveError"); | 75 if (baseError.toString() != reflectiveError.toString()) { |
| 71 Expect.stringEquals(baseError.toString(), reflectiveError.toString()); | 76 print("\n==Base==\n $baseError"); |
| 77 print("\n==Reflective==\n $reflectiveError"); |
| 78 throw "Expected matching errors"; |
| 79 } |
| 72 } | 80 } |
| 73 | 81 |
| 74 testMatchingMessages() { | 82 testMatchingMessages() { |
| 75 var mirrors = currentMirrorSystem(); | 83 var mirrors = currentMirrorSystem(); |
| 76 var libMirror = mirrors.findLibrary(#MirrorsTest); | 84 var libMirror = mirrors.findLibrary(#MirrorsTest); |
| 77 expectMatchingErrors(() => libMirror.invoke(#foo, []), | 85 expectMatchingErrors(() => libMirror.invoke(#foo, []), |
| 78 () => foo()); | 86 () => foo()); |
| 79 expectMatchingErrors(() => libMirror.getField(#foo), | 87 expectMatchingErrors(() => libMirror.getField(#foo), |
| 80 () => foo); | 88 () => foo); |
| 81 expectMatchingErrors(() => libMirror.setField(#foo, null), | 89 expectMatchingErrors(() => libMirror.setField(#foo, null), |
| 82 () => foo= null); | 90 () => foo= null); |
| 83 expectMatchingErrors(() => libMirror.setField(#finalTopLevel, null), | 91 expectMatchingErrors(() => libMirror.setField(#finalTopLevel, null), |
| 84 () => finalTopLevel= null); | 92 () => finalTopLevel= null); |
| 85 | 93 |
| 86 var classMirror = reflectClass(A); | 94 var classMirror = reflectClass(A); |
| 87 expectMatchingErrors(() => classMirror.invoke(#foo, []), | 95 expectMatchingErrors(() => classMirror.invoke(#foo, []), |
| 88 () => A.foo()); | 96 () => A.foo()); |
| 89 expectMatchingErrors(() => classMirror.getField(#foo), | 97 expectMatchingErrors(() => classMirror.getField(#foo), |
| 90 () => A.foo); | 98 () => A.foo); |
| 91 expectMatchingErrors(() => classMirror.setField(#foo, null), | 99 expectMatchingErrors(() => classMirror.setField(#foo, null), |
| 92 () => A.foo= null); | 100 () => A.foo= null); |
| 93 expectMatchingErrors(() => classMirror.setField(#finalStatic, null), | 101 expectMatchingErrors(() => classMirror.setField(#finalStatic, null), |
| 94 () => A.finalStatic= null); | 102 () => A.finalStatic= null); |
| 103 expectMatchingErrors(() => classMirror.newInstance(#constructor, [1, 2, 3]), |
| 104 () => new A.constructor(1, 2, 3)); |
| 95 | 105 |
| 96 var instanceMirror = reflect(new A()); | 106 var instanceMirror = reflect(new A()); |
| 97 expectMatchingErrors(() => instanceMirror.invoke(#foo, []), | 107 expectMatchingErrors(() => instanceMirror.invoke(#foo, []), |
| 98 () => new A().foo()); | 108 () => new A().foo()); |
| 99 expectMatchingErrors(() => instanceMirror.getField(#foo), | 109 expectMatchingErrors(() => instanceMirror.getField(#foo), |
| 100 () => new A().foo); | 110 () => new A().foo); |
| 101 expectMatchingErrors(() => instanceMirror.setField(#foo, null), | 111 expectMatchingErrors(() => instanceMirror.setField(#foo, null), |
| 102 () => new A().foo= null); | 112 () => new A().foo= null); |
| 103 expectMatchingErrors(() => instanceMirror.setField(#finalInstance, null), | 113 expectMatchingErrors(() => instanceMirror.setField(#finalInstance, null), |
| 104 () => new A().finalInstance= null); | 114 () => new A().finalInstance= null); |
| 105 } | 115 } |
| 106 | 116 |
| 107 main() { | 117 main() { |
| 108 testMessageContents(); | 118 testMessageContents(); |
| 109 testMatchingMessages(); /// dart2js: ok | 119 testMatchingMessages(); /// dart2js: ok |
| 110 } | 120 } |
| OLD | NEW |