Class: RSpec::Matchers::BuiltIn::BeWithin

Inherits:
Object
  • Object
show all
Includes:
MatchAliases
Defined in:
lib/rspec/matchers/built_in/be_within.rb

Instance Method Summary (collapse)

Methods included from MatchAliases

#==, #===

Constructor Details

- (BeWithin) initialize(delta)

A new instance of BeWithin

7
8
9
# File 'lib/rspec/matchers/built_in/be_within.rb', line 7
def initialize(delta)
  @delta = delta
end

Instance Method Details

- (Object) description

40
41
42
# File 'lib/rspec/matchers/built_in/be_within.rb', line 40
def description
  "be within #{@delta}#{@unit} of #{@expected}"
end

- (Object) failure_message_for_should

32
33
34
# File 'lib/rspec/matchers/built_in/be_within.rb', line 32
def failure_message_for_should
  "expected #{@actual} to #{description}"
end

- (Object) failure_message_for_should_not

36
37
38
# File 'lib/rspec/matchers/built_in/be_within.rb', line 36
def failure_message_for_should_not
  "expected #{@actual} not to #{description}"
end

- (Boolean) matches?(actual)

Returns:

  • (Boolean)
11
12
13
14
15
16
# File 'lib/rspec/matchers/built_in/be_within.rb', line 11
def matches?(actual)
  @actual = actual
  raise needs_expected     unless defined? @expected
  raise needs_subtractable unless @actual.respond_to? :-
  (@actual - @expected).abs <= @tolerance
end

- (Object) of(expected)

18
19
20
21
22
23
# File 'lib/rspec/matchers/built_in/be_within.rb', line 18
def of(expected)
  @expected  = expected
  @tolerance = @delta
  @unit      = ''
  self
end

- (Object) percent_of(expected)

25
26
27
28
29
30
# File 'lib/rspec/matchers/built_in/be_within.rb', line 25
def percent_of(expected)
  @expected  = expected
  @tolerance = @delta * @expected.abs / 100.0
  @unit      = '%'
  self
end