| 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 bool hasGetter(classMirror, getterName) { | 52 if (!classMirror.getters.containsKey(symbol)) { |
| 53 var candidate = classMirror.declarations[getterName]; | |
| 54 return candidate != null && | |
| 55 candidate is MethodMirror && | |
| 56 candidate.isGetter; | |
| 57 } | |
| 58 if (!hasGetter(classMirror, symbol)) { | |
| 59 addStateInfo(matchState, {'reason': 'has no property named "$_name"'}); | 53 addStateInfo(matchState, {'reason': 'has no property named "$_name"'}); |
| 60 return false; | 54 return false; |
| 61 } | 55 } |
| 62 if (_matcher == null) return true; | 56 if (_matcher == null) return true; |
| 63 var result = mirror.getField(symbol); | 57 var result = mirror.getField(symbol); |
| 64 var resultMatches = _matcher.matches(result.reflectee, matchState); | 58 var resultMatches = _matcher.matches(result.reflectee, matchState); |
| 65 if (!resultMatches) { | 59 if (!resultMatches) { |
| 66 addStateInfo(matchState, {'value': result.reflectee}); | 60 addStateInfo(matchState, {'value': result.reflectee}); |
| 67 } | 61 } |
| 68 return resultMatches; | 62 return resultMatches; |
| (...skipping 18 matching lines...) Expand all Loading... |
| 87 var innerDescription = new StringDescription(); | 81 var innerDescription = new StringDescription(); |
| 88 _matcher.describeMismatch(matchState['value'], innerDescription, | 82 _matcher.describeMismatch(matchState['value'], innerDescription, |
| 89 matchState['state'], verbose); | 83 matchState['state'], verbose); |
| 90 if (innerDescription.length > 0) { | 84 if (innerDescription.length > 0) { |
| 91 mismatchDescription.add(' which ').add(innerDescription.toString()); | 85 mismatchDescription.add(' which ').add(innerDescription.toString()); |
| 92 } | 86 } |
| 93 } | 87 } |
| 94 return mismatchDescription; | 88 return mismatchDescription; |
| 95 } | 89 } |
| 96 } | 90 } |
| OLD | NEW |