Chromium Code Reviews| Index: pkg/unittest/lib/src/core_matchers.dart |
| diff --git a/pkg/unittest/lib/src/core_matchers.dart b/pkg/unittest/lib/src/core_matchers.dart |
| index 86f51f36569c3f43c6b0da4750b477971657865d..71548720b4cbb9d8cd78cdf90842e5dc005ceb00 100644 |
| --- a/pkg/unittest/lib/src/core_matchers.dart |
| +++ b/pkg/unittest/lib/src/core_matchers.dart |
| @@ -99,9 +99,8 @@ class _DeepMatcher extends Matcher { |
| // Returns a pair (reason, location) |
| List _compareIterables(expected, actual, matcher, depth, location) { |
| - if (actual is! Iterable) { |
| - return ['is not Iterable', location]; |
| - } |
| + if (actual is! Iterable) return ['is not Iterable', location]; |
| + |
| var expectedIterator = expected.iterator; |
| var actualIterator = actual.iterator; |
| var index = 0; |
| @@ -109,11 +108,10 @@ class _DeepMatcher extends Matcher { |
| var newLocation = '${location}[${index}]'; |
| if (expectedIterator.moveNext()) { |
| if (actualIterator.moveNext()) { |
| - var rp = matcher(expectedIterator.current, |
| - actualIterator.current, newLocation, |
| - depth); |
| + var rp = matcher(expectedIterator.current, actualIterator.current, |
| + newLocation, depth); |
| if (rp != null) return rp; |
| - ++index; |
| + index++; |
| } else { |
| return ['shorter than expected', newLocation]; |
| } |
| @@ -123,82 +121,81 @@ class _DeepMatcher extends Matcher { |
| return null; |
| } |
|
Siggi Cherem (dart-lang)
2014/02/28 02:41:04
since you are already fixing style :), consider ch
Bob Nystrom
2014/02/28 17:31:24
Done, and then some.
|
| } |
| - return null; |
| } |
| List _recursiveMatch(expected, actual, String location, int depth) { |
| - String reason = null; |
| + // If the expected value is a matcher, try to match it. |
| + if (expected is Matcher) { |
| + var matchState = {}; |
| + if (expected.matches(actual, matchState)) return null; |
| + |
| + var description = new StringDescription(); |
| + expected.describe(description); |
| + return ['does not match $description', location]; |
| + } else { |
| + // Otherwise, test for equality. |
| + try { |
| + if (expected == actual) return null; |
| + } catch (e, s) { |
| + // TODO(gram): Add a test for this case. |
| + return ['== threw "$e"', location]; |
| + } |
| + } |
| + |
| + if (depth > _limit) { |
| + return ['recursion depth limit exceeded', location]; |
| + } |
| + |
| // If _limit is 1 we can only recurse one level into object. |
| bool canRecurse = depth == 0 || _limit > 1; |
| - bool equal; |
| - try { |
| - equal = (expected == actual); |
| - } catch (e, s) { |
| - // TODO(gram): Add a test for this case. |
| - reason = '== threw "$e"'; |
| - return [reason, location]; |
| + |
| + if (expected is Iterable && canRecurse) { |
| + return _compareIterables(expected, actual, _recursiveMatch, depth + 1, |
| + location); |
| } |
| - if (equal) { |
| - // Do nothing. |
| - } else if (depth > _limit) { |
| - reason = 'recursion depth limit exceeded'; |
| - } else { |
| - if (expected is Iterable && canRecurse) { |
| - List result = _compareIterables(expected, actual, |
| - _recursiveMatch, depth + 1, location); |
| - if (result != null) { |
| - reason = result[0]; |
| - location = result[1]; |
| - } |
| - } else if (expected is Map && canRecurse) { |
| - if (actual is! Map) { |
| - reason = 'expected a map'; |
| - } else { |
| - var err = (expected.length == actual.length) ? '' : |
| - 'has different length and '; |
| - for (var key in expected.keys) { |
| - if (!actual.containsKey(key)) { |
| - reason = '${err}is missing map key \'$key\''; |
| - break; |
| - } |
| - } |
| - if (reason == null) { |
| - for (var key in actual.keys) { |
| - if (!expected.containsKey(key)) { |
| - reason = '${err}has extra map key \'$key\''; |
| - break; |
| - } |
| - } |
| - if (reason == null) { |
| - for (var key in expected.keys) { |
| - var rp = _recursiveMatch(expected[key], actual[key], |
| - "${location}['${key}']", depth + 1); |
| - if (rp != null) { |
| - reason = rp[0]; |
| - location = rp[1]; |
| - break; |
| - } |
| - } |
| - } |
| - } |
| + |
| + if (expected is Map && canRecurse) { |
| + if (actual is! Map) { |
| + return ['expected a map', location]; |
|
Siggi Cherem (dart-lang)
2014/02/28 02:41:04
nit: this might fit the 1-line if:
if(...) retu
Bob Nystrom
2014/02/28 17:31:24
Done.
|
| + } |
| + |
| + var err = (expected.length == actual.length) ? '' : |
| + 'has different length and '; |
| + for (var key in expected.keys) { |
| + if (!actual.containsKey(key)) { |
| + return ["${err}is missing map key '$key'", location]; |
| } |
| - } else { |
| - var description = new StringDescription(); |
| - // If we have recursed, show the expected value too; if not, |
| - // expect() will show it for us. |
| - if (depth > 0) { |
| - description.add('was '). |
| - addDescriptionOf(actual). |
| - add(' instead of '). |
| - addDescriptionOf(expected); |
| - reason = description.toString(); |
| - } else { |
| - reason = ''; // We're not adding any value to the actual value. |
| + } |
| + |
| + for (var key in actual.keys) { |
| + if (!expected.containsKey(key)) { |
| + return ["${err}has extra map key '$key'", location]; |
| } |
| } |
| + |
| + for (var key in expected.keys) { |
| + var rp = _recursiveMatch(expected[key], actual[key], |
| + "${location}['${key}']", depth + 1); |
| + if (rp != null) return rp; |
| + } |
| + |
| + return null; |
| } |
| - if (reason == null) return null; |
| - return [reason, location]; |
| + |
| + var description = new StringDescription(); |
| + |
| + // If we have recursed, show the expected value too; if not, expect() will |
| + // show it for us. |
| + if (depth > 0) { |
| + description.add('was '). |
| + addDescriptionOf(actual). |
| + add(' instead of '). |
| + addDescriptionOf(expected); |
| + return [description.toString(), location]; |
| + } |
| + |
| + // We're not adding any value to the actual value. |
| + return ["", location]; |
| } |
| String _match(expected, actual, Map matchState) { |