OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2013, 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 test; |
| 6 |
| 7 import 'dart:mirrors'; |
| 8 import 'package:expect/expect.dart'; |
| 9 |
| 10 typedef int _F(int i); |
| 11 |
| 12 class _C<_T> { |
| 13 get g {} |
| 14 set s(x) {} |
| 15 m(_p) {} |
| 16 get _g {} |
| 17 set _s(x) {} |
| 18 _m() {} |
| 19 } |
| 20 |
| 21 main() { |
| 22 // Test private symbols are distinct across libraries, and the same within a |
| 23 // library when created multiple ways. Test the string can be properly |
| 24 // extracted. |
| 25 LibraryMirror libcore = currentMirrorSystem().findLibrary(#dart.core); |
| 26 LibraryMirror libmath = currentMirrorSystem().findLibrary(#dart.math); |
| 27 LibraryMirror libtest = currentMirrorSystem().findLibrary(#test); |
| 28 |
| 29 Symbol corefoo = MirrorSystem.getSymbol('foo', libcore); |
| 30 Symbol mathfoo = MirrorSystem.getSymbol('foo', libmath); |
| 31 Symbol testfoo = MirrorSystem.getSymbol('foo', libtest); |
| 32 Symbol nullfoo1 = MirrorSystem.getSymbol('foo'); |
| 33 Symbol nullfoo2 = MirrorSystem.getSymbol('foo', null); |
| 34 |
| 35 Expect.equals(corefoo, mathfoo); |
| 36 Expect.equals(mathfoo, testfoo); |
| 37 Expect.equals(testfoo, corefoo); |
| 38 Expect.equals(nullfoo1, corefoo); |
| 39 Expect.equals(nullfoo2, corefoo); |
| 40 |
| 41 Expect.equals('foo', MirrorSystem.getName(corefoo)); |
| 42 Expect.equals('foo', MirrorSystem.getName(mathfoo)); |
| 43 Expect.equals('foo', MirrorSystem.getName(testfoo)); |
| 44 Expect.equals('foo', MirrorSystem.getName(#foo)); |
| 45 Expect.equals('foo', MirrorSystem.getName(nullfoo1)); |
| 46 Expect.equals('foo', MirrorSystem.getName(nullfoo2)); |
| 47 |
| 48 Symbol core_foo = MirrorSystem.getSymbol('_foo', libcore); |
| 49 Symbol math_foo = MirrorSystem.getSymbol('_foo', libmath); |
| 50 Symbol test_foo = MirrorSystem.getSymbol('_foo', libtest); |
| 51 |
| 52 Expect.equals('_foo', MirrorSystem.getName(core_foo)); |
| 53 Expect.equals('_foo', MirrorSystem.getName(math_foo)); |
| 54 Expect.equals('_foo', MirrorSystem.getName(test_foo)); |
| 55 Expect.equals('_foo', MirrorSystem.getName(#_foo)); |
| 56 |
| 57 Expect.notEquals(core_foo, math_foo); |
| 58 Expect.notEquals(math_foo, test_foo); |
| 59 Expect.notEquals(test_foo, core_foo); |
| 60 |
| 61 Expect.notEquals(corefoo, core_foo); |
| 62 Expect.notEquals(mathfoo, math_foo); |
| 63 Expect.notEquals(testfoo, test_foo); |
| 64 |
| 65 Expect.equals(test_foo, #_foo); |
| 66 |
| 67 |
| 68 // Test interactions with the manglings for getters and setters, etc. |
| 69 ClassMirror cm = reflectClass(_C); |
| 70 Expect.equals(#_C, cm.simpleName); |
| 71 Expect.equals('_C', MirrorSystem.getName(cm.simpleName)); |
| 72 |
| 73 MethodMirror mm = cm.declarations[#g]; |
| 74 Expect.isNotNull(mm); |
| 75 Expect.isTrue(mm.isGetter); |
| 76 Expect.equals(#g, mm.simpleName); |
| 77 Expect.equals('g', MirrorSystem.getName(mm.simpleName)); |
| 78 |
| 79 mm = cm.declarations[const Symbol('s=')]; |
| 80 Expect.isNotNull(mm); |
| 81 Expect.isTrue(mm.isSetter); |
| 82 Expect.equals(const Symbol('s='), mm.simpleName); |
| 83 Expect.equals('s=', MirrorSystem.getName(mm.simpleName)); |
| 84 |
| 85 mm = cm.declarations[#m]; |
| 86 Expect.isNotNull(mm); |
| 87 Expect.isTrue(mm.isRegularMethod); |
| 88 Expect.equals(#m, mm.simpleName); |
| 89 Expect.equals('m', MirrorSystem.getName(mm.simpleName)); |
| 90 |
| 91 mm = cm.declarations[#_g]; |
| 92 Expect.isNotNull(mm); |
| 93 Expect.isTrue(mm.isGetter); |
| 94 Expect.equals(#_g, mm.simpleName); |
| 95 Expect.equals('_g', MirrorSystem.getName(mm.simpleName)); |
| 96 |
| 97 mm = cm.declarations[MirrorSystem.getSymbol('_s=', libtest)]; |
| 98 Expect.isNotNull(mm); |
| 99 Expect.isTrue(mm.isSetter); |
| 100 Expect.equals(MirrorSystem.getSymbol('_s=', libtest), mm.simpleName); |
| 101 Expect.equals('_s=', MirrorSystem.getName(mm.simpleName)); |
| 102 |
| 103 mm = cm.declarations[#_m]; |
| 104 Expect.isNotNull(mm); |
| 105 Expect.isTrue(mm.isRegularMethod); |
| 106 Expect.equals(#_m, mm.simpleName); |
| 107 Expect.equals('_m', MirrorSystem.getName(mm.simpleName)); |
| 108 |
| 109 TypeVariableMirror tvm = cm.typeVariables[0]; |
| 110 Expect.isNotNull(tvm); |
| 111 Expect.equals(#_T, tvm.simpleName); |
| 112 Expect.equals('_T', MirrorSystem.getName(tvm.simpleName)); |
| 113 |
| 114 TypedefMirror tdm = reflectType(_F); |
| 115 Expect.equals(#_F, tdm.simpleName); |
| 116 Expect.equals('_F', MirrorSystem.getName(tdm.simpleName)); |
| 117 |
| 118 ParameterMirror pm = (cm.declarations[#m] as MethodMirror).parameters[0]; |
| 119 Expect.equals(#_p, pm.simpleName); |
| 120 Expect.equals('_p', MirrorSystem.getName(pm.simpleName)); |
| 121 |
| 122 |
| 123 // Private symbol without a library. |
| 124 Expect.throws(() => MirrorSystem.getSymbol('_private'), |
| 125 (e) => e is ArgumentError); |
| 126 |
| 127 var notALibraryMirror = 7; |
| 128 Expect.throws(() => MirrorSystem.getSymbol('_private', notALibraryMirror), |
| 129 (e) => e is ArgumentError || e is TypeError); |
| 130 |
| 131 Expect.throws(() => MirrorSystem.getSymbol('public', notALibraryMirror), |
| 132 (e) => e is ArgumentError || e is TypeError); |
| 133 } |
OLD | NEW |