Class: RSpec::Matchers::BuiltIn::OperatorMatcher

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

Direct Known Subclasses

NegativeOperatorMatcher, PositiveOperatorMatcher

Class Method Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (OperatorMatcher) initialize(actual)

A new instance of OperatorMatcher

29
30
31
# File 'lib/rspec/matchers/operator_matcher.rb', line 29
def initialize(actual)
  @actual = actual
end

Class Method Details

+ (Object) get(klass, operator)

19
20
21
22
23
24
25
26
# File 'lib/rspec/matchers/operator_matcher.rb', line 19
def get(klass, operator)
  klass.ancestors.each { |ancestor|
    matcher = registry[ancestor] && registry[ancestor][operator]
    return matcher if matcher
  }
  nil
end

+ (Object) register(klass, operator, matcher)

10
11
12
13
# File 'lib/rspec/matchers/operator_matcher.rb', line 10
def register(klass, operator, matcher)
  registry[klass] ||= {}
  registry[klass][operator] = matcher
end

+ (Object) registry

6
7
8
# File 'lib/rspec/matchers/operator_matcher.rb', line 6
def registry
  @registry ||= {}
end

+ (Object) unregister(klass, operator)

15
16
17
# File 'lib/rspec/matchers/operator_matcher.rb', line 15
def unregister(klass, operator)
  registry[klass] && registry[klass].delete(operator)
end

+ (Object) use_custom_matcher_or_delegate(operator)

33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/rspec/matchers/operator_matcher.rb', line 33
def self.use_custom_matcher_or_delegate(operator)
  define_method(operator) do |expected|
    if !has_non_generic_implementation_of?(operator) && matcher = OperatorMatcher.get(@actual.class, operator)
      @actual.__send__(::RSpec::Matchers.last_should, matcher.new(expected))
    else
      eval_match(@actual, operator, expected)
    end
  end
  negative_operator = operator.sub(/^=/, '!')
  if negative_operator != operator && respond_to?(negative_operator)
    define_method(negative_operator) do |expected|
      opposite_should = ::RSpec::Matchers.last_should == :should ? :should_not : :should
      raise "RSpec does not support `#{::RSpec::Matchers.last_should} #{negative_operator} expected`.  " +
        "Use `#{opposite_should} #{operator} expected` instead."
    end
  end
end

Instance Method Details

- (Object) description

60
61
62
# File 'lib/rspec/matchers/operator_matcher.rb', line 60
def description
  "#{@operator} #{@expected.inspect}"
end

- (Object) fail_with_message(message)

56
57
58
# File 'lib/rspec/matchers/operator_matcher.rb', line 56
def fail_with_message(message)
  RSpec::Expectations.fail_with(message, @expected, @actual)
end

- (Boolean) has_non_generic_implementation_of?(op)

Returns:

  • (Boolean)
67
68
69
70
71
# File 'lib/rspec/matchers/operator_matcher.rb', line 67
def has_non_generic_implementation_of?(op)
  Expectations.method_handle_for(@actual, op).owner != ::Kernel
rescue NameError
  false
end