Chromium Code Reviews| Index: lib/unittest/operator_matchers.dart |
| =================================================================== |
| --- lib/unittest/operator_matchers.dart (revision 0) |
| +++ lib/unittest/operator_matchers.dart (revision 0) |
| @@ -0,0 +1,177 @@ |
| +// Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
| +// for details. All rights reserved. Use of this source code is governed by a |
| +// BSD-style license that can be found in the LICENSE file. |
| + |
| +/** |
| + * This returns a matcher that inverts [matcher] to its logical negation. |
| + */ |
| +IMatcher isNot(matcher) => new _IsNot(wrapMatcher(matcher)); |
| + |
| +class _IsNot extends Matcher { |
| + |
| + IMatcher _matcher; |
| + |
| + _IsNot(IMatcher this._matcher); |
| + |
| + bool matches(item) => !_matcher.matches(item); |
| + |
| + IDescription describe(IDescription description) => |
| + description.append('not ').appendDescriptionOf(_matcher); |
| +} |
| + |
| +/** |
| + * This returns a matcher that matches if all of the matchers passed as |
| + * arguments (up to 7) match. Instead of passing the matchers separately |
|
Bob Nystrom
2012/05/30 23:23:51
Why 7?
gram
2012/06/01 17:33:15
Because it is greater than 6 and less than 8 ;-)
Bob Nystrom
2012/06/01 18:22:22
Works for me.
|
| + * they can be passed as a single List argument. |
| + * Any argument that is not a matcher is implicitly wrapped in a |
| + * Matcher to check for equality. |
| +*/ |
| +IMatcher allOf(arg0, |
| + [arg1 = null, |
| + arg2 = null, |
| + arg3 = null, |
| + arg4 = null, |
| + arg5 = null, |
| + arg6 = null]) { |
| + if (arg0 is List) { |
| + expect(arg1, isNull()); |
| + expect(arg2, isNull()); |
| + expect(arg3, isNull()); |
| + expect(arg4, isNull()); |
| + expect(arg5, isNull()); |
| + expect(arg6, isNull()); |
| + for (int i = 0; i < arg0.length; i++) { |
| + arg0[i] = wrapMatcher(arg0[i]); |
| + } |
| + return new _AllOf(arg0); |
| + } else { |
| + List matchers = new List(); |
| + if (arg0 != null) { |
| + matchers.add(arg0); |
| + } |
| + if (arg1 != null) { |
| + matchers.add(arg1); |
| + } |
| + if (arg2 != null) { |
| + matchers.add(arg2); |
| + } |
| + if (arg3 != null) { |
| + matchers.add(arg3); |
| + } |
| + if (arg4 != null) { |
| + matchers.add(arg4); |
| + } |
| + if (arg5 != null) { |
| + matchers.add(arg5); |
| + } |
| + if (arg6 != null) { |
| + matchers.add(arg6); |
| + } |
| + return new _AllOf(matchers); |
| + } |
| +} |
| + |
| +class _AllOf extends Matcher { |
| + var _matchers; |
| + |
| + _AllOf(this._matchers); |
| + |
| + bool matches(item) { |
| + for (var matcher in _matchers) { |
| + if (!matcher.matches(item)) { |
| + return false; |
| + } |
| + } |
| + return true; |
| + } |
| + |
| + IDescription describeMismatch(item, IDescription mismatchDescription) { |
| + for (var matcher in _matchers) { |
| + if (!matcher.matches(item)) { |
| + mismatchDescription.appendDescriptionOf(matcher).append(' '); |
| + matcher.describeMismatch(item, mismatchDescription); |
| + break; |
| + } |
| + } |
| + return mismatchDescription; |
| + } |
| + |
| + IDescription describe(IDescription description) => |
| + description.appendList('(', ' and ', ')', _matchers); |
| +} |
| + |
| +/** |
| + * Matches if any of the given matchers evaluate to true. The |
| + * arguments can be a set of matchers as separate parameters |
| + * (up to 7), or a List of matchers. |
| + * |
| + * The matchers are evaluated from left to right using short-circuit |
| + * evaluation, so evaluation stops as soon as a matcher returns true. |
| + * |
| + * Any argument that is not a matcher is implicitly wrapped in a |
| + * Matcher to check for equality. |
| +*/ |
| + |
| +IMatcher anyOf(arg0, |
| + [arg1 = null, |
| + arg2 = null, |
| + arg3 = null, |
| + arg4 = null, |
| + arg5 = null, |
| + arg6 = null]) { |
| + if (arg0 is List) { |
| + expect(arg1, isNull()); |
| + expect(arg2, isNull()); |
| + expect(arg3, isNull()); |
| + expect(arg4, isNull()); |
| + expect(arg5, isNull()); |
| + expect(arg6, isNull()); |
| + for (int i = 0; i < arg0.length; i++) { |
| + arg0[i] = wrapMatcher(arg0[i]); |
| + } |
| + return new _AnyOf(arg0); |
| + } else { |
| + List matchers = new List(); |
| + if (arg0 != null) { |
| + matchers.add(arg0); |
| + } |
| + if (arg1 != null) { |
| + matchers.add(arg1); |
| + } |
| + if (arg2 != null) { |
| + matchers.add(arg2); |
| + } |
| + if (arg3 != null) { |
| + matchers.add(arg3); |
| + } |
| + if (arg4 != null) { |
| + matchers.add(arg4); |
| + } |
| + if (arg5 != null) { |
| + matchers.add(arg5); |
| + } |
| + if (arg6 != null) { |
| + matchers.add(arg6); |
| + } |
| + return new _AnyOf(matchers); |
| + } |
| +} |
| + |
| +class _AnyOf extends Matcher { |
| + var _matchers; |
| + |
| + _AnyOf(this._matchers); |
| + |
| + bool matches(item) { |
| + for (var matcher in _matchers) { |
| + if (matcher.matches(item)) { |
| + return true; |
| + } |
| + } |
| + return false; |
| + } |
| + |
| + IDescription describe(IDescription description) => |
| + description.appendList('(', ' or ', ')', _matchers); |
| +} |
| + |