Index: lib/unittest/map_matchers.dart |
=================================================================== |
--- lib/unittest/map_matchers.dart (revision 0) |
+++ lib/unittest/map_matchers.dart (revision 0) |
@@ -0,0 +1,52 @@ |
+// Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
+// for details. All rights reserved. Use of this source code is governed by a |
+// BSD-style license that can be found in the LICENSE file. |
+ |
+/** |
+ * Returns a matcher which matches objects containing the given [value]. |
Bob Nystrom
2012/06/01 18:22:23
Might want to clarify that this is for maps, since
gram
2012/06/01 22:22:00
Done.
|
+ */ |
+IMatcher containsValue(value) => new _ContainsValue(value); |
+ |
+class _ContainsValue extends Matcher { |
+ final _value; |
+ |
+ const _ContainsValue(this._value); |
+ |
+ bool matches(item) => item.containsValue(_value); |
+ IDescription describe(IDescription description) => |
+ description.add('contains value ').addDescriptionOf(_value); |
+} |
+ |
+/** |
+ * Returns a matcher which matches maps containing the key-value pair |
+ * with [key] => [value]. |
+ */ |
+IMatcher containsPair(key, value) => |
+ new _ContainsMapping(key, wrapMatcher(value)); |
+ |
+class _ContainsMapping extends Matcher { |
+ final _key; |
+ final IMatcher _valueMatcher; |
+ |
+ const _ContainsMapping(this._key, IMatcher this._valueMatcher); |
+ |
+ bool matches(item) => |
+ item.containsKey(_key) && _valueMatcher.matches(item[_key]); |
+ |
+ IDescription describe(IDescription description) { |
+ return description.add('contains pair ').addDescriptionOf(_key). |
+ add(' => ').addDescriptionOf(_valueMatcher); |
+ } |
+ |
+ IDescription describeMismatch(item, IDescription mismatchDescription) { |
+ if (!item.containsKey(_key)) { |
+ return mismatchDescription.addDescriptionOf(item). |
+ add("' doesn't contain key '").addDescriptionOf(_key); |
+ } else { |
+ return mismatchDescription.add(' contains key ').addDescriptionOf(_key). |
+ add(' but with value '); |
+ _valueMatcher.describeMismatch(item[_key], mismatchDescription); |
+ return mismatchDescription; |
+ } |
+ } |
+} |