OLD | NEW |
1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2014, 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 @MirrorsUsed(targets: "Foo") | 5 @MirrorsUsed(targets: "Foo") |
6 import "dart:mirrors"; | 6 import "dart:mirrors"; |
7 | 7 |
8 import 'package:expect/expect.dart'; | 8 import 'package:expect/expect.dart'; |
9 | 9 |
10 class ParameterAnnotation { | 10 class ParameterAnnotation { |
11 final String value; | 11 final String value; |
12 const ParameterAnnotation(this.value); | 12 const ParameterAnnotation(this.value); |
13 } | 13 } |
14 | 14 |
15 class Foo { | 15 class Foo { |
16 Foo(@ParameterAnnotation("vogel") p) {} | 16 Foo(@ParameterAnnotation("vogel") p) {} |
17 Foo.named(@ParameterAnnotation("hamster") p) {} | 17 Foo.named(@ParameterAnnotation("hamster") p) {} |
18 Foo.named2(@ParameterAnnotation("hamster") p, | 18 Foo.named2( |
19 @ParameterAnnotation("wurm") p2) {} | 19 @ParameterAnnotation("hamster") p, @ParameterAnnotation("wurm") p2) {} |
20 | 20 |
21 f1(@ParameterAnnotation("hest") p) {} | 21 f1(@ParameterAnnotation("hest") p) {} |
22 f2(@ParameterAnnotation("hest") @ParameterAnnotation("fisk") p) {} | 22 f2(@ParameterAnnotation("hest") @ParameterAnnotation("fisk") p) {} |
23 f3(a, @ParameterAnnotation("fugl") p) {} | 23 f3(a, @ParameterAnnotation("fugl") p) {} |
24 f4(@ParameterAnnotation("fisk") a, | 24 f4(@ParameterAnnotation("fisk") a, {@ParameterAnnotation("hval") p}) {} |
25 {@ParameterAnnotation("hval") p}) {} | 25 f5(@ParameterAnnotation("fisk") a, [@ParameterAnnotation("hval") p]) {} |
26 f5(@ParameterAnnotation("fisk") a, | 26 f6({@ParameterAnnotation("fisk") z, @ParameterAnnotation("hval") p}) {} |
27 [@ParameterAnnotation("hval") p]) {} | |
28 f6({@ParameterAnnotation("fisk") z, | |
29 @ParameterAnnotation("hval") p}) {} | |
30 | 27 |
31 set s1(@ParameterAnnotation("cheval") p) {} | 28 set s1(@ParameterAnnotation("cheval") p) {} |
32 } | 29 } |
33 | 30 |
34 expectAnnotations(Type type, Symbol method, int parameterIndex, | 31 expectAnnotations( |
35 List<String> expectedValues) { | 32 Type type, Symbol method, int parameterIndex, List<String> expectedValues) { |
36 MethodMirror mirror = reflectClass(type).declarations[method]; | 33 MethodMirror mirror = reflectClass(type).declarations[method]; |
37 ParameterMirror parameter = mirror.parameters[parameterIndex]; | 34 ParameterMirror parameter = mirror.parameters[parameterIndex]; |
38 List<InstanceMirror> annotations = parameter.metadata; | 35 List<InstanceMirror> annotations = parameter.metadata; |
39 Expect.equals(annotations.length, expectedValues.length, | 36 Expect.equals(annotations.length, expectedValues.length, |
40 "wrong number of parameter annotations"); | 37 "wrong number of parameter annotations"); |
41 for (int i = 0; i < annotations.length; i++) { | 38 for (int i = 0; i < annotations.length; i++) { |
42 Expect.equals(expectedValues[i], annotations[i].reflectee.value, | 39 Expect.equals( |
43 "annotation #$i of parameter #$parameterIndex " | 40 expectedValues[i], |
44 "of $type.$method."); | 41 annotations[i].reflectee.value, |
| 42 "annotation #$i of parameter #$parameterIndex " |
| 43 "of $type.$method."); |
45 } | 44 } |
46 } | 45 } |
47 | 46 |
48 main() { | 47 main() { |
49 expectAnnotations(Foo, #Foo, 0, ["vogel"]); | 48 expectAnnotations(Foo, #Foo, 0, ["vogel"]); |
50 expectAnnotations(Foo, #Foo.named, 0, ["hamster"]); | 49 expectAnnotations(Foo, #Foo.named, 0, ["hamster"]); |
51 expectAnnotations(Foo, #Foo.named2, 0, ["hamster"]); | 50 expectAnnotations(Foo, #Foo.named2, 0, ["hamster"]); |
52 expectAnnotations(Foo, #Foo.named2, 1, ["wurm"]); | 51 expectAnnotations(Foo, #Foo.named2, 1, ["wurm"]); |
53 | 52 |
54 expectAnnotations(Foo, #f1, 0, ["hest"]); | 53 expectAnnotations(Foo, #f1, 0, ["hest"]); |
55 expectAnnotations(Foo, #f2, 0, ["hest", "fisk"]); | 54 expectAnnotations(Foo, #f2, 0, ["hest", "fisk"]); |
56 expectAnnotations(Foo, #f3, 0, []); | 55 expectAnnotations(Foo, #f3, 0, []); |
57 expectAnnotations(Foo, #f3, 1, ["fugl"]); | 56 expectAnnotations(Foo, #f3, 1, ["fugl"]); |
58 expectAnnotations(Foo, #f4, 0, ["fisk"]); | 57 expectAnnotations(Foo, #f4, 0, ["fisk"]); |
59 expectAnnotations(Foo, #f4, 1, ["hval"]); | 58 expectAnnotations(Foo, #f4, 1, ["hval"]); |
60 expectAnnotations(Foo, #f5, 0, ["fisk"]); | 59 expectAnnotations(Foo, #f5, 0, ["fisk"]); |
61 expectAnnotations(Foo, #f5, 1, ["hval"]); | 60 expectAnnotations(Foo, #f5, 1, ["hval"]); |
62 expectAnnotations(Foo, #f6, 0, ["fisk"]); | 61 expectAnnotations(Foo, #f6, 0, ["fisk"]); |
63 expectAnnotations(Foo, #f6, 1, ["hval"]); | 62 expectAnnotations(Foo, #f6, 1, ["hval"]); |
64 | 63 |
65 expectAnnotations(Foo, const Symbol('s1='), 0, ["cheval"]); | 64 expectAnnotations(Foo, const Symbol('s1='), 0, ["cheval"]); |
66 } | 65 } |
OLD | NEW |