OLD | NEW |
1 // Copyright (c) 2017, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2017, 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 import 'dart:mirrors'; | 5 import 'dart:mirrors'; |
6 import 'package:expect/expect.dart'; | 6 import 'package:expect/expect.dart'; |
7 | 7 |
8 class Bar { | 8 class Bar { |
9 final String name; | 9 final String name; |
10 | 10 |
11 const Bar(this.name); | 11 const Bar(this.name); |
12 } | 12 } |
13 | 13 |
14 class Foo { | 14 class Foo { |
15 @Bar('bar') | 15 @Bar('bar') |
16 int x = 40; | 16 int x = 40; |
17 | 17 |
18 @Bar('baz') | 18 @Bar('baz') |
19 final String y = 'hi'; | 19 final String y = 'hi'; |
| 20 |
| 21 @Bar('foo') |
| 22 void set z(int val) { |
| 23 x = val; |
| 24 } |
20 } | 25 } |
21 | 26 |
22 void main() { | 27 void main() { |
23 dynamic f = new Foo(); | 28 dynamic f = new Foo(); |
24 Expect.throws(() { | 29 Expect.throws(() { |
25 f.x = 'hello'; | 30 f.x = 'hello'; |
26 }); | 31 }); |
27 f.x += 2; | 32 f.x += 2; |
28 Expect.equals(f.x, 42); | 33 Expect.equals(f.x, 42); |
29 Expect.equals(f.y, 'hi'); | 34 Expect.equals(f.y, 'hi'); |
30 | 35 |
| 36 Expect.throws(() { |
| 37 f.z = 'hello'; |
| 38 }); |
| 39 f.z = 0; |
| 40 Expect.equals(f.x, 0); |
| 41 |
31 var members = reflect(f).type.declarations; | 42 var members = reflect(f).type.declarations; |
32 var x = members[#x] as VariableMirror; | 43 var x = members[#x] as VariableMirror; |
33 var y = members[#y] as VariableMirror; | 44 var y = members[#y] as VariableMirror; |
34 Expect.equals(x.type.simpleName, #int); | 45 Expect.equals(x.type.simpleName, #int); |
35 Expect.equals(y.type.simpleName, #String); | 46 Expect.equals(y.type.simpleName, #String); |
36 } | 47 } |
OLD | NEW |