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 @fisk @symbol | 5 @string @symbol |
6 library test.metadata_test; | 6 library test.metadata_test; |
7 | 7 |
8 import 'dart:mirrors'; | 8 import 'dart:mirrors'; |
9 | 9 |
10 const fisk = 'a metadata string'; | 10 const string = 'a metadata string'; |
11 | 11 |
12 const symbol = const Symbol('fisk'); | 12 const symbol = const Symbol('symbol'); |
13 | 13 |
14 @symbol @fisk | 14 const hest = 'hest'; |
| 15 |
| 16 @symbol @string |
15 class MyClass { | 17 class MyClass { |
16 @fisk @symbol @fisk | 18 @hest @hest @symbol |
| 19 var x; |
| 20 |
| 21 @string @symbol @string |
17 myMethod() => 1; | 22 myMethod() => 1; |
18 } | 23 } |
19 | 24 |
20 checkMetadata(DeclarationMirror mirror, List expectedMetadata) { | 25 checkMetadata(DeclarationMirror mirror, List expectedMetadata) { |
21 List metadata = mirror.metadata.map((m) => m.reflectee).toList(); | 26 List metadata = mirror.metadata.map((m) => m.reflectee).toList(); |
22 if (metadata == null) { | 27 if (metadata == null) { |
23 throw 'Null metadata on $mirror'; | 28 throw 'Null metadata on $mirror'; |
24 } | 29 } |
25 int expectedLength = expectedMetadata.length; | 30 int expectedLength = expectedMetadata.length; |
26 int actualLength = metadata.length; | 31 int actualLength = metadata.length; |
27 if (expectedLength != actualLength) { | 32 if (expectedLength != actualLength) { |
28 throw 'Expected length = $expectedLength, but got length = $actualLength.'; | 33 throw 'Expected length = $expectedLength, but got length = $actualLength.'; |
29 } | 34 } |
30 for (int i = 0; i < expectedLength; i++) { | 35 for (int i = 0; i < expectedLength; i++) { |
31 if (metadata[i] != expectedMetadata[i]) { | 36 if (metadata[i] != expectedMetadata[i]) { |
32 throw '${metadata[i]} is not "${expectedMetadata[i]}"' | 37 throw '${metadata[i]} is not "${expectedMetadata[i]}"' |
33 ' in $mirror at index $i'; | 38 ' in $mirror at index $i'; |
34 } | 39 } |
35 } | 40 } |
36 print(metadata); | 41 print(metadata); |
37 } | 42 } |
38 | 43 |
39 @symbol @fisk @symbol | 44 @symbol @string @symbol |
40 main() { | 45 main() { |
41 if (MirrorSystem.getName(symbol) != 'fisk') { | 46 if (MirrorSystem.getName(symbol) != 'symbol') { |
42 // This happened in dart2js due to how early library metadata is | 47 // This happened in dart2js due to how early library metadata is |
43 // computed. | 48 // computed. |
44 throw 'Bad constant: $symbol'; | 49 throw 'Bad constant: $symbol'; |
45 } | 50 } |
46 | 51 |
47 MirrorSystem mirrors = currentMirrorSystem(); | 52 MirrorSystem mirrors = currentMirrorSystem(); |
48 checkMetadata(mirrors.findLibrary(const Symbol('test.metadata_test')).first, | 53 checkMetadata(mirrors.findLibrary(const Symbol('test.metadata_test')).first, |
49 [fisk, symbol]); | 54 [string, symbol]); |
50 checkMetadata(reflect(new MyClass()).type, [symbol, fisk]); | 55 ClassMirror myClassMirror = reflectClass(MyClass); |
| 56 checkMetadata(myClassMirror, [symbol, string]); |
51 ClosureMirror closure = reflect(main); | 57 ClosureMirror closure = reflect(main); |
52 checkMetadata(closure.function, [symbol, fisk, symbol]); | 58 checkMetadata(closure.function, [symbol, string, symbol]); |
53 closure = reflect(new MyClass().myMethod); | 59 closure = reflect(new MyClass().myMethod); |
54 checkMetadata(closure.function, [fisk, symbol, fisk]); | 60 checkMetadata(closure.function, [string, symbol, string]); |
| 61 |
| 62 VariableMirror xMirror = myClassMirror.variables[const Symbol('x')]; |
| 63 checkMetadata(xMirror, [hest, hest, symbol]); |
55 | 64 |
56 // TODO(ahe): Test local functions. | 65 // TODO(ahe): Test local functions. |
57 } | 66 } |
OLD | NEW |