Class: MotionSpec::Matcher::BeWithin

Inherits:
Object
  • Object
show all
Defined in:
lib/motion-spec/matcher/be_within.rb

Constant Summary collapse

INVALID_MATCH_ERROR =
'be_within matcher incomplete. Missing .of value'

Instance Method Summary collapse

Constructor Details

#initialize(range) ⇒ BeWithin

Returns a new instance of BeWithin.



7
8
9
# File 'lib/motion-spec/matcher/be_within.rb', line 7

def initialize(range)
  @range = range
end

Instance Method Details

#fail!(subject, negated) ⇒ Object



21
22
23
24
25
26
27
# File 'lib/motion-spec/matcher/be_within.rb', line 21

def fail!(subject, negated)
  fail FailedExpectation.new(
    FailMessageRenderer.message_for_be_within(
      negated, subject, @range, @center_value
    )
  )
end

#matches?(subject) ⇒ Boolean

Returns:

  • (Boolean)


16
17
18
19
# File 'lib/motion-spec/matcher/be_within.rb', line 16

def matches?(subject)
  fail InvalidMatcher.new(INVALID_MATCH_ERROR) unless @center_value
  (subject - @center_value).abs <= @range
end

#of(center_value) ⇒ Object



11
12
13
14
# File 'lib/motion-spec/matcher/be_within.rb', line 11

def of(center_value)
  @center_value = center_value
  self
end