Parent

Class/Module Index [+]

Quicksearch

RSpec::Matchers::BuiltIn::BaseMatcher

@api private

Used internally as a base class for matchers that ship with rspec-expectations and rspec-rails.

### Warning:

This class is for internal use, and subject to change without notice. We strongly recommend that you do not base your custom matchers on this class. If/when this changes, we will announce it and remove this warning.

Constants

UNDEFINED

@api private Used to detect when no arg is passed to `initialize`. `nil` cannot be used because it’s a valid value to pass.

Attributes

actual[R]

@private

expected[R]

@private

rescued_exception[R]

@private

Public Class Methods

new(expected=UNDEFINED) click to toggle source
# File lib/rspec/matchers/built_in/base_matcher.rb, line 26
def initialize(expected=UNDEFINED)
  @expected = expected unless UNDEFINED.equal?(expected)
end

Public Instance Methods

description() click to toggle source

@api private Generates a “pretty” description using the logic in {Pretty}. @return [String]

# File lib/rspec/matchers/built_in/base_matcher.rb, line 78
def description
  return name_to_sentence unless defined?(@expected)
  "#{name_to_sentence}#{to_sentence @expected}"
end
diffable?() click to toggle source

@api private Matchers are not diffable by default. Override this to make your subclass diffable.

# File lib/rspec/matchers/built_in/base_matcher.rb, line 86
def diffable?
  false
end
failure_message() click to toggle source

@api private Provides a good generic failure message. Based on `description`. When subclassing, if you are not satisfied with this failure message you often only need to override `description`. @return [String]

# File lib/rspec/matchers/built_in/base_matcher.rb, line 60
def failure_message
  assert_ivars :@actual
  "expected #{@actual.inspect} to #{description}"
end
failure_message_when_negated() click to toggle source

@api private Provides a good generic negative failure message. Based on `description`. When subclassing, if you are not satisfied with this failure message you often only need to override `description`. @return [String]

# File lib/rspec/matchers/built_in/base_matcher.rb, line 70
def failure_message_when_negated
  assert_ivars :@actual
  "expected #{@actual.inspect} not to #{description}"
end
match_unless_raises(*exceptions) click to toggle source

@api private Used to wrap a block of code that will indicate failure by raising one of the named exceptions.

This is used by rspec-rails for some of its matchers that wrap rails’ assertions.

# File lib/rspec/matchers/built_in/base_matcher.rb, line 45
def match_unless_raises(*exceptions)
  exceptions.unshift Exception if exceptions.empty?
  begin
    yield
    true
  rescue *exceptions => @rescued_exception
    false
  end
end
matches?(actual) click to toggle source

@api private Indicates if the match is successful. Delegates to `match`, which should be defined on a subclass. Takes care of consistently initializing the `actual` attribute.

# File lib/rspec/matchers/built_in/base_matcher.rb, line 34
def matches?(actual)
  @actual = actual
  match(expected, actual)
end
supports_block_expectations?() click to toggle source

@api private Most matchers are value matchers (i.e. meant to work with `expect(value)`) rather than block matchers (i.e. meant to work with `expect { }`), so this defaults to false. Block matchers must override this to return true.

# File lib/rspec/matchers/built_in/base_matcher.rb, line 94
def supports_block_expectations?
  false
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.