| 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 /// 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 library matcher.mirror_matchers; | 7 library matcher.mirror_matchers; |
| 8 | 8 |
| 9 import 'dart:mirrors'; | 9 import 'dart:mirrors'; |
| 10 | 10 |
| 11 import 'matcher.dart'; | 11 import 'matcher.dart'; |
| 12 | 12 |
| 13 /// Returns a matcher that checks if a class instance has a property | 13 /// Returns a matcher that checks if a class instance has a property |
| 14 /// with name [name], and optionally, if that property in turn satisfies | 14 /// with name [name], and optionally, if that property in turn satisfies |
| 15 /// a [matcher]. | 15 /// a [matcher]. |
| 16 Matcher hasProperty(String name, [matcher]) => | 16 Matcher hasProperty(String name, [matcher]) => |
| 17 new _HasProperty(name, matcher == null ? null : wrapMatcher(matcher)); | 17 new _HasProperty(name, matcher == null ? null : wrapMatcher(matcher)); |
| 18 | 18 |
| 19 class _HasProperty extends Matcher { | 19 class _HasProperty extends Matcher { |
| 20 final String _name; | 20 final String _name; |
| 21 final Matcher _matcher; | 21 final Matcher _matcher; |
| 22 | 22 |
| 23 const _HasProperty(this._name, [this._matcher]); | 23 const _HasProperty(this._name, [this._matcher]); |
| 24 | 24 |
| 25 bool matches(item, Map matchState) { | 25 bool matches(item, Map matchState) { |
| 26 var mirror = reflect(item); | 26 var mirror = reflect(item); |
| 27 var classMirror = mirror.type; | 27 var classMirror = mirror.type; |
| 28 var symbol = new Symbol(_name); | 28 var symbol = new Symbol(_name); |
| 29 var candidate = classMirror.declarations[symbol]; | 29 var candidate = classMirror.declarations[symbol]; |
| 30 if (candidate == null) { | 30 if (candidate == null) { |
| 31 addStateInfo(matchState, {'reason': 'has no property named "$_name"'}); | 31 addStateInfo(matchState, {'reason': 'has no property named "$_name"'}); |
| 32 return false; | 32 return false; |
| 33 } | 33 } |
| 34 bool isInstanceField = candidate is VariableMirror && !candidate.isStatic; | 34 bool isInstanceField = candidate is VariableMirror && !candidate.isStatic; |
| 35 bool isInstanceGetter = | 35 bool isInstanceGetter = |
| 36 candidate is MethodMirror && candidate.isGetter && !candidate.isStatic; | 36 candidate is MethodMirror && candidate.isGetter && !candidate.isStatic; |
| 37 if (!(isInstanceField || isInstanceGetter)) { | 37 if (!(isInstanceField || isInstanceGetter)) { |
| 38 addStateInfo(matchState, {'reason': | 38 addStateInfo(matchState, { |
| 39 'has a member named "$_name", but it is not an instance property'}); | 39 'reason': |
| 40 'has a member named "$_name", but it is not an instance property' |
| 41 }); |
| 40 return false; | 42 return false; |
| 41 } | 43 } |
| 42 if (_matcher == null) return true; | 44 if (_matcher == null) return true; |
| 43 var result = mirror.getField(symbol); | 45 var result = mirror.getField(symbol); |
| 44 var resultMatches = _matcher.matches(result.reflectee, matchState); | 46 var resultMatches = _matcher.matches(result.reflectee, matchState); |
| 45 if (!resultMatches) { | 47 if (!resultMatches) { |
| 46 addStateInfo(matchState, {'value': result.reflectee}); | 48 addStateInfo(matchState, {'value': result.reflectee}); |
| 47 } | 49 } |
| 48 return resultMatches; | 50 return resultMatches; |
| 49 } | 51 } |
| 50 | 52 |
| 51 Description describe(Description description) { | 53 Description describe(Description description) { |
| 52 description.add('has property "$_name"'); | 54 description.add('has property "$_name"'); |
| 53 if (_matcher != null) { | 55 if (_matcher != null) { |
| 54 description.add(' which matches ').addDescriptionOf(_matcher); | 56 description.add(' which matches ').addDescriptionOf(_matcher); |
| 55 } | 57 } |
| 56 return description; | 58 return description; |
| 57 } | 59 } |
| 58 | 60 |
| 59 Description describeMismatch(item, Description mismatchDescription, | 61 Description describeMismatch( |
| 60 Map matchState, bool verbose) { | 62 item, Description mismatchDescription, Map matchState, bool verbose) { |
| 61 var reason = matchState == null ? null : matchState['reason']; | 63 var reason = matchState == null ? null : matchState['reason']; |
| 62 if (reason != null) { | 64 if (reason != null) { |
| 63 mismatchDescription.add(reason); | 65 mismatchDescription.add(reason); |
| 64 } else { | 66 } else { |
| 65 mismatchDescription.add('has property "$_name" with value '). | 67 mismatchDescription |
| 66 addDescriptionOf(matchState['value']); | 68 .add('has property "$_name" with value ') |
| 69 .addDescriptionOf(matchState['value']); |
| 67 var innerDescription = new StringDescription(); | 70 var innerDescription = new StringDescription(); |
| 68 _matcher.describeMismatch(matchState['value'], innerDescription, | 71 _matcher.describeMismatch( |
| 69 matchState['state'], verbose); | 72 matchState['value'], innerDescription, matchState['state'], verbose); |
| 70 if (innerDescription.length > 0) { | 73 if (innerDescription.length > 0) { |
| 71 mismatchDescription.add(' which ').add(innerDescription.toString()); | 74 mismatchDescription.add(' which ').add(innerDescription.toString()); |
| 72 } | 75 } |
| 73 } | 76 } |
| 74 return mismatchDescription; | 77 return mismatchDescription; |
| 75 } | 78 } |
| 76 } | 79 } |
| OLD | NEW |