OLD | NEW |
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 library matcher.core_matchers; | 5 library matcher.core_matchers; |
6 | 6 |
7 import 'dart:async'; | 7 import 'dart:async'; |
8 | 8 |
9 import 'description.dart'; | 9 import 'description.dart'; |
10 import 'expect.dart'; | 10 import 'expect.dart'; |
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
78 } | 78 } |
79 | 79 |
80 /// Returns a matcher that matches if the value is structurally equal to | 80 /// Returns a matcher that matches if the value is structurally equal to |
81 /// [expected]. | 81 /// [expected]. |
82 /// | 82 /// |
83 /// If [expected] is a [Matcher], then it matches using that. Otherwise it tests | 83 /// If [expected] is a [Matcher], then it matches using that. Otherwise it tests |
84 /// for equality using `==` on the expected value. | 84 /// for equality using `==` on the expected value. |
85 /// | 85 /// |
86 /// For [Iterable]s and [Map]s, this will recursively match the elements. To | 86 /// For [Iterable]s and [Map]s, this will recursively match the elements. To |
87 /// handle cyclic structures a recursion depth [limit] can be provided. The | 87 /// handle cyclic structures a recursion depth [limit] can be provided. The |
88 /// default limit is 100. | 88 /// default limit is 100. [Set]s will be compared order-independently. |
89 Matcher equals(expected, [int limit=100]) => | 89 Matcher equals(expected, [int limit=100]) => |
90 expected is String | 90 expected is String |
91 ? new _StringEqualsMatcher(expected) | 91 ? new _StringEqualsMatcher(expected) |
92 : new _DeepMatcher(expected, limit); | 92 : new _DeepMatcher(expected, limit); |
93 | 93 |
94 class _DeepMatcher extends Matcher { | 94 class _DeepMatcher extends Matcher { |
95 final _expected; | 95 final _expected; |
96 final int _limit; | 96 final int _limit; |
97 var count; | 97 var count; |
98 | 98 |
(...skipping 18 matching lines...) Expand all Loading... |
117 if (!expectedNext) return ['longer than expected', newLocation]; | 117 if (!expectedNext) return ['longer than expected', newLocation]; |
118 if (!actualNext) return ['shorter than expected', newLocation]; | 118 if (!actualNext) return ['shorter than expected', newLocation]; |
119 | 119 |
120 // Match the elements. | 120 // Match the elements. |
121 var rp = matcher(expectedIterator.current, actualIterator.current, | 121 var rp = matcher(expectedIterator.current, actualIterator.current, |
122 newLocation, depth); | 122 newLocation, depth); |
123 if (rp != null) return rp; | 123 if (rp != null) return rp; |
124 } | 124 } |
125 } | 125 } |
126 | 126 |
| 127 List _compareSets(Set expected, actual, matcher, depth, location) { |
| 128 if (actual is! Iterable) return ['is not Iterable', location]; |
| 129 actual = actual.toSet(); |
| 130 |
| 131 for (var expectedElement in expected) { |
| 132 if (actual.every((actualElement) => |
| 133 matcher(expectedElement, actualElement, location, depth) != null)) { |
| 134 return ['does not contain $expectedElement', location]; |
| 135 } |
| 136 } |
| 137 |
| 138 if (actual.length > expected.length) { |
| 139 return ['larger than expected', location]; |
| 140 } else if (actual.length < expected.length) { |
| 141 return ['smaller than expected', location]; |
| 142 } else { |
| 143 return null; |
| 144 } |
| 145 } |
| 146 |
127 List _recursiveMatch(expected, actual, String location, int depth) { | 147 List _recursiveMatch(expected, actual, String location, int depth) { |
128 // If the expected value is a matcher, try to match it. | 148 // If the expected value is a matcher, try to match it. |
129 if (expected is Matcher) { | 149 if (expected is Matcher) { |
130 var matchState = {}; | 150 var matchState = {}; |
131 if (expected.matches(actual, matchState)) return null; | 151 if (expected.matches(actual, matchState)) return null; |
132 | 152 |
133 var description = new StringDescription(); | 153 var description = new StringDescription(); |
134 expected.describe(description); | 154 expected.describe(description); |
135 return ['does not match $description', location]; | 155 return ['does not match $description', location]; |
136 } else { | 156 } else { |
137 // Otherwise, test for equality. | 157 // Otherwise, test for equality. |
138 try { | 158 try { |
139 if (expected == actual) return null; | 159 if (expected == actual) return null; |
140 } catch (e, s) { | 160 } catch (e, s) { |
141 // TODO(gram): Add a test for this case. | 161 // TODO(gram): Add a test for this case. |
142 return ['== threw "$e"', location]; | 162 return ['== threw "$e"', location]; |
143 } | 163 } |
144 } | 164 } |
145 | 165 |
146 if (depth > _limit) return ['recursion depth limit exceeded', location]; | 166 if (depth > _limit) return ['recursion depth limit exceeded', location]; |
147 | 167 |
148 // If _limit is 1 we can only recurse one level into object. | 168 // If _limit is 1 we can only recurse one level into object. |
149 bool canRecurse = depth == 0 || _limit > 1; | 169 if (depth == 0 || _limit > 1) { |
| 170 if (expected is Set) { |
| 171 return _compareSets(expected, actual, _recursiveMatch, depth + 1, |
| 172 location); |
| 173 } else if (expected is Iterable) { |
| 174 return _compareIterables(expected, actual, _recursiveMatch, depth + 1, |
| 175 location); |
| 176 } else if (expected is Map) { |
| 177 if (actual is! Map) return ['expected a map', location]; |
150 | 178 |
151 if (expected is Iterable && canRecurse) { | 179 var err = (expected.length == actual.length) ? '' : |
152 return _compareIterables(expected, actual, _recursiveMatch, depth + 1, | 180 'has different length and '; |
153 location); | 181 for (var key in expected.keys) { |
154 } | 182 if (!actual.containsKey(key)) { |
| 183 return ["${err}is missing map key '$key'", location]; |
| 184 } |
| 185 } |
155 | 186 |
156 if (expected is Map && canRecurse) { | 187 for (var key in actual.keys) { |
157 if (actual is! Map) return ['expected a map', location]; | 188 if (!expected.containsKey(key)) { |
| 189 return ["${err}has extra map key '$key'", location]; |
| 190 } |
| 191 } |
158 | 192 |
159 var err = (expected.length == actual.length) ? '' : | 193 for (var key in expected.keys) { |
160 'has different length and '; | 194 var rp = _recursiveMatch(expected[key], actual[key], |
161 for (var key in expected.keys) { | 195 "${location}['${key}']", depth + 1); |
162 if (!actual.containsKey(key)) { | 196 if (rp != null) return rp; |
163 return ["${err}is missing map key '$key'", location]; | |
164 } | 197 } |
| 198 |
| 199 return null; |
165 } | 200 } |
166 | |
167 for (var key in actual.keys) { | |
168 if (!expected.containsKey(key)) { | |
169 return ["${err}has extra map key '$key'", location]; | |
170 } | |
171 } | |
172 | |
173 for (var key in expected.keys) { | |
174 var rp = _recursiveMatch(expected[key], actual[key], | |
175 "${location}['${key}']", depth + 1); | |
176 if (rp != null) return rp; | |
177 } | |
178 | |
179 return null; | |
180 } | 201 } |
181 | 202 |
182 var description = new StringDescription(); | 203 var description = new StringDescription(); |
183 | 204 |
184 // If we have recursed, show the expected value too; if not, expect() will | 205 // If we have recursed, show the expected value too; if not, expect() will |
185 // show it for us. | 206 // show it for us. |
186 if (depth > 0) { | 207 if (depth > 0) { |
187 description.add('was '). | 208 description.add('was '). |
188 addDescriptionOf(actual). | 209 addDescriptionOf(actual). |
189 add(' instead of '). | 210 add(' instead of '). |
(...skipping 509 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
699 addDescriptionOf(matchState['feature']); | 720 addDescriptionOf(matchState['feature']); |
700 var innerDescription = new StringDescription(); | 721 var innerDescription = new StringDescription(); |
701 _matcher.describeMismatch(matchState['feature'], innerDescription, | 722 _matcher.describeMismatch(matchState['feature'], innerDescription, |
702 matchState['state'], verbose); | 723 matchState['state'], verbose); |
703 if (innerDescription.length > 0) { | 724 if (innerDescription.length > 0) { |
704 mismatchDescription.add(' which ').add(innerDescription.toString()); | 725 mismatchDescription.add(' which ').add(innerDescription.toString()); |
705 } | 726 } |
706 return mismatchDescription; | 727 return mismatchDescription; |
707 } | 728 } |
708 } | 729 } |
OLD | NEW |