Class: Crspec::Matchers::BeWithinMatcher

Inherits:
BaseMatcher show all
Defined in:
lib/crspec/matchers.rb

Instance Attribute Summary

Attributes inherited from BaseMatcher

#actual, #expected

Instance Method Summary collapse

Methods inherited from BaseMatcher

#and, #failure_message_when_negated, #or

Constructor Details

#initialize(delta) ⇒ BeWithinMatcher

Returns a new instance of BeWithinMatcher.



523
524
525
526
# File 'lib/crspec/matchers.rb', line 523

def initialize(delta)
  super(delta)
  @delta = delta
end

Instance Method Details

#failure_messageObject



546
547
548
# File 'lib/crspec/matchers.rb', line 546

def failure_message
  "Expected #{@actual.inspect} to be within #{@delta} of #{@center}"
end

#matches?(actual) ⇒ Boolean

Returns:

  • (Boolean)

Raises:

  • (ArgumentError)


539
540
541
542
543
544
# File 'lib/crspec/matchers.rb', line 539

def matches?(actual)
  raise ArgumentError, "be_within requires .of(value)" unless defined?(@center)

  @actual = actual
  (actual - @center).abs <= @delta
end

#of(value) ⇒ Object



528
529
530
531
# File 'lib/crspec/matchers.rb', line 528

def of(value)
  @center = value
  self
end

#percent_of(value) ⇒ Object



533
534
535
536
537
# File 'lib/crspec/matchers.rb', line 533

def percent_of(value)
  @center = value
  @delta = value.abs * @expected / 100.0
  self
end