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

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

Instance Method Summary (collapse)

Constructor Details

- (BeWithin) initialize(delta)

A new instance of BeWithin



5
6
7
# File 'lib/rspec/matchers/built_in/be_within.rb', line 5

def initialize(delta)
  @delta = delta
end

Instance Method Details

- (Object) description



39
40
41
# File 'lib/rspec/matchers/built_in/be_within.rb', line 39

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

- (Object) failure_message_for_should



31
32
33
# File 'lib/rspec/matchers/built_in/be_within.rb', line 31

def failure_message_for_should
  "expected #{@actual} to #{description}"
end

- (Object) failure_message_for_should_not



35
36
37
# File 'lib/rspec/matchers/built_in/be_within.rb', line 35

def failure_message_for_should_not
  "expected #{@actual} not to #{description}"
end

- (Boolean) matches?(actual) Also known as: ==

Returns:

  • (Boolean)


9
10
11
12
13
14
# File 'lib/rspec/matchers/built_in/be_within.rb', line 9

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)



17
18
19
20
21
22
# File 'lib/rspec/matchers/built_in/be_within.rb', line 17

def of(expected)
  @expected  = expected
  @tolerance = @delta
  @unit      = ''
  self
end

- (Object) percent_of(expected)



24
25
26
27
28
29
# File 'lib/rspec/matchers/built_in/be_within.rb', line 24

def percent_of(expected)
  @expected  = expected
  @tolerance = @delta * @expected.abs / 100.0
  @unit      = '%'
  self
end