Class: RSpec::Matchers::OperatorMatcher

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(actual) ⇒ OperatorMatcher

Returns a new instance of OperatorMatcher.



28
29
30
# File 'lib/rspec/matchers/operator_matcher.rb', line 28

def initialize(actual)
  @actual = actual
end

Class Method Details

.get(klass, operator) ⇒ Object



18
19
20
21
22
23
24
25
# File 'lib/rspec/matchers/operator_matcher.rb', line 18

def get(klass, operator)
  klass.ancestors.each { |ancestor|
    matcher = registry[ancestor] && registry[ancestor][operator]
    return matcher if matcher
  }

  nil
end

.register(klass, operator, matcher) ⇒ Object



9
10
11
12
# File 'lib/rspec/matchers/operator_matcher.rb', line 9

def register(klass, operator, matcher)
  registry[klass] ||= {}
  registry[klass][operator] = matcher
end

.registryObject



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

def registry
  @registry ||= {}
end

.unregister(klass, operator) ⇒ Object



14
15
16
# File 'lib/rspec/matchers/operator_matcher.rb', line 14

def unregister(klass, operator)
  registry[klass] && registry[klass].delete(operator)
end

.use_custom_matcher_or_delegate(operator) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/rspec/matchers/operator_matcher.rb', line 32

def self.use_custom_matcher_or_delegate(operator)
  define_method(operator) do |expected|
    if uses_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

#descriptionObject



59
60
61
# File 'lib/rspec/matchers/operator_matcher.rb', line 59

def description
  "#{@operator} #{@expected.inspect}"
end

#fail_with_message(message) ⇒ Object



55
56
57
# File 'lib/rspec/matchers/operator_matcher.rb', line 55

def fail_with_message(message)
  RSpec::Expectations.fail_with(message, @expected, @actual)
end