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

Inherits:
BaseMatcher show all
Defined in:
lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/rspec-expectations-3.12.2/lib/rspec/matchers/built_in/be_within.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Provides the implementation for ‘be_within`. Not intended to be instantiated directly.

Constant Summary

Constants inherited from BaseMatcher

RSpec::Matchers::BuiltIn::BaseMatcher::UNDEFINED

Instance Attribute Summary

Attributes inherited from BaseMatcher

#actual, #expected, #matcher_name, #rescued_exception

Instance Method Summary collapse

Methods inherited from BaseMatcher

#actual_formatted, #diffable?, #expected_formatted, #expects_call_stack_jump?, #match_unless_raises, matcher_name, #supports_block_expectations?, #supports_value_expectations?

Methods included from RSpec::Matchers::BuiltIn::BaseMatcher::DefaultFailureMessages

has_default_failure_messages?

Methods included from RSpec::Matchers::BuiltIn::BaseMatcher::HashFormatting

improve_hash_formatting

Methods included from Composable

#===, #and, #description_of, #or, should_enumerate?, surface_descriptions_in, unreadable_io?, #values_match?

Constructor Details

#initialize(delta) ⇒ BeWithin

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of BeWithin.



8
9
10
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/rspec-expectations-3.12.2/lib/rspec/matchers/built_in/be_within.rb', line 8

def initialize(delta)
  @delta = delta
end

Instance Method Details

#descriptionString

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:



52
53
54
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/rspec-expectations-3.12.2/lib/rspec/matchers/built_in/be_within.rb', line 52

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

#failure_messageString

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:



40
41
42
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/rspec-expectations-3.12.2/lib/rspec/matchers/built_in/be_within.rb', line 40

def failure_message
  "expected #{actual_formatted} to #{description}#{not_numeric_clause}"
end

#failure_message_when_negatedString

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:



46
47
48
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/rspec-expectations-3.12.2/lib/rspec/matchers/built_in/be_within.rb', line 46

def failure_message_when_negated
  "expected #{actual_formatted} not to #{description}"
end

#matches?(actual) ⇒ Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (Boolean)


32
33
34
35
36
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/rspec-expectations-3.12.2/lib/rspec/matchers/built_in/be_within.rb', line 32

def matches?(actual)
  @actual = actual
  raise needs_expected unless defined? @expected
  numeric? && (@actual - @expected).abs <= @tolerance
end

#of(expected) ⇒ Object

Sets the expected value.



14
15
16
17
18
19
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/rspec-expectations-3.12.2/lib/rspec/matchers/built_in/be_within.rb', line 14

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

#percent_of(expected) ⇒ Object

Sets the expected value, and makes the matcher do a percent comparison.



24
25
26
27
28
29
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/rspec-expectations-3.12.2/lib/rspec/matchers/built_in/be_within.rb', line 24

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