class RSpec::Bash::Mocks::Matchers::BaseMatcher

@private

Attributes

double[R]

Public Class Methods

new() click to toggle source
# File lib/rspec/bash/mocks/matchers/base_matcher.rb, line 13
def initialize()
  fail "@double must be created by implementation" if @double.nil?
  fail "@display_name must be specified by implementation" if @display_name.nil?
end

Public Instance Methods

and_always_return(code) click to toggle source
# File lib/rspec/bash/mocks/matchers/base_matcher.rb, line 44
def and_always_return(code)
  and_return(code, times: Float::INFINITY)
end
and_always_yield(subshell: true, &body) click to toggle source
# File lib/rspec/bash/mocks/matchers/base_matcher.rb, line 48
def and_always_yield(subshell: true, &body)
  and_yield(subshell: subshell, times: Float::INFINITY, &body)
end
and_call_original() click to toggle source
# File lib/rspec/bash/mocks/matchers/base_matcher.rb, line 74
def and_call_original
  tap { @double.call_original = true }
end
and_return(code, times: 1) click to toggle source
# File lib/rspec/bash/mocks/matchers/base_matcher.rb, line 40
def and_return(code, times: 1)
  and_yield(subshell: false, times: times) { |*| "return #{code}" }
end
and_yield(subshell: true, times: 1, &body) click to toggle source
# File lib/rspec/bash/mocks/matchers/base_matcher.rb, line 29
def and_yield(subshell: true, times: 1, &body)
  tap {
    @double.subshell = subshell

    behavior = find_last_blank_or_create_behavior
    behavior[:body] = body
    behavior[:charges] = behavior[:charges] == 0 ? times : behavior[:charges]
    behavior[:subshell] = subshell
  }
end
at_least(n) click to toggle source
# File lib/rspec/bash/mocks/matchers/base_matcher.rb, line 66
def at_least(n)
  tap { @double.expected_call_count = [:at_least, n] }
end
at_most(n) click to toggle source
# File lib/rspec/bash/mocks/matchers/base_matcher.rb, line 70
def at_most(n)
  tap { @double.expected_call_count = [:at_most, n] }
end
exactly(n) click to toggle source
# File lib/rspec/bash/mocks/matchers/base_matcher.rb, line 52
def exactly(n)
  tap do
    if @double.behaviors.last
      @double.behaviors.last[:charges] = n

      (n-1).times do
        @double.expected_calls << @double.expected_calls.last
      end
    else
      @double.expected_call_count = [:exactly, n]
    end
  end
end
matches?(subject, &block) click to toggle source

@private

(RSpec::Bash::Script): RSpec::Bash::Mocks::ScriptMessageExpectation

# File lib/rspec/bash/mocks/matchers/base_matcher.rb, line 101
def matches?(subject, &block)
  proxy_for(subject).expect_message(
    double: @double,
    display_name: @display_name
  )
end
name() click to toggle source
# File lib/rspec/bash/mocks/matchers/base_matcher.rb, line 18
def name
  @display_name
end
never() click to toggle source
# File lib/rspec/bash/mocks/matchers/base_matcher.rb, line 78
def never
  exactly(0)
end
once() click to toggle source
# File lib/rspec/bash/mocks/matchers/base_matcher.rb, line 82
def once
  exactly(1)
end
setup_allowance(subject, &block) click to toggle source

@private

(RSpec::Bash::Script): RSpec::Bash::Mocks::ScriptMessageExpectation

# File lib/rspec/bash/mocks/matchers/base_matcher.rb, line 111
def setup_allowance(subject, &block)
  proxy_for(subject).allow_message(
    double: @double
  )
end
thrice() click to toggle source
# File lib/rspec/bash/mocks/matchers/base_matcher.rb, line 90
def thrice
  exactly(3)
end
times() click to toggle source
# File lib/rspec/bash/mocks/matchers/base_matcher.rb, line 94
def times
  self
end
twice() click to toggle source
# File lib/rspec/bash/mocks/matchers/base_matcher.rb, line 86
def twice
  exactly(2)
end
with_args(args) click to toggle source
# File lib/rspec/bash/mocks/matchers/base_matcher.rb, line 22
def with_args(args)
  tap {
    @double.expected_calls << args
    @double.behaviors << create_behavior({ args: args })
  }
end

Protected Instance Methods

create_behavior(args: nil, body: nil, charges: 0) click to toggle source
# File lib/rspec/bash/mocks/matchers/base_matcher.rb, line 129
def create_behavior(args: nil, body: nil, charges: 0)
  {
    args: args,
    body: body,
    charges: charges
  }
end
find_last_blank_or_create_behavior() click to toggle source
# File lib/rspec/bash/mocks/matchers/base_matcher.rb, line 123
def find_last_blank_or_create_behavior
  @double.behaviors.detect { |x| x[:body].nil? } || begin
    create_behavior.tap { |x| @double.behaviors << x }
  end
end
proxy_for(subject) click to toggle source
# File lib/rspec/bash/mocks/matchers/base_matcher.rb, line 119
def proxy_for(subject)
  ::RSpec::Mocks.space.proxy_for(subject)
end