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 * The mirror matchers library provides some additional matchers that | 5 /// `unittest.mirror_matchers` has been moved to the `matcher` package. |
6 * make use of dart:mirrors. | 6 /// |
7 * | 7 /// Add `matcher` to your `pubspec.yaml` file and import it via |
8 * ## Installing ## | 8 /// `import 'package:matcher/mirror_matchers.dart';` |
9 * | 9 @deprecated |
10 * Use [pub][] to install this package. Add the following to your `pubspec.yaml` | |
11 * file. | |
12 * | |
13 * dependencies: | |
14 * unittest: any | |
15 * | |
16 * Then run `pub install`. | |
17 * | |
18 * Import this into your Dart code with: | |
19 * | |
20 * import 'package:unittest/mirror_matchers.dart'; | |
21 * | |
22 * For more information, see the [unittest package on pub.dartlang.org]. | |
23 * (http://pub.dartlang.org/packages/unittest). | |
24 * | |
25 * [pub]: http://pub.dartlang.org | |
26 * [pkg]: http://pub.dartlang.org/packages/mirror_matchers | |
27 */ | |
28 library unittest.mirror_matchers; | 10 library unittest.mirror_matchers; |
29 | 11 |
30 import 'dart:mirrors'; | 12 export 'package:matcher/mirror_matchers.dart'; |
31 | |
32 import 'matcher.dart'; | |
33 | |
34 /** | |
35 * Returns a matcher that checks if a class instance has a property | |
36 * with name [name], and optionally, if that property in turn satisfies | |
37 * a [matcher]. | |
38 */ | |
39 Matcher hasProperty(String name, [matcher]) => | |
40 new _HasProperty(name, matcher == null ? null : wrapMatcher(matcher)); | |
41 | |
42 class _HasProperty extends Matcher { | |
43 final String _name; | |
44 final Matcher _matcher; | |
45 | |
46 const _HasProperty(this._name, [this._matcher]); | |
47 | |
48 bool matches(item, Map matchState) { | |
49 var mirror = reflect(item); | |
50 var classMirror = mirror.type; | |
51 var symbol = new Symbol(_name); | |
52 var candidate = classMirror.declarations[symbol]; | |
53 if (candidate == null) { | |
54 addStateInfo(matchState, {'reason': 'has no property named "$_name"'}); | |
55 return false; | |
56 } | |
57 bool isInstanceField = candidate is VariableMirror && !candidate.isStatic; | |
58 bool isInstanceGetter = | |
59 candidate is MethodMirror && candidate.isGetter && !candidate.isStatic; | |
60 if (!(isInstanceField || isInstanceGetter)) { | |
61 addStateInfo(matchState, {'reason': | |
62 'has a member named "$_name", but it is not an instance property'}); | |
63 return false; | |
64 } | |
65 if (_matcher == null) return true; | |
66 var result = mirror.getField(symbol); | |
67 var resultMatches = _matcher.matches(result.reflectee, matchState); | |
68 if (!resultMatches) { | |
69 addStateInfo(matchState, {'value': result.reflectee}); | |
70 } | |
71 return resultMatches; | |
72 } | |
73 | |
74 Description describe(Description description) { | |
75 description.add('has property "$_name"'); | |
76 if (_matcher != null) { | |
77 description.add(' which matches ').addDescriptionOf(_matcher); | |
78 } | |
79 return description; | |
80 } | |
81 | |
82 Description describeMismatch(item, Description mismatchDescription, | |
83 Map matchState, bool verbose) { | |
84 var reason = matchState == null ? null : matchState['reason']; | |
85 if (reason != null) { | |
86 mismatchDescription.add(reason); | |
87 } else { | |
88 mismatchDescription.add('has property "$_name" with value '). | |
89 addDescriptionOf(matchState['value']); | |
90 var innerDescription = new StringDescription(); | |
91 _matcher.describeMismatch(matchState['value'], innerDescription, | |
92 matchState['state'], verbose); | |
93 if (innerDescription.length > 0) { | |
94 mismatchDescription.add(' which ').add(innerDescription.toString()); | |
95 } | |
96 } | |
97 return mismatchDescription; | |
98 } | |
99 } | |
OLD | NEW |