Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2012, 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 /** | |
| 6 * Returns a matcher that matches empty maps. | |
| 7 */ | |
| 8 IMatcher emptyMap() => new _EmptyMap(); | |
|
Bob Nystrom
2012/05/30 23:23:51
Can we just have a single isEmpty matcher? Why als
gram
2012/06/01 17:33:15
I combined the matchers for string, map and collec
Bob Nystrom
2012/06/01 18:22:22
Agreed. We do lots of other type tests in this lib
| |
| 9 | |
| 10 class _EmptyMap extends _MapMatcher { | |
| 11 bool matches(item) => item is Map && item.isEmpty(); | |
| 12 IDescription describe(IDescription description) => | |
| 13 description.append('empty'); | |
| 14 } | |
| 15 | |
| 16 /** | |
| 17 * Returns a matcher that matches maps containing the given [key] | |
| 18 */ | |
| 19 IMatcher mapContainsKey(key) => new _MapContainsKey(key); | |
| 20 | |
| 21 class _MapContainsKey extends _MapMatcher { | |
| 22 var _key; | |
| 23 | |
| 24 _MapContainsKey(this._key); | |
| 25 | |
| 26 bool matches(item) => item is Map && item.containsKey(_key); | |
|
Bob Nystrom
2012/05/30 23:23:51
Does it add value to test the type here, or should
gram
2012/06/01 17:33:15
Done.
| |
| 27 | |
| 28 IDescription describe(IDescription description) => | |
| 29 description.append('contains key ').appendDescriptionOf(_key); | |
| 30 } | |
| 31 | |
| 32 /** | |
| 33 * Returns a matcher which matches maps containing the given [value]. | |
| 34 */ | |
| 35 IMatcher mapContainsValue(value) => new _MapContainsValue(value); | |
| 36 | |
| 37 class _MapContainsValue extends _MapMatcher { | |
| 38 var _value; | |
| 39 | |
| 40 _MapContainsValue(this._value); | |
| 41 | |
| 42 bool matches(item) => item is Map && item.containsValue(_value); | |
| 43 IDescription describe(IDescription description) => | |
| 44 description.append('contains value ').appendDescriptionOf(_value); | |
| 45 } | |
| 46 | |
| 47 /** | |
| 48 * Returns a matcher which matches maps containing the key-value pair | |
| 49 * with [key] => [value]. | |
| 50 */ | |
| 51 IMatcher mapContains(key, value) => | |
| 52 new _MapContainsMapping(key, wrapMatcher(value)); | |
| 53 | |
| 54 class _MapContainsMapping extends _MapMatcher { | |
| 55 var _key; | |
| 56 IMatcher _valueMatcher; | |
| 57 | |
| 58 _MapContainsMapping(this._key, IMatcher this._valueMatcher); | |
| 59 | |
| 60 bool matches(item) => | |
| 61 item is Map && | |
| 62 item.containsKey(_key) && | |
| 63 _valueMatcher.matches(item[_key]); | |
| 64 | |
| 65 IDescription describe(IDescription description) => | |
|
Bob Nystrom
2012/05/30 23:23:51
I would use a full {} body for this since it's mul
gram
2012/06/01 17:33:15
Done.
| |
| 66 description.append('contains pair '). | |
| 67 appendDescriptionOf(_key). | |
| 68 append(' => '). | |
| 69 appendDescriptionOf(_valueMatcher); | |
| 70 | |
| 71 IDescription describeMismatch(item, IDescription mismatchDescription) { | |
| 72 if (item is Map) { | |
| 73 if (!item.containsKey(_key)) { | |
| 74 return mismatchDescription. | |
| 75 appendDescriptionOf(item). | |
|
Bob Nystrom
2012/05/30 23:23:51
Indent another 2.
gram
2012/06/01 17:33:15
Done.
| |
| 76 append("' doesn't contain key '"). | |
| 77 appendDescriptionOf(_key); | |
| 78 } else { | |
| 79 return mismatchDescription. | |
| 80 append(' contains key '). | |
|
Bob Nystrom
2012/05/30 23:23:51
Ditto.
gram
2012/06/01 17:33:15
Done.
| |
| 81 appendDescriptionOf(_key). | |
| 82 append(' but with value '); | |
| 83 _valueMatcher.describeMismatch(item[_key], mismatchDescription); | |
| 84 return mismatchDescription; | |
| 85 } | |
| 86 } else { | |
| 87 return super.describeMismatch(item, mismatchDescription); | |
| 88 } | |
| 89 } | |
| 90 } | |
| 91 | |
| 92 /** | |
| 93 * Returns a matcher which matches maps whose length matches | |
| 94 * the given [matcher]. This is different from hasLength(), which | |
| 95 * works on any types that have length properties. | |
| 96 */ | |
| 97 IMatcher mapLength(matcher) => new _MapLength(wrapMatcher(matcher)); | |
|
Bob Nystrom
2012/05/30 23:23:51
This feels unnecessary to me. Can we remove it?
gram
2012/06/01 17:33:15
Done.
| |
| 98 | |
| 99 class _MapLength extends _MapMatcher { | |
| 100 IMatcher _matcher; | |
| 101 | |
| 102 _MapLength(this._matcher); | |
| 103 | |
| 104 bool matches(item) => item is Map && _matcher.matches(item.length); | |
| 105 | |
| 106 IDescription describe(IDescription description) => | |
| 107 description.append('map length '). | |
| 108 appendDescriptionOf(_matcher); | |
| 109 } | |
| 110 | |
| 111 // Map matchers match against a Map. We add this intermediate | |
|
Bob Nystrom
2012/05/30 23:23:51
Make this a doc comment.
gram
2012/06/01 17:33:15
Removed.
| |
| 112 // class to give better mismatch error messages than the base Matcher class. | |
| 113 | |
| 114 class _MapMatcher extends Matcher { | |
| 115 IDescription describeMismatch(item, IDescription mismatchDescription) { | |
| 116 if (!(item is Map)) { | |
| 117 return mismatchDescription. | |
| 118 appendDescriptionOf(item). | |
| 119 append(' not a map'); | |
| 120 } else { | |
| 121 return super.describeMismatch(item, mismatchDescription); | |
| 122 } | |
| 123 } | |
| 124 } | |
| OLD | NEW |