Class: RSpec::Matchers::BuiltIn::BaseMatcher Private

Inherits:
Object
  • Object
show all
Includes:
DefaultFailureMessages, Composable
Defined in:
lib/rspec/matchers/built_in/base_matcher.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.

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.

Direct Known Subclasses

All, Be, BeAKindOf, BeAnInstanceOf, BeBetween, BeComparedTo, BeFalsey, BeNil, BePredicate, BeTruthy, BeWithin, Change, ChangeRelatively, Compound, ContainExactly, Cover, Eq, Eql, Equal, Exist, Has, HaveAttributes, Include, Match, Output, RespondTo, Satisfy, SpecificValuesChange, StartOrEndWith, YieldControl, YieldSuccessiveArgs, YieldWithArgs, YieldWithNoArgs

Defined Under Namespace

Modules: DefaultFailureMessages

Constant Summary

UNDEFINED =

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

Used to detect when no arg is passed to initialize. nil cannot be used because it's a valid value to pass.

Object.new.freeze

Instance Method Summary (collapse)

Methods included from DefaultFailureMessages

#failure_message, #failure_message_when_negated

Methods included from Composable

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

Constructor Details

- (BaseMatcher) initialize(expected = UNDEFINED)

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 BaseMatcher

28
29
30
# File 'lib/rspec/matchers/built_in/base_matcher.rb', line 28
def initialize(expected=UNDEFINED)
  @expected = expected unless UNDEFINED.equal?(expected)
end

Instance Method Details

- (String) description

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.

Generates a description using EnglishPhrasing.

Returns:

  • (String)
60
61
62
63
64
# File 'lib/rspec/matchers/built_in/base_matcher.rb', line 60
def description
  desc = EnglishPhrasing.split_words(self.class.matcher_name)
  desc << EnglishPhrasing.list(@expected) if defined?(@expected)
  desc
end

- (Boolean) diffable?

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.

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

Returns:

  • (Boolean)
69
70
71
# File 'lib/rspec/matchers/built_in/base_matcher.rb', line 69
def diffable?
  false
end

- (Boolean) expects_call_stack_jump?

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)
82
83
84
# File 'lib/rspec/matchers/built_in/base_matcher.rb', line 82
def expects_call_stack_jump?
  false
end

- (Object) match_unless_raises(*exceptions)

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.

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.

47
48
49
50
51
52
53
54
55
# File 'lib/rspec/matchers/built_in/base_matcher.rb', line 47
def match_unless_raises(*exceptions)
  exceptions.unshift Exception if exceptions.empty?
  begin
    yield
    true
  rescue *exceptions => @rescued_exception
    false
  end
end

- (Boolean) matches?(actual)

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.

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

Returns:

  • (Boolean)
36
37
38
39
# File 'lib/rspec/matchers/built_in/base_matcher.rb', line 36
def matches?(actual)
  @actual = actual
  match(expected, actual)
end

- (Boolean) supports_block_expectations?

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.

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.

Returns:

  • (Boolean)
77
78
79
# File 'lib/rspec/matchers/built_in/base_matcher.rb', line 77
def supports_block_expectations?
  false
end