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 @fisk @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 fisk = 'a metadata string'; |
11 | 11 |
12 const symbol = const Symbol('fisk'); | 12 const symbol = const Symbol('fisk'); |
13 | 13 |
14 @symbol @fisk | 14 @symbol @fisk |
15 class MyClass { | 15 class MyClass { |
16 @fisk @symbol @fisk | |
17 myMethod() => 1; | |
16 } | 18 } |
17 | 19 |
18 checkMetadata(DeclarationMirror mirror, List expectedMetadata) { | 20 checkMetadata(DeclarationMirror mirror, List expectedMetadata) { |
19 List metadata = mirror.metadata.map((m) => m.reflectee).toList(); | 21 List metadata = mirror.metadata.map((m) => m.reflectee).toList(); |
20 if (metadata == null) { | 22 if (metadata == null) { |
21 throw 'Null metadata on $mirror'; | 23 throw 'Null metadata on $mirror'; |
22 } | 24 } |
23 int expectedLength = expectedMetadata.length; | 25 int expectedLength = expectedMetadata.length; |
24 int actualLength = metadata.length; | 26 int actualLength = metadata.length; |
25 if (expectedLength != actualLength) { | 27 if (expectedLength != actualLength) { |
26 throw 'Expected length = $expectedLength, but got length = $actualLength.'; | 28 throw 'Expected length = $expectedLength, but got length = $actualLength.'; |
27 } | 29 } |
28 for (int i = 0; i < expectedLength; i++) { | 30 for (int i = 0; i < expectedLength; i++) { |
29 if (metadata[i] != expectedMetadata[i]) { | 31 if (metadata[i] != expectedMetadata[i]) { |
30 throw '${metadata[i]} is not "${expectedMetadata[i]}"' | 32 throw '${metadata[i]} is not "${expectedMetadata[i]}"' |
31 ' in $mirror at index $i'; | 33 ' in $mirror at index $i'; |
32 } | 34 } |
33 } | 35 } |
34 print(metadata); | 36 print(metadata); |
35 } | 37 } |
36 | 38 |
39 @symbol @fisk @symbol | |
37 main() { | 40 main() { |
38 if (MirrorSystem.getName(symbol) != 'fisk') { | 41 if (MirrorSystem.getName(symbol) != 'fisk') { |
39 // This happened in dart2js due to how early library metadata is | 42 // This happened in dart2js due to how early library metadata is |
40 // computed. | 43 // computed. |
41 throw 'Bad constant: $symbol'; | 44 throw 'Bad constant: $symbol'; |
42 } | 45 } |
43 | 46 |
44 MirrorSystem mirrors = currentMirrorSystem(); | 47 MirrorSystem mirrors = currentMirrorSystem(); |
45 checkMetadata(mirrors.findLibrary(const Symbol('test.metadata_test')).first, | 48 checkMetadata(mirrors.findLibrary(const Symbol('test.metadata_test')).first, |
46 [fisk, symbol]); | 49 [fisk, symbol]); |
47 checkMetadata(reflect(new MyClass()).type, [symbol, fisk]); | 50 checkMetadata(reflect(new MyClass()).type, [symbol, fisk]); |
51 ClosureMirror closure = reflect(main); | |
52 checkMetadata(closure.function, [symbol, fisk, symbol]); | |
53 closure = reflect(new MyClass().myMethod); | |
54 checkMetadata(closure.function, [fisk, symbol, fisk]); | |
48 } | 55 } |
kasperl
2013/06/03 06:12:40
No support for local functions yet?
ahe
2013/06/03 09:13:54
Don't know. Added a TODO.
| |
OLD | NEW |