Class: Matchi::BeWithin::Of

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

Overview

*BeWithin of* matcher.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(delta, expected) ⇒ Of

Initialize the matcher with a delta and an expected value.

Examples:

require "matchi/be_within/of"

Matchi::BeWithin::Of.new(1, 41)

Parameters:

  • delta (Numeric)

    The accepted variation of the actual value.

  • expected (Numeric)

    The expected value.



19
20
21
22
# File 'lib/matchi/be_within/of.rb', line 19

def initialize(delta, expected)
  @delta    = delta
  @expected = expected
end

Instance Attribute Details

#expectedNumeric (readonly)

Returns An expected value.

Returns:

  • (Numeric)

    An expected value.



8
9
10
# File 'lib/matchi/be_within/of.rb', line 8

def expected
  @expected
end

Instance Method Details

#inspectObject

A string containing a human-readable representation of the matcher.



43
44
45
# File 'lib/matchi/be_within/of.rb', line 43

def inspect
  "#{self.class}(#{@delta}, #{expected})"
end

#matches?Boolean

Boolean comparison on the expected be_within by comparing the actual value and the expected value.

Examples:

require "matchi/be_within/of"

matcher = Matchi::BeWithin::Of.new(1, 41)

matcher.expected        # => 41
matcher.matches? { 42 } # => true

Yield Returns:

  • (Numeric)

    The block of code to execute.

Returns:

  • (Boolean)

    Comparison between the actual and the expected values.



38
39
40
# File 'lib/matchi/be_within/of.rb', line 38

def matches?
  (expected - yield).abs <= @delta
end

#to_sObject

Returns a string representing the matcher.



48
49
50
# File 'lib/matchi/be_within/of.rb', line 48

def to_s
  "be within #{@delta} of #{expected}"
end