Line data Source code
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 3 : // BSD-style license that can be found in the LICENSE file. 4 : 5 : import 'feature_matcher.dart'; 6 : import 'interfaces.dart'; 7 : 8 : /// Returns a matcher which matches if the match argument is within [delta] 9 : /// of some [value]. 10 : /// 11 : /// In other words, this matches if the match argument is greater than 12 : /// than or equal [value]-[delta] and less than or equal to [value]+[delta]. 13 0 : Matcher closeTo(num value, num delta) => _IsCloseTo(value, delta); 14 : 15 : class _IsCloseTo extends FeatureMatcher<num> { 16 : final num _value, _delta; 17 : 18 0 : const _IsCloseTo(this._value, this._delta); 19 : 20 0 : @override 21 : bool typedMatches(dynamic item, Map matchState) { 22 0 : var diff = item - _value; 23 0 : if (diff < 0) diff = -diff; 24 0 : return diff <= _delta; 25 : } 26 : 27 0 : @override 28 : Description describe(Description description) => description 29 0 : .add('a numeric value within ') 30 0 : .addDescriptionOf(_delta) 31 0 : .add(' of ') 32 0 : .addDescriptionOf(_value); 33 : 34 0 : @override 35 : Description describeTypedMismatch(dynamic item, 36 : Description mismatchDescription, Map matchState, bool verbose) { 37 0 : var diff = item - _value; 38 0 : if (diff < 0) diff = -diff; 39 0 : return mismatchDescription.add(' differs by ').addDescriptionOf(diff); 40 : } 41 : } 42 : 43 : /// Returns a matcher which matches if the match argument is greater 44 : /// than or equal to [low] and less than or equal to [high]. 45 0 : Matcher inInclusiveRange(num low, num high) => _InRange(low, high, true, true); 46 : 47 : /// Returns a matcher which matches if the match argument is greater 48 : /// than [low] and less than [high]. 49 0 : Matcher inExclusiveRange(num low, num high) => 50 0 : _InRange(low, high, false, false); 51 : 52 : /// Returns a matcher which matches if the match argument is greater 53 : /// than [low] and less than or equal to [high]. 54 0 : Matcher inOpenClosedRange(num low, num high) => 55 0 : _InRange(low, high, false, true); 56 : 57 : /// Returns a matcher which matches if the match argument is greater 58 : /// than or equal to a [low] and less than [high]. 59 0 : Matcher inClosedOpenRange(num low, num high) => 60 0 : _InRange(low, high, true, false); 61 : 62 : class _InRange extends FeatureMatcher<num> { 63 : final num _low, _high; 64 : final bool _lowMatchValue, _highMatchValue; 65 : 66 0 : const _InRange( 67 : this._low, this._high, this._lowMatchValue, this._highMatchValue); 68 : 69 0 : @override 70 : bool typedMatches(dynamic value, Map matchState) { 71 0 : if (value < _low || value > _high) { 72 : return false; 73 : } 74 0 : if (value == _low) { 75 0 : return _lowMatchValue; 76 : } 77 0 : if (value == _high) { 78 0 : return _highMatchValue; 79 : } 80 : // Value may still be outside if range if it can't be compared. 81 0 : return value > _low && value < _high; 82 : } 83 : 84 0 : @override 85 : Description describe(Description description) => 86 0 : description.add('be in range from ' 87 0 : "$_low (${_lowMatchValue ? 'inclusive' : 'exclusive'}) to " 88 0 : "$_high (${_highMatchValue ? 'inclusive' : 'exclusive'})"); 89 : }