Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(5)

Side by Side Diff: pkg/unittest/lib/mirror_matchers.dart

Issue 21085010: Added hasProperty matcher. (Closed) Base URL: http://dart.googlecode.com/svn/branches/bleeding_edge/dart/
Patch Set: Created 7 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « no previous file | pkg/unittest/lib/src/core_matchers.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
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
3 // BSD-style license that can be found in the LICENSE file.
4 /**
5 * The mirror matchers library provides some additional matchers that
6 * make use of dart:mirrors.
7 *
8 * ## Installing ##
9 *
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 mirror_matchers;
29
30 import 'dart:async';
31 import 'dart:mirrors';
32 import 'package:meta/meta.dart';
33
34 import 'matcher.dart';
35
36 /**
37 * Returns a matcher that checks if a class instance has a property
38 * with name [name], and optionally, if that property in turn satisfies
39 * a [matcher].
40 */
41 Matcher hasProperty(String name, [matcher]) =>
42 new _HasProperty(name, matcher == null ? null : wrapMatcher(matcher));
43
44 class _HasProperty extends Matcher {
45 final String _name;
46 final Matcher _matcher;
47
48 const _HasProperty(this._name, [this._matcher]);
49
50 bool matches(item, Map matchState) {
51 var mirror = reflect(item);
52 var classMirror = mirror.type;
53 var symbol = new Symbol(_name);
54 if (!classMirror.getters.containsKey(symbol)) {
55 addStateInfo(matchState, {'reason': 'has no property named "$_name"'});
56 return false;
57 }
58 if (_matcher == null) return true;
59 var result = mirror.getField(symbol);
60 var resultMatches = _matcher.matches(result.reflectee, matchState);
61 if (!resultMatches) {
62 addStateInfo(matchState, {'value': result.reflectee});
63 }
64 return resultMatches;
65 }
66
67 Description describe(Description description) {
68 description.add('has property "$_name"');
69 if (_matcher != null) {
70 description.add(' which matches ').addDescriptionOf(_matcher);
71 }
72 return description;
73 }
74
75 Description describeMismatch(item, Description mismatchDescription,
76 Map matchState, bool verbose) {
77 var reason = matchState == null ? null : matchState['reason'];
78 if (reason != null) {
79 mismatchDescription.add(reason);
80 } else {
81 mismatchDescription.add('has property "$_name" with value ').
82 addDescriptionOf(matchState['value']);
83 var innerDescription = new StringDescription();
84 _matcher.describeMismatch(matchState['value'], innerDescription,
85 matchState['state'], verbose);
86 if (innerDescription.length > 0) {
87 mismatchDescription.add(' which ').add(innerDescription.toString());
88 }
89 }
90 return mismatchDescription;
91 }
92 }
OLDNEW
« no previous file with comments | « no previous file | pkg/unittest/lib/src/core_matchers.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698