Class: Matchi::BeWithin

Inherits:
Object
  • Object
show all
Defined in:
lib/matchi/be_within.rb

Overview

Delta comparison matcher that checks if a numeric value is within a specified range.

This matcher verifies that a numeric value falls within a certain distance (delta) of an expected value. It’s particularly useful for floating-point comparisons or when checking if a value is approximately equal to another within a given tolerance.

Examples:

Basic usage with integers

matcher = Matchi::BeWithin.new(1).of(10)
matcher.match? { 9 }    # => true
matcher.match? { 10 }   # => true
matcher.match? { 11 }   # => true
matcher.match? { 12 }   # => false

Floating point comparisons

matcher = Matchi::BeWithin.new(0.1).of(3.14)
matcher.match? { 3.1 }   # => true
matcher.match? { 3.2 }   # => true
matcher.match? { 3.0 }   # => false

Working with percentages

matcher = Matchi::BeWithin.new(5).of(100)  # 5% margin
matcher.match? { 95 }    # => true
matcher.match? { 105 }   # => true
matcher.match? { 110 }   # => false

See Also:

Defined Under Namespace

Classes: Of

Instance Method Summary collapse

Constructor Details

#initialize(delta) ⇒ BeWithin

Initialize the matcher with a delta value.

Examples:

BeWithin.new(0.5)           # Using float
BeWithin.new(2)             # Using integer

Parameters:

  • delta (Numeric)

    The maximum allowed difference from the expected value

Raises:

  • (ArgumentError)

    if delta is not a Numeric

  • (ArgumentError)

    if delta is negative



45
46
47
48
49
50
# File 'lib/matchi/be_within.rb', line 45

def initialize(delta)
  raise ::ArgumentError, "delta must be a Numeric" unless delta.is_a?(::Numeric)
  raise ::ArgumentError, "delta must be non-negative" if delta.negative?

  @delta = delta
end

Instance Method Details

#match?Boolean

Raises NotImplementedError as this is not a complete matcher.

This class acts as a builder for the actual matcher, which is created by calling the #of method. Direct use of #match? is not supported.

Examples:

# Don't do this:
BeWithin.new(0.5).match? { 42 }  # Raises NotImplementedError

# Do this instead:
BeWithin.new(0.5).of(42).match? { 41.8 }  # Works correctly

Returns:

  • (Boolean)

Raises:

  • (NotImplementedError)

    always, as this method should not be used



67
68
69
# File 'lib/matchi/be_within.rb', line 67

def match?
  raise ::NotImplementedError, "BeWithin is not a complete matcher. Use BeWithin#of to create a valid matcher."
end

#of(expected) ⇒ #match?

Specifies the expected reference value.

Examples:

be_within_wrapper = BeWithin.new(0.5)
be_within_wrapper.of(3.14)  # Creates matcher for values in range 2.64..3.64

Parameters:

  • expected (Numeric)

    The reference value to compare against

Returns:

  • (#match?)

    A matcher that checks if a value is within the specified range

Raises:

  • (ArgumentError)

    if expected is not a Numeric



84
85
86
# File 'lib/matchi/be_within.rb', line 84

def of(expected)
  Of.new(@delta, expected)
end