Class: RSpec::Matchers::OperatorMatcher

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

Direct Known Subclasses

BuiltIn::NegativeOperatorMatcher, BuiltIn::PositiveOperatorMatcher

Class Method Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (OperatorMatcher) initialize(actual)

A new instance of OperatorMatcher



19
20
21
# File 'lib/rspec/matchers/operator_matcher.rb', line 19

def initialize(actual)
  @actual = actual
end

Class Method Details

+ (Object) get(klass, operator)



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

def get(klass, operator)
  registry[klass] && registry[klass][operator]
end

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



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

+ (Object) registry



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

def registry
  @registry ||= {}
end

+ (Object) use_custom_matcher_or_delegate(operator)



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/rspec/matchers/operator_matcher.rb', line 23

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



50
51
52
# File 'lib/rspec/matchers/operator_matcher.rb', line 50

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

- (Object) fail_with_message(message)



46
47
48
# File 'lib/rspec/matchers/operator_matcher.rb', line 46

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