Sha256: 4a0a01fdc0cc4bbd4d30f9ce365475be7d8ff23db4b298ab897ed4974a678a0c
Contents?: true
Size: 1.56 KB
Versions: 3
Compression:
Stored size: 1.56 KB
Contents
# frozen_string_literal: true module Matchi class BeWithin # *BeWithin of* matcher. class Of # Initialize the matcher with a delta and an expected value. # # @example # require "matchi/be_within/of" # # Matchi::BeWithin::Of.new(1, 41) # # @param delta [Numeric] The accepted variation of the actual value. # @param expected [Numeric] The expected value. def initialize(delta, expected) raise ::ArgumentError, "delta must be a Numeric" unless delta.is_a?(::Numeric) raise ::ArgumentError, "expected must be a Numeric" unless expected.is_a?(::Numeric) raise ::ArgumentError, "delta must be non-negative" if delta.negative? @delta = delta @expected = expected end # Boolean comparison on the expected be_within by comparing the actual # value and the expected value. # # @example # require "matchi/be_within/of" # # matcher = Matchi::BeWithin::Of.new(1, 41) # matcher.match? { 42 } # => true # # @yieldreturn [Numeric] The block of code to execute. # # @return [Boolean] Comparison between the actual and the expected values. def match? raise ::ArgumentError, "a block must be provided" unless block_given? (@expected - yield).abs <= @delta end # Returns a string representing the matcher. # # @return [String] a human-readable description of the matcher def to_s "be within #{@delta} of #{@expected}" end end end end
Version data entries
3 entries across 3 versions & 1 rubygems
Version | Path |
---|---|
matchi-4.1.1 | lib/matchi/be_within/of.rb |
matchi-4.1.0 | lib/matchi/be_within/of.rb |
matchi-4.0.0 | lib/matchi/be_within/of.rb |