OLD | NEW |
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 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 | 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 test.reflect_model_test; | 5 library test.reflect_model_test; |
6 | 6 |
7 import 'dart:mirrors'; | 7 import 'dart:mirrors'; |
8 | 8 |
9 import 'package:expect/expect.dart'; | 9 import 'package:expect/expect.dart'; |
10 | 10 |
11 import 'model.dart'; | 11 import 'model.dart'; |
12 import 'stringify.dart'; | 12 import 'stringify.dart'; |
13 | 13 |
14 isNoSuchMethodError(e) => e is NoSuchMethodError; | 14 isNoSuchMethodError(e) => e is NoSuchMethodError; |
15 | 15 |
| 16 variablesOf(ClassMirror cm) { |
| 17 var result = new Map(); |
| 18 cm.declarations.forEach((k,v) { |
| 19 if (v is VariableMirror) result[k] = v; |
| 20 }); |
| 21 return result; |
| 22 } |
| 23 |
| 24 gettersOf(ClassMirror cm) { |
| 25 var result = new Map(); |
| 26 cm.declarations.forEach((k,v) { |
| 27 if (v is MethodMirror && v.isGetter) result[k] = v; |
| 28 }); |
| 29 return result; |
| 30 } |
| 31 |
| 32 settersOf(ClassMirror cm) { |
| 33 var result = new Map(); |
| 34 cm.declarations.forEach((k,v) { |
| 35 if (v is MethodMirror && v.isSetter) result[k] = v; |
| 36 }); |
| 37 return result; |
| 38 } |
| 39 |
| 40 methodsOf(ClassMirror cm) { |
| 41 var result = new Map(); |
| 42 cm.declarations.forEach((k,v) { |
| 43 if (v is MethodMirror && v.isRegularMethod) result[k] = v; |
| 44 }); |
| 45 return result; |
| 46 } |
| 47 |
16 main() { | 48 main() { |
17 var unnamed = new Symbol(''); | 49 var unnamed = new Symbol(''); |
18 var field = new Symbol('field'); | 50 var field = new Symbol('field'); |
19 var instanceMethod = new Symbol('instanceMethod'); | 51 var instanceMethod = new Symbol('instanceMethod'); |
20 var accessor = new Symbol('accessor'); | 52 var accessor = new Symbol('accessor'); |
21 var aMethod = new Symbol('aMethod'); | 53 var aMethod = new Symbol('aMethod'); |
22 var bMethod = new Symbol('bMethod'); | 54 var bMethod = new Symbol('bMethod'); |
23 var cMethod = new Symbol('cMethod'); | 55 var cMethod = new Symbol('cMethod'); |
24 | 56 |
25 var aClass = reflectClass(A); | 57 var aClass = reflectClass(A); |
26 var bClass = reflectClass(B); | 58 var bClass = reflectClass(B); |
27 var cClass = reflectClass(C); | 59 var cClass = reflectClass(C); |
28 var a = aClass.newInstance(unnamed, []); | 60 var a = aClass.newInstance(unnamed, []); |
29 var b = bClass.newInstance(unnamed, []); | 61 var b = bClass.newInstance(unnamed, []); |
30 var c = cClass.newInstance(unnamed, []); | 62 var c = cClass.newInstance(unnamed, []); |
31 | 63 |
32 expect('{field: Variable(s(field) in s(A))}', aClass.variables); | 64 expect('{field: Variable(s(field) in s(A))}', variablesOf(aClass)); |
33 expect('{}', bClass.variables); | 65 expect('{}', variablesOf(bClass)); |
34 expect('{}', cClass.variables); | 66 expect('{}', variablesOf(cClass)); |
35 | 67 |
36 Expect.isNull(a.getField(field).reflectee); | 68 Expect.isNull(a.getField(field).reflectee); |
37 Expect.equals('B:get field', b.getField(field).reflectee); | 69 Expect.equals('B:get field', b.getField(field).reflectee); |
38 Expect.equals('B:get field', c.getField(field).reflectee); | 70 Expect.equals('B:get field', c.getField(field).reflectee); |
39 | 71 |
40 Expect.equals(42, a.setField(field, 42).reflectee); | 72 Expect.equals(42, a.setField(field, 42).reflectee); |
41 Expect.equals(87, b.setField(field, 87).reflectee); | 73 Expect.equals(87, b.setField(field, 87).reflectee); |
42 Expect.equals(89, c.setField(field, 89).reflectee); | 74 Expect.equals(89, c.setField(field, 89).reflectee); |
43 | 75 |
44 Expect.equals(42, a.getField(field).reflectee); | 76 Expect.equals(42, a.getField(field).reflectee); |
45 Expect.equals('B:get field', b.getField(field).reflectee); | 77 Expect.equals('B:get field', b.getField(field).reflectee); |
46 Expect.equals('B:get field', c.getField(field).reflectee); | 78 Expect.equals('B:get field', c.getField(field).reflectee); |
47 Expect.equals(89, fieldC); | 79 Expect.equals(89, fieldC); |
48 | 80 |
49 expect('{accessor: Method(s(accessor) in s(A), getter)' | 81 expect('{accessor: Method(s(accessor) in s(A), getter)' |
50 '}', | 82 '}', |
51 aClass.getters); | 83 gettersOf(aClass)); |
52 expect('{accessor: Method(s(accessor) in s(B), getter)' | 84 expect('{accessor: Method(s(accessor) in s(B), getter)' |
53 ', field: Method(s(field) in s(B), getter)}', | 85 ', field: Method(s(field) in s(B), getter)}', |
54 bClass.getters); | 86 gettersOf(bClass)); |
55 expect('{accessor: Method(s(accessor) in s(C), getter)}', | 87 expect('{accessor: Method(s(accessor) in s(C), getter)}', |
56 cClass.getters); | 88 gettersOf(cClass)); |
57 | 89 |
58 expect('{accessor=: Method(s(accessor=) in s(A), setter)' | 90 expect('{accessor=: Method(s(accessor=) in s(A), setter)' |
59 '}', | 91 '}', |
60 aClass.setters); | 92 settersOf(aClass)); |
61 expect('{accessor=: Method(s(accessor=) in s(B), setter)}', | 93 expect('{accessor=: Method(s(accessor=) in s(B), setter)}', |
62 bClass.setters); | 94 settersOf(bClass)); |
63 expect('{accessor=: Method(s(accessor=) in s(C), setter)' | 95 expect('{accessor=: Method(s(accessor=) in s(C), setter)' |
64 ', field=: Method(s(field=) in s(C), setter)}', | 96 ', field=: Method(s(field=) in s(C), setter)}', |
65 cClass.setters); | 97 settersOf(cClass)); |
66 | 98 |
67 Expect.equals('A:instanceMethod(7)', a.invoke(instanceMethod, [7]).reflectee); | 99 Expect.equals('A:instanceMethod(7)', a.invoke(instanceMethod, [7]).reflectee); |
68 Expect.equals('B:instanceMethod(9)', b.invoke(instanceMethod, [9]).reflectee); | 100 Expect.equals('B:instanceMethod(9)', b.invoke(instanceMethod, [9]).reflectee); |
69 Expect.equals( | 101 Expect.equals( |
70 'C:instanceMethod(13)', c.invoke(instanceMethod, [13]).reflectee); | 102 'C:instanceMethod(13)', c.invoke(instanceMethod, [13]).reflectee); |
71 | 103 |
72 expect( | 104 expect( |
73 '{aMethod: Method(s(aMethod) in s(A))' | 105 '{aMethod: Method(s(aMethod) in s(A))' |
74 ', instanceMethod: Method(s(instanceMethod) in s(A))}', | 106 ', instanceMethod: Method(s(instanceMethod) in s(A))}', |
75 aClass.methods); | 107 methodsOf(aClass)); |
76 | 108 |
77 expect( | 109 expect( |
78 '{bMethod: Method(s(bMethod) in s(B))' | 110 '{bMethod: Method(s(bMethod) in s(B))' |
79 ', instanceMethod: Method(s(instanceMethod) in s(B))}', | 111 ', instanceMethod: Method(s(instanceMethod) in s(B))}', |
80 bClass.methods); | 112 methodsOf(bClass)); |
81 expect( | 113 expect( |
82 '{cMethod: Method(s(cMethod) in s(C))' | 114 '{cMethod: Method(s(cMethod) in s(C))' |
83 ', instanceMethod: Method(s(instanceMethod) in s(C))}', | 115 ', instanceMethod: Method(s(instanceMethod) in s(C))}', |
84 cClass.methods); | 116 methodsOf(cClass)); |
85 | 117 |
86 Expect.equals('A:get accessor', a.getField(accessor).reflectee); | 118 Expect.equals('A:get accessor', a.getField(accessor).reflectee); |
87 Expect.equals('B:get accessor', b.getField(accessor).reflectee); | 119 Expect.equals('B:get accessor', b.getField(accessor).reflectee); |
88 Expect.equals('C:get accessor', c.getField(accessor).reflectee); | 120 Expect.equals('C:get accessor', c.getField(accessor).reflectee); |
89 | 121 |
90 Expect.equals('foo', a.setField(accessor, 'foo').reflectee); | 122 Expect.equals('foo', a.setField(accessor, 'foo').reflectee); |
91 Expect.equals('bar', b.setField(accessor, 'bar').reflectee); | 123 Expect.equals('bar', b.setField(accessor, 'bar').reflectee); |
92 Expect.equals('baz', c.setField(accessor, 'baz').reflectee); | 124 Expect.equals('baz', c.setField(accessor, 'baz').reflectee); |
93 | 125 |
94 Expect.equals('foo', accessorA); | 126 Expect.equals('foo', accessorA); |
95 Expect.equals('bar', accessorB); | 127 Expect.equals('bar', accessorB); |
96 Expect.equals('baz', accessorC); | 128 Expect.equals('baz', accessorC); |
97 | 129 |
98 Expect.equals('aMethod', a.invoke(aMethod, []).reflectee); | 130 Expect.equals('aMethod', a.invoke(aMethod, []).reflectee); |
99 Expect.equals('aMethod', b.invoke(aMethod, []).reflectee); | 131 Expect.equals('aMethod', b.invoke(aMethod, []).reflectee); |
100 Expect.equals('aMethod', c.invoke(aMethod, []).reflectee); | 132 Expect.equals('aMethod', c.invoke(aMethod, []).reflectee); |
101 | 133 |
102 Expect.throws(() { a.invoke(bMethod, []); }, isNoSuchMethodError); | 134 Expect.throws(() { a.invoke(bMethod, []); }, isNoSuchMethodError); |
103 Expect.equals('bMethod', b.invoke(bMethod, []).reflectee); | 135 Expect.equals('bMethod', b.invoke(bMethod, []).reflectee); |
104 Expect.equals('bMethod', c.invoke(bMethod, []).reflectee); | 136 Expect.equals('bMethod', c.invoke(bMethod, []).reflectee); |
105 | 137 |
106 Expect.throws(() { a.invoke(cMethod, []); }, isNoSuchMethodError); | 138 Expect.throws(() { a.invoke(cMethod, []); }, isNoSuchMethodError); |
107 Expect.throws(() { b.invoke(cMethod, []); }, isNoSuchMethodError); | 139 Expect.throws(() { b.invoke(cMethod, []); }, isNoSuchMethodError); |
108 Expect.equals('cMethod', c.invoke(cMethod, []).reflectee); | 140 Expect.equals('cMethod', c.invoke(cMethod, []).reflectee); |
109 } | 141 } |
OLD | NEW |