matchi 4.1.1 → 4.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,51 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Matchi
4
- class BeWithin
5
- # *BeWithin of* matcher.
6
- class Of
7
- # Initialize the matcher with a delta and an expected value.
8
- #
9
- # @example
10
- # require "matchi/be_within/of"
11
- #
12
- # Matchi::BeWithin::Of.new(1, 41)
13
- #
14
- # @param delta [Numeric] The accepted variation of the actual value.
15
- # @param expected [Numeric] The expected value.
16
- def initialize(delta, expected)
17
- raise ::ArgumentError, "delta must be a Numeric" unless delta.is_a?(::Numeric)
18
- raise ::ArgumentError, "expected must be a Numeric" unless expected.is_a?(::Numeric)
19
- raise ::ArgumentError, "delta must be non-negative" if delta.negative?
20
-
21
- @delta = delta
22
- @expected = expected
23
- end
24
-
25
- # Boolean comparison on the expected be_within by comparing the actual
26
- # value and the expected value.
27
- #
28
- # @example
29
- # require "matchi/be_within/of"
30
- #
31
- # matcher = Matchi::BeWithin::Of.new(1, 41)
32
- # matcher.match? { 42 } # => true
33
- #
34
- # @yieldreturn [Numeric] The block of code to execute.
35
- #
36
- # @return [Boolean] Comparison between the actual and the expected values.
37
- def match?
38
- raise ::ArgumentError, "a block must be provided" unless block_given?
39
-
40
- (@expected - yield).abs <= @delta
41
- end
42
-
43
- # Returns a string representing the matcher.
44
- #
45
- # @return [String] a human-readable description of the matcher
46
- def to_s
47
- "be within #{@delta} of #{@expected}"
48
- end
49
- end
50
- end
51
- end