Class: BeCloseMatcher

Inherits:
Object show all
Defined in:
lib/extensions/mspec/mspec/matchers/be_close.rb

Instance Method Summary collapse

Constructor Details

#initialize(expected, tolerance) ⇒ BeCloseMatcher

Returns a new instance of BeCloseMatcher.



4
5
6
7
# File 'lib/extensions/mspec/mspec/matchers/be_close.rb', line 4

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

Instance Method Details

#failure_messageObject



14
15
16
# File 'lib/extensions/mspec/mspec/matchers/be_close.rb', line 14

def failure_message
  ["Expected #{@expected}", "to be within +/- #{@tolerance} of #{@actual}"]
end

#matches?(actual) ⇒ Boolean

Returns:

  • (Boolean)


9
10
11
12
# File 'lib/extensions/mspec/mspec/matchers/be_close.rb', line 9

def matches?(actual)
  @actual = actual
  (@actual - @expected).abs < @tolerance
end

#negative_failure_messageObject



18
19
20
# File 'lib/extensions/mspec/mspec/matchers/be_close.rb', line 18

def negative_failure_message
  ["Expected #{@expected}", "not to be within +/- #{@tolerance} of #{@actual}"]
end