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

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

Issue 10579008: Added test setup/teardown. (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') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file 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 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 /** 5 /**
6 * Returns a matcher which matches [Collection]s in which all elements 6 * Returns a matcher which matches [Collection]s in which all elements
7 * match the given [matcher]. 7 * match the given [matcher].
8 */ 8 */
9 Matcher everyElement(matcher) => new _EveryElement(wrapMatcher(matcher)); 9 Matcher everyElement(matcher) => new _EveryElement(wrapMatcher(matcher));
10 10
(...skipping 25 matching lines...) Expand all
36 return item.some( (e) => _matcher.matches(e) ); 36 return item.some( (e) => _matcher.matches(e) );
37 } 37 }
38 38
39 Description describe(Description description) => 39 Description describe(Description description) =>
40 description.add('some element ').addDescriptionOf(_matcher); 40 description.add('some element ').addDescriptionOf(_matcher);
41 } 41 }
42 42
43 /** 43 /**
44 * Returns a matcher which matches [Iterable]s that have the same 44 * Returns a matcher which matches [Iterable]s that have the same
45 * length and the same elements as [expected], and in the same order. 45 * length and the same elements as [expected], and in the same order.
46 * This is equivalent to equals but does not recurse.
46 */ 47 */
48
47 Matcher orderedEquals(Iterable expected) => new _OrderedEquals(expected); 49 Matcher orderedEquals(Iterable expected) => new _OrderedEquals(expected);
48 50
49 class _OrderedEquals extends BaseMatcher { 51 class _OrderedEquals extends BaseMatcher {
50 Iterable _expected; 52 final Iterable _expected;
53 Matcher _matcher;
51 54
52 _OrderedEquals(this._expected); 55 _OrderedEquals(this._expected) {
53 56 _matcher = equals(_expected, 1);
54 String _test(item) {
55 return _compareIterables(_expected, item,
56 (expected, actual, location) => expected == actual? null : location);
57 } 57 }
58 58
59 bool matches(item) => (_test(item) == null); 59 bool matches(item) => (item is Iterable) && _matcher.matches(item);
60 60
61 Description describe(Description description) => 61 Description describe(Description description) =>
62 description.add('equals ').addDescriptionOf(_expected).add(' ordered'); 62 description.add('equals ').addDescriptionOf(_expected).add(' ordered');
63 63
64 Description describeMismatch(item, Description mismatchDescription) => 64 Description describeMismatch(item, Description mismatchDescription) {
65 mismatchDescription.add(_test(item)); 65 if (item is !Iterable) {
66 return mismatchDescription.add('not an Iterable');
67 } else {
68 return _matcher.describeMismatch(item, mismatchDescription);
69 }
70 }
66 } 71 }
67
68 /** 72 /**
69 * Returns a matcher which matches [Iterable]s that have the same 73 * Returns a matcher which matches [Iterable]s that have the same
70 * length and the same elements as [expected], but not necessarily in 74 * length and the same elements as [expected], but not necessarily in
71 * the same order. Note that this is O(n^2) so should only be used on 75 * the same order. Note that this is O(n^2) so should only be used on
72 * small objects. 76 * small objects.
73 */ 77 */
74 Matcher unorderedEquals(Iterable expected) => 78 Matcher unorderedEquals(Iterable expected) =>
75 new _UnorderedEquals(expected); 79 new _UnorderedEquals(expected);
76 80
77 class _UnorderedEquals extends BaseMatcher { 81 class _UnorderedEquals extends BaseMatcher {
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
117 for (var actualElement in item) { 121 for (var actualElement in item) {
118 if (!matched[actualPosition]) { 122 if (!matched[actualPosition]) {
119 if (expectedElement == actualElement) { 123 if (expectedElement == actualElement) {
120 matched[actualPosition] = gotMatch = true; 124 matched[actualPosition] = gotMatch = true;
121 break; 125 break;
122 } 126 }
123 } 127 }
124 ++actualPosition; 128 ++actualPosition;
125 } 129 }
126 if (!gotMatch) { 130 if (!gotMatch) {
127 return 'has no match for element ${expectedElement} ' 131 Description reason = new StringDescription();
128 'at position ${expectedPosition}'; 132 reason.add('has no match for element ').
133 addDescriptionOf(expectedElement).
134 add(' at position ${expectedPosition}');
135 return reason.toString();
129 } 136 }
130 ++expectedPosition; 137 ++expectedPosition;
131 } 138 }
132 return null; 139 return null;
133 } 140 }
134 141
135 bool matches(item) => (_test(item) == null); 142 bool matches(item) => (_test(item) == null);
136 143
137 Description describe(Description description) => 144 Description describe(Description description) =>
138 description.add('equals ').addDescriptionOf(_expected).add(' unordered'); 145 description.add('equals ').addDescriptionOf(_expected).add(' unordered');
139 146
140 Description describeMismatch(item, Description mismatchDescription) => 147 Description describeMismatch(item, Description mismatchDescription) =>
141 mismatchDescription.add(_test(item)); 148 mismatchDescription.add(_test(item));
142 } 149 }
143 150
144 /** 151 /**
145 * Collection matchers match against [Collection]s. We add this intermediate 152 * Collection matchers match against [Collection]s. We add this intermediate
146 * class to give better mismatch error messages than the base Matcher class. 153 * class to give better mismatch error messages than the base Matcher class.
147 */ 154 */
148 155 /* abstract */ class _CollectionMatcher extends BaseMatcher {
149 /*abstract*/ class _CollectionMatcher extends BaseMatcher {
150 const _CollectionMatcher(); 156 const _CollectionMatcher();
151 Description describeMismatch(item, Description mismatchDescription) { 157 Description describeMismatch(item, Description mismatchDescription) {
152 if (item is !Collection) { 158 if (item is !Collection) {
153 return mismatchDescription. 159 return mismatchDescription.
154 addDescriptionOf(item). 160 addDescriptionOf(item).
155 add(' not a collection'); 161 add(' not a collection');
156 } else { 162 } else {
157 return super.describeMismatch(item, mismatchDescription); 163 return super.describeMismatch(item, mismatchDescription);
158 } 164 }
159 } 165 }
160 } 166 }
OLDNEW
« no previous file with comments | « no previous file | lib/unittest/core_matchers.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698