Class: RSpec::Matchers::DelegateMatcher::Delegation
- Inherits:
-
Object
- Object
- RSpec::Matchers::DelegateMatcher::Delegation
- Defined in:
- lib/delegate_matcher/delegation.rb
Overview
rubocop:disable Metrics/ClassLength
Instance Attribute Summary collapse
-
#delegate ⇒ Object
Returns the value of attribute delegate.
-
#dispatcher ⇒ Object
Returns the value of attribute dispatcher.
-
#expected ⇒ Object
Returns the value of attribute expected.
Instance Method Summary collapse
- #allow_nil_failure_message(negated) ⇒ Object
- #allow_nil_ok? ⇒ Boolean
- #argument_failure_message(negated) ⇒ Object
- #arguments_ok? ⇒ Boolean
- #block_failure_message(negated) ⇒ Object
- #block_ok? ⇒ Boolean
- #delegate_return_value ⇒ Object
- #delegation_ok? ⇒ Boolean
- #failure_message(negated) ⇒ Object
-
#initialize(expected) ⇒ Delegation
constructor
A new instance of Delegation.
- #ok? ⇒ Boolean
- #return_value_failure_message(negated) ⇒ Object
- #return_value_ok? ⇒ Boolean
Constructor Details
#initialize(expected) ⇒ Delegation
Returns a new instance of Delegation.
13 14 15 16 17 |
# File 'lib/delegate_matcher/delegation.rb', line 13 def initialize(expected) self.expected = expected self.dispatcher = DelegateMatcher::Dispatcher.new(expected) self.delegate = expected.to.map { |to| StubDelegate.new(expected, to) } end |
Instance Attribute Details
#delegate ⇒ Object
Returns the value of attribute delegate.
11 12 13 |
# File 'lib/delegate_matcher/delegation.rb', line 11 def delegate @delegate end |
#dispatcher ⇒ Object
Returns the value of attribute dispatcher.
10 11 12 |
# File 'lib/delegate_matcher/delegation.rb', line 10 def dispatcher @dispatcher end |
#expected ⇒ Object
Returns the value of attribute expected.
9 10 11 |
# File 'lib/delegate_matcher/delegation.rb', line 9 def expected @expected end |
Instance Method Details
#allow_nil_failure_message(negated) ⇒ Object
122 123 124 125 126 127 128 129 130 131 |
# File 'lib/delegate_matcher/delegation.rb', line 122 def (negated) case when expected.allow_nil.nil? || negated ^ allow_nil_ok? '' when negated %("#{expected.to_description}" was #{expected.allow_nil ? '' : 'not '}allowed to be nil) else %("#{expected.to_description}" was #{expected.allow_nil ? 'not ' : ''}allowed to be nil) end end |
#allow_nil_ok? ⇒ Boolean
28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/delegate_matcher/delegation.rb', line 28 def allow_nil_ok? return true if expected.allow_nil.nil? begin expected.to.each { |to| NilDelegate.new(expected, to) { dispatcher.call } } allow_nil = true rescue NoMethodError allow_nil = false end allow_nil == expected.allow_nil end |
#argument_failure_message(negated) ⇒ Object
87 88 89 90 91 92 93 94 |
# File 'lib/delegate_matcher/delegation.rb', line 87 def (negated) case when expected.as_args.nil? || negated ^ arguments_ok? '' else "was called with #{delegate[0].argument_description}" end end |
#arguments_ok? ⇒ Boolean
41 42 43 44 |
# File 'lib/delegate_matcher/delegation.rb', line 41 def arguments_ok? args_matcher = Mocks::ArgumentListMatcher.new(*expected.as_args) delegate.all? { |delegate| args_matcher.args_match?(*delegate.args) } end |
#block_failure_message(negated) ⇒ Object
96 97 98 99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/delegate_matcher/delegation.rb', line 96 def (negated) proc_source = ProcSource.new(delegate[0].block) case when expected.block.nil? || negated ^ block_ok? '' when negated "a block was #{expected.block ? '' : 'not '}passed" when expected.block delegate.all? { |d| d.block.nil? } ? 'a block was not passed' : "a different block '#{proc_source}' was passed" else %(a block #{proc_source} was passed) end end |
#block_ok? ⇒ Boolean
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/delegate_matcher/delegation.rb', line 46 def block_ok? expected_block = expected.block return true if expected_block.nil? delegate.all? do |d| case when expected_block == false d.block.nil? when expected_block == true d.block == dispatcher.block else d.block_source == expected.block_source end end end |
#delegate_return_value ⇒ Object
73 74 75 |
# File 'lib/delegate_matcher/delegation.rb', line 73 def delegate_return_value delegate.length == 1 ? delegate[0].return_value : delegate.map(&:return_value) end |
#delegation_ok? ⇒ Boolean
19 20 21 22 |
# File 'lib/delegate_matcher/delegation.rb', line 19 def delegation_ok? dispatcher.call delegate.all?(&:received) end |
#failure_message(negated) ⇒ Object
77 78 79 80 81 82 83 84 85 |
# File 'lib/delegate_matcher/delegation.rb', line 77 def (negated) = [ (negated), (negated), (negated), (negated) ].reject(&:empty?).join(' and ') .empty? ? nil : end |
#ok? ⇒ Boolean
24 25 26 |
# File 'lib/delegate_matcher/delegation.rb', line 24 def ok? allow_nil_ok? && delegation_ok? && arguments_ok? && block_ok? && return_value_ok? end |
#return_value_failure_message(negated) ⇒ Object
111 112 113 114 115 116 117 118 119 120 |
# File 'lib/delegate_matcher/delegation.rb', line 111 def (negated) case when !delegate.any?(&:received) || negated ^ return_value_ok? '' when negated '' else "a value of \"#{dispatcher.return_value}\" was returned instead of \"#{expected.return_value || delegate_return_value}\"" end end |
#return_value_ok? ⇒ Boolean
62 63 64 65 66 67 68 69 70 71 |
# File 'lib/delegate_matcher/delegation.rb', line 62 def return_value_ok? case when !expected.check_return true when expected.return_value.nil? dispatcher.return_value == delegate_return_value else dispatcher.return_value == expected.return_value end end |