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 |
| 15 class MyClass { |
| 16 } |
| 17 |
| 18 checkMetadata(DeclarationMirror mirror, List expectedMetadata) { |
| 19 List metadata = mirror.metadata.map((m) => m.reflectee).toList(); |
| 20 if (metadata == null) { |
| 21 throw 'Null metadata on $mirror'; |
| 22 } |
| 23 int expectedLength = expectedMetadata.length; |
| 24 int actualLength = metadata.length; |
| 25 if (expectedLength != actualLength) { |
| 26 throw 'Expected length = $expectedLength, but got length = $actualLength.'; |
| 27 } |
| 28 for (int i = 0; i < expectedLength; i++) { |
| 29 if (metadata[i] != expectedMetadata[i]) { |
| 30 throw '${metadata[i]} is not "${expectedMetadata[i]}"' |
| 31 ' in $mirror at index $i'; |
| 32 } |
| 33 } |
| 34 print(metadata); |
| 35 } |
| 36 |
14 main() { | 37 main() { |
15 MirrorSystem mirrors = currentMirrorSystem(); | |
16 LibraryMirror library = | |
17 mirrors.findLibrary(const Symbol('test.metadata_test')).first; | |
18 List metadata = library.metadata.map((m) => m.reflectee).toList(); | |
19 if (metadata.length != 2) { | |
20 throw 'Expected two pieces of metadata on library'; | |
21 } | |
22 if (!metadata.contains(fisk)) { | |
23 throw '$metadata does not contain "$fisk"'; | |
24 } | |
25 if (!metadata.contains(symbol)) { | |
26 throw '$metadata does not contain "$symbol"'; | |
27 } | |
28 if (MirrorSystem.getName(symbol) != 'fisk') { | 38 if (MirrorSystem.getName(symbol) != 'fisk') { |
29 // This happened in dart2js due to how early library metadata is | 39 // This happened in dart2js due to how early library metadata is |
30 // computed. | 40 // computed. |
31 throw 'Bad constant: $symbol'; | 41 throw 'Bad constant: $symbol'; |
32 } | 42 } |
33 print(metadata); | 43 |
| 44 MirrorSystem mirrors = currentMirrorSystem(); |
| 45 checkMetadata(mirrors.findLibrary(const Symbol('test.metadata_test')).first, |
| 46 [fisk, symbol]); |
| 47 checkMetadata(reflect(new MyClass()).type, [symbol, fisk]); |
34 } | 48 } |
OLD | NEW |