Class: Moarspec::Matchers::Not

Inherits:
RSpec::Matchers::BuiltIn::BaseMatcher
  • Object
show all
Defined in:
lib/moarspec/matchers/dont.rb

Defined Under Namespace

Classes: Delegator

Instance Method Summary collapse

Constructor Details

#initializeNot

Returns a new instance of Not.



7
8
9
10
# File 'lib/moarspec/matchers/dont.rb', line 7

def initialize(*)
  super
  @delegator = Delegator.new
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(m, *a) ⇒ Object



39
40
41
42
43
44
45
46
47
# File 'lib/moarspec/matchers/dont.rb', line 39

def method_missing(m, *a, &)
  if @matcher
    @matcher.send(m, *a, &)
  else
    @matcher = @delegator.send(m, *a, &)
  end

  self
end

Instance Method Details

#descriptionObject



12
13
14
# File 'lib/moarspec/matchers/dont.rb', line 12

def description
  "not #{@matcher.description}"
end

#failure_messageObject



31
32
33
# File 'lib/moarspec/matchers/dont.rb', line 31

def failure_message
  @matcher.failure_message_when_negated
end

#match(_expected, actual) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/moarspec/matchers/dont.rb', line 16

def match(_expected, actual)
  @matcher or fail ArgumentError, '`dont` matcher used without any matcher to negate. ' \
                                  'Usage: dont.other_matcher(args)'

  # https://www.rubydoc.info/github/rspec/rspec-expectations/RSpec%2FMatchers%2FMatcherProtocol:does_not_match%3F
  # In a negative expectation such as `expect(x).not_to foo`, RSpec will call
  # `foo.does_not_match?(x)` if this method is defined. If it's not defined it
  # will fall back to using `!foo.matches?(x)`.
  if @matcher.respond_to?(:does_not_match?)
    @matcher.does_not_match?(actual)
  else
    !@matcher.matches?(actual)
  end
end

#respond_to_missing?(method, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


49
50
51
52
53
54
55
# File 'lib/moarspec/matchers/dont.rb', line 49

def respond_to_missing?(method, include_private = false)
  if @matcher
    @matcher.respond_to?(method, include_private)
  else
    @delegator.respond_to_missing?(method, include_private)
  end
end

#supports_block_expectations?Boolean

Returns:

  • (Boolean)


35
36
37
# File 'lib/moarspec/matchers/dont.rb', line 35

def supports_block_expectations?
  @matcher.supports_block_expectations?
end

#withObject



62
63
64
65
66
67
68
69
70
# File 'lib/moarspec/matchers/dont.rb', line 62

def with(...)
  if @matcher
    @matcher.with(...)
  else
    @matcher = @delegator.with(...)
  end

  self
end