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 * The mirror matchers library provides some additional matchers that |
6 * make use of dart:mirrors. | 6 * make use of dart:mirrors. |
7 * | 7 * |
8 * ## Installing ## | 8 * ## Installing ## |
9 * | 9 * |
10 * Use [pub][] to install this package. Add the following to your `pubspec.yaml` | 10 * Use [pub][] to install this package. Add the following to your `pubspec.yaml` |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
42 class _HasProperty extends Matcher { | 42 class _HasProperty extends Matcher { |
43 final String _name; | 43 final String _name; |
44 final Matcher _matcher; | 44 final Matcher _matcher; |
45 | 45 |
46 const _HasProperty(this._name, [this._matcher]); | 46 const _HasProperty(this._name, [this._matcher]); |
47 | 47 |
48 bool matches(item, Map matchState) { | 48 bool matches(item, Map matchState) { |
49 var mirror = reflect(item); | 49 var mirror = reflect(item); |
50 var classMirror = mirror.type; | 50 var classMirror = mirror.type; |
51 var symbol = new Symbol(_name); | 51 var symbol = new Symbol(_name); |
52 if (!classMirror.getters.containsKey(symbol)) { | 52 bool hasGetter(classMirror, getterName) { |
| 53 var candidate = classMirror.declarations[getterName]; |
| 54 return candidate != null && |
| 55 candidate is MethodMirror && |
| 56 candidate.isGetter; |
| 57 } |
| 58 if (!hasGetter(classMirror, symbol)) { |
53 addStateInfo(matchState, {'reason': 'has no property named "$_name"'}); | 59 addStateInfo(matchState, {'reason': 'has no property named "$_name"'}); |
54 return false; | 60 return false; |
55 } | 61 } |
56 if (_matcher == null) return true; | 62 if (_matcher == null) return true; |
57 var result = mirror.getField(symbol); | 63 var result = mirror.getField(symbol); |
58 var resultMatches = _matcher.matches(result.reflectee, matchState); | 64 var resultMatches = _matcher.matches(result.reflectee, matchState); |
59 if (!resultMatches) { | 65 if (!resultMatches) { |
60 addStateInfo(matchState, {'value': result.reflectee}); | 66 addStateInfo(matchState, {'value': result.reflectee}); |
61 } | 67 } |
62 return resultMatches; | 68 return resultMatches; |
(...skipping 18 matching lines...) Expand all Loading... |
81 var innerDescription = new StringDescription(); | 87 var innerDescription = new StringDescription(); |
82 _matcher.describeMismatch(matchState['value'], innerDescription, | 88 _matcher.describeMismatch(matchState['value'], innerDescription, |
83 matchState['state'], verbose); | 89 matchState['state'], verbose); |
84 if (innerDescription.length > 0) { | 90 if (innerDescription.length > 0) { |
85 mismatchDescription.add(' which ').add(innerDescription.toString()); | 91 mismatchDescription.add(' which ').add(innerDescription.toString()); |
86 } | 92 } |
87 } | 93 } |
88 return mismatchDescription; | 94 return mismatchDescription; |
89 } | 95 } |
90 } | 96 } |
OLD | NEW |