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

Side by Side Diff: lib/unittest/collection_matchers.dart

Issue 10441104: New expectation functions plus convert old tests to use these. (Closed) Base URL: http://dart.googlecode.com/svn/branches/bleeding_edge/dart/
Patch Set: Created 8 years, 6 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 | lib/unittest/core_matchers.dart » ('j') | lib/unittest/core_matchers.dart » ('J')
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(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 // Collection matchers match against a collection. We add this intermediate
Bob Nystrom 2012/05/30 23:23:51 Make this a doc comment for the class: /** * Col
gram 2012/06/01 17:33:15 Done.
6 // class to give better mismatch error messages than the base Matcher class.
7
8 class _CollectionMatcher extends Matcher {
9 IDescription describeMismatch(item, IDescription mismatchDescription) {
10 if (!(item is Collection)) {
Bob Nystrom 2012/05/30 23:23:51 if (item is !Collection)
gram 2012/06/01 17:33:15 Done.
11 return mismatchDescription.
12 appendDescriptionOf(item).
13 append(' not a collection');
14 } else {
15 return super.describeMismatch(item, mismatchDescription);
16 }
17 }
18 }
19 //-----------------------------------------------------------
Bob Nystrom 2012/05/30 23:23:51 I don't think we need a comments for structure. Wh
gram 2012/06/01 17:33:15 Done.
20 /**
21 * This returns a matcher that matches empty collections.
Bob Nystrom 2012/05/30 23:23:51 "This returns" -> "Returns" Here and elsewhere. W
gram 2012/06/01 17:33:15 Done.
22 */
23 IMatcher emptyCollection() => new _EmptyCollection();
Bob Nystrom 2012/05/30 23:23:51 Make this a variable so you don't need the (): fi
gram 2012/06/01 17:33:15 Done.
24
25 class _EmptyCollection extends _CollectionMatcher {
26 bool matches(collection) {
Bob Nystrom 2012/05/30 23:23:51 Annotate this to type Collection?
gram 2012/06/01 17:33:15 None of the matches() methods are typed, as they a
Bob Nystrom 2012/06/01 18:22:22 That makes sense.
gram 2012/06/01 22:21:59 Done.
27 try {
28 return collection.isEmpty();
29 } catch (var e) {
Bob Nystrom 2012/05/30 23:23:51 Why are you swallowing exceptions here?
gram 2012/06/01 17:33:15 The idea is that you can pass anything in, and tha
Bob Nystrom 2012/06/01 18:22:22 Is that helpful to users? If I do: expect(foo, is
gram 2012/06/01 22:21:59 I think we're both partly right. I do think that e
Bob Nystrom 2012/06/01 22:29:00 Perfect. I love it.
30 return false;
31 }
32 }
33
34 IDescription describe(IDescription description) =>
35 description.append('be empty');
36 }
37
38 //-----------------------------------------------------------
39 /**
40 * This returns a matcher that matches collection that contains [obj].
Bob Nystrom 2012/05/30 23:23:51 "collection" -> "a collection"
gram 2012/06/01 17:33:15 Done.
41 */
42 IMatcher contains(obj) => new _CollectionContains(obj);
43
44 class _CollectionContains extends _CollectionMatcher {
45
46 var _val;
Bob Nystrom 2012/05/30 23:23:51 "_val" -> "_value"
gram 2012/06/01 17:33:15 Done.
47
48 _CollectionContains(this._val);
49
50 bool matches(item) {
51 try {
52 return item.some( (e) => e == _val );
Bob Nystrom 2012/05/30 23:23:51 Space like: return item.some((e) => e == _val);
gram 2012/06/01 17:33:15 Done.
53 } catch (var e) {
54 return false;
55 }
56 }
57 IDescription describe(IDescription description) =>
Bob Nystrom 2012/05/30 23:23:51 Maybe a blank line before this one?
gram 2012/06/01 17:33:15 Done.
58 description.append('contains ').appendDescriptionOf(_val);
59 }
60
61 //-----------------------------------------------------------
62 /**
63 * This returns a matcher which matches collections in which all elements
64 * match the given [matcher].
65 */
66 IMatcher everyElement(matcher) => new _EveryElement(wrapMatcher(matcher));
67
68 class _EveryElement extends _CollectionMatcher {
69 Matcher _matcher;
70
71 _EveryElement(Matcher this._matcher);
Bob Nystrom 2012/05/30 23:23:51 We don't type annotate "this." constructor params
gram 2012/06/01 17:33:15 Done.
72
73 bool matches(item) {
74 try {
75 return item.every( (e) => _matcher.matches(e) );
Bob Nystrom 2012/05/30 23:23:51 No spaces around lambda fn.
gram 2012/06/01 17:33:15 Done.
76 } catch (var e) {
77 return false;
78 }
79 }
80 IDescription describe(IDescription description) =>
81 description.append('every element ').
82 appendDescriptionOf(_matcher);
83 }
84
85 //-----------------------------------------------------------
86 /**
87 * This returns a matcher which matches collections in which at least one
88 * element matches the given [matcher].
89 */
90 IMatcher someElement(matcher) => new _SomeElement(wrapMatcher(matcher));
91
92 class _SomeElement extends _CollectionMatcher {
93 Matcher _matcher;
94
95 _SomeElement(Matcher this._matcher);
96
97 bool matches(item) {
98 try {
99 return item.some( (e) => _matcher.matches(e) );
100 } catch (var e) {
101 return false;
102 }
103 }
104 IDescription describe(IDescription description) =>
105 description.append('some element ').
106 appendDescriptionOf(_matcher);
107 }
108
109 //-----------------------------------------------------------
110 /**
111 * This returns a matcher which matches collections that have the same
112 * length and the same elements as [expected], and in the same order.
113 */
114
115 IMatcher orderedEquals(expected) => new _OrderedEquals(new List.from(expected));
Bob Nystrom 2012/05/30 23:23:51 Why new List.from() here? Can "expected" be annot
gram 2012/06/01 17:33:15 Done.
gram 2012/06/01 17:33:15 We need at least one of the two to be indexable. I
Bob Nystrom 2012/06/01 18:22:22 Is this comment still accurate? It looks like the
116
117 class _OrderedEquals extends Matcher {
118 var _expected;
119
120 _OrderedEquals(this._expected);
121
122 bool matches(item) {
123 try {
124 List actual = new List.from(item);
Bob Nystrom 2012/05/30 23:23:51 "List" -> "var". We rarely annotate locals.
gram 2012/06/01 17:33:15 Done.
125 if (_expected.length == actual.length) {
Bob Nystrom 2012/05/30 23:23:51 It would be nice if this worked with any Iterable
gram 2012/06/01 17:33:15 Done.
126 for (int i = 0; i < _expected.length; i++) {
127 if (_expected[i] != actual[i]) {
128 return false;
129 }
130 }
131 return true;
132 }
133 } catch (var e) {
134 }
135 return false;
136 }
137
138 IDescription describe(IDescription description) =>
139 description.append('equals ').
140 appendDescriptionOf(_expected).append(' ordered');
141 }
142
143 //-----------------------------------------------------------
144 /**
145 * This returns a matcher which matches collections that have the same
146 * length and the same elements as [expected], but not necessarily in
147 * the same order.
148 */
149 IMatcher unorderedEquals(expected) =>
Bob Nystrom 2012/05/30 23:23:51 Can this be type annotated?
gram 2012/06/01 17:33:15 Done.
150 new _UnorderedEquals(new Set.from(expected));
Bob Nystrom 2012/05/30 23:23:51 Does this mean all items in expected must be hasha
gram 2012/06/01 17:33:15 I rewrote it so that isn't necessary. It is O(n^2)
151
152 class _UnorderedEquals extends Matcher {
153 var _expected;
154
155 _UnorderedEquals(this._expected);
156
157 bool matches(item) {
158 try {
159 Set items = new Set.from(item);
Bob Nystrom 2012/05/30 23:23:51 "Set" -> "var".
gram 2012/06/01 17:33:15 Done.
160 Set missing = new Set.from(_expected);
161 missing.removeAll(items);
162 Set extra = items;
163 extra.removeAll(_expected);
164 return missing.isEmpty() && extra.isEmpty();
165 } catch (var e) {
166 return false;
167 }
168 }
169
170 IDescription describe(IDescription description) =>
171 description.append('equals ').
172 appendDescriptionOf(_expected).append(' unordered');
173 }
174
OLDNEW
« no previous file with comments | « no previous file | lib/unittest/core_matchers.dart » ('j') | lib/unittest/core_matchers.dart » ('J')

Powered by Google App Engine
This is Rietveld 408576698