Chromium Code Reviews| Index: lib/unittest/numeric_matchers.dart |
| =================================================================== |
| --- lib/unittest/numeric_matchers.dart (revision 0) |
| +++ lib/unittest/numeric_matchers.dart (revision 0) |
| @@ -0,0 +1,209 @@ |
| +// 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. |
| + |
| +/** |
| + * Returns a matcher which matches if the match argument is greater |
| + * than the given [value]. |
| + */ |
| +IMatcher greaterThan(value) => |
| + new _OrderingComparison(value, false, false, true, 'greater than'); |
| + |
| +/** |
| + * Returns a matcher which matches if the match argument is greater |
| + * than or equal to the given [value]. |
| + */ |
| +IMatcher greaterThanOrEqualTo(value) => |
| + new _OrderingComparison(value, true, false, true, 'greater than or equal to'); |
| + |
| +/** |
| + * Returns a matcher which matches if the match argument is less |
| + * than the given [value]. |
| + */ |
| +IMatcher lessThan(value) => |
| + new _OrderingComparison(value, false, true, false, 'less than'); |
| + |
| +/** |
| + * Returns a matcher which matches if the match argument is less |
| + * than or equal to the given [value]. |
| + */ |
| +IMatcher lessThanOrEqualTo(value) => |
| + new _OrderingComparison(value, true, true, false, 'less than or equal to'); |
| + |
| +/** |
| + * A matcher which matches if the match argument is zero. |
| + */ |
| +final IMatcher isZero = |
| + const _OrderingComparison(0, true, false, false, 'equal to'); |
| + |
| + |
| +/** |
| + * A matcher which matches if the match argument is non-zero. |
| + */ |
| +final IMatcher isNonZero = |
| + const _OrderingComparison(0, false, true, true, 'not equal to'); |
| + |
| +/** |
| + * A matcher which matches if the match argument is positive |
| + * (0 inclusive). |
| + */ |
| +final IMatcher isPositive = |
| + const _OrderingComparison(0, true, false, true, 'positive', false); |
| + |
| +/** |
| + * A matcher which matches if the match argument is negative |
| + * (0 exclusive). |
| + */ |
| +final IMatcher isNegative = |
| + const _OrderingComparison(0, false, true, false, 'negative', false); |
| + |
| +bool _isNumeric(value) { |
| + return value is int || value is double; |
| +} |
| + |
| +class _OrderingComparison extends Matcher { |
| + /** Expected value. */ |
| + final _value; |
| + /** What to return if actual == expected */ |
| + final bool _equalValue; |
| + /** What to return if actual < expected */ |
| + final bool _lessThanValue; |
| + /** What to return if actual > expected */ |
| + final bool _greaterThanValue; |
| + /** Textual name of the inequality */ |
| + final String _comparisonDescription; |
| + /** Whether to include the expected value in the description */ |
| + final bool _valueInDescription; |
| + |
| + const _OrderingComparison( |
| + this._value, |
| + this._equalValue, |
| + this._lessThanValue, |
| + this._greaterThanValue, |
| + this._comparisonDescription, |
| + [this._valueInDescription = true]); |
| + |
| + bool matches(item) { |
| + if (item == _value) { |
| + return _equalValue; |
| + } else if (item < _value) { |
| + return _lessThanValue; |
| + } else { |
| + return _greaterThanValue; |
| + } |
| + } |
| + |
| + IDescription describe(IDescription description) { |
| + return description.add('a value '). |
| + add(_comparisonDescription). |
| + add(' '). |
| + addDescriptionOf(_value); |
| + } |
| +} |
| + |
| +/** |
| + * Returns a matcher which matches if the match argument is within [delta] |
| + * of some [value]; i.e. if the match argument is greater than |
| + * than or equal [value]-[delta] and less than or equal to [value]+[delta]. |
| + */ |
| +IMatcher closeTo(value, delta) => new _IsCloseTo(value, delta); |
| + |
| +class _IsCloseTo extends Matcher { |
| + final num _value, _delta; |
| + |
| + const _IsCloseTo(this._value, this._delta); |
| + |
| + bool matches(item) { |
| + if (!_isNumeric(item)) { |
| + return false; |
| + } |
| + var diff = item - _value; |
| + if (diff < 0) diff = -diff; |
| + return (diff <= _delta); |
| + } |
| + |
| + IDescription describe(IDescription description) => |
| + description.add('a numeric value within '). |
| + addDescriptionOf(_delta). |
| + add(' of '). |
| + addDescriptionOf(_value); |
| + |
| + IDescription describeMismatch(item, IDescription mismatchDescription) { |
| + if (item is !num) { |
| + return mismatchDescription. |
| + addDescriptionOf(item). |
| + add(' not numeric'); |
| + } else { |
| + var diff = item - _value; |
| + if (diff < 0) diff = -diff; |
| + return mismatchDescription. |
| + addDescriptionOf(item). |
| + add(' differed by '). |
| + addDescriptionOf(diff); |
| + } |
| + } |
| +} |
| + |
| +/** |
| + * Returns a matcher which matches if the match argument is greater |
| + * than or equal to [low] and less than or equal to [high]. |
| + */ |
| +IMatcher inInclusiveRange(low, high) => new _InRange(low, high, true, true); |
| + |
| +/** |
| + * Returns a matcher which matches if the match argument is greater |
| + * than [low] and less than [high]. |
| + */ |
| +IMatcher inExclusiveRange(low, high) => new _InRange(low, high, false, false); |
| + |
| +/** |
| + * Returns a matcher which matches if the match argument is greater |
| + * than [low] and less than or equal to [high]. |
| + */ |
| +IMatcher inOpenClosedRange(low, high) => new _InRange(low, high, false, true); |
| + |
| +/** |
| + * Returns a matcher which matches if the match argument is greater |
| + * than or equal to a [low] and less than [high]. |
| + */ |
| +IMatcher inClosedOpenRange(low, high) => new _InRange(low, high, true, false); |
| + |
| +class _InRange extends Matcher { |
| + final num _low, _high; |
| + final bool _lowMatchValue, _highMatchValue; |
| + |
| + const _InRange(this._low, this._high, |
| + this._lowMatchValue, this._highMatchValue); |
| + |
| + bool matches(value) { |
| + if (value is !num) { // !_isNumeric(value)) { |
|
Bob Nystrom
2012/06/01 18:22:23
Remove dead code.
gram
2012/06/01 22:22:00
Done.
|
| + return false; |
| + } |
| + if (value < _low || value > _high) { |
| + return false; |
| + } |
| + if (value == _low) { |
| + return _lowMatchValue; |
| + } |
| + if (value == _high) { |
| + return _highMatchValue; |
| + } |
| + return true; |
| + } |
| + |
| + IDescription describe(IDescription description) => |
| + description.add("be in range from " |
| + "$_low (${_lowMatchValue ? 'inclusive' : 'exclusive'}) to " |
| + "$_high (${_highMatchValue ? 'inclusive' : 'exclusive'})"); |
| + |
| + IDescription describeMismatch(item, IDescription mismatchDescription) { |
| + if (item is !num) { |
| + return mismatchDescription. |
| + addDescriptionOf(item). |
| + add(' not numeric'); |
| + } else { |
| + return super.describeMismatch(item, mismatchDescription); |
| + } |
| + } |
| +} |
| + |