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