Class: Spec::Mocks::ArgumentExpectation

Inherits:
Object
  • Object
show all
Defined in:
lib/spec/mocks/argument_expectation.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args, &block) ⇒ ArgumentExpectation

Returns a new instance of ArgumentExpectation.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/spec/mocks/argument_expectation.rb', line 7

def initialize(args, &block)
  @args = args
  @matchers_block = block
  @match_any_args = false
  @matchers = nil
  
  if ArgumentMatchers::AnyArgsMatcher === args.first
    @match_any_args = true
  elsif ArgumentMatchers::NoArgsMatcher === args.first
    @matchers = []
  else
    @matchers = args.collect {|arg| matcher_for(arg)}
  end
end

Instance Attribute Details

#argsObject (readonly)

Returns the value of attribute args.



5
6
7
# File 'lib/spec/mocks/argument_expectation.rb', line 5

def args
  @args
end

Instance Method Details

#args_match?(given_args) ⇒ Boolean

Returns:

  • (Boolean)


32
33
34
# File 'lib/spec/mocks/argument_expectation.rb', line 32

def args_match?(given_args)
  match_any_args? || matchers_block_matches?(given_args) || matchers_match?(given_args)
end

#is_matcher?(obj) ⇒ Boolean

Returns:

  • (Boolean)


28
29
30
# File 'lib/spec/mocks/argument_expectation.rb', line 28

def is_matcher?(obj)
  return obj.respond_to?(:matches?) & obj.respond_to?(:description)
end

#match_any_args?Boolean

Returns:

  • (Boolean)


44
45
46
# File 'lib/spec/mocks/argument_expectation.rb', line 44

def match_any_args?
  @match_any_args
end

#matcher_for(arg) ⇒ Object



22
23
24
25
26
# File 'lib/spec/mocks/argument_expectation.rb', line 22

def matcher_for(arg)
  return ArgumentMatchers::MatcherMatcher.new(arg)   if is_matcher?(arg)
  return ArgumentMatchers::RegexpMatcher.new(arg) if arg.is_a?(Regexp)
  return ArgumentMatchers::EqualityProxy.new(arg)
end

#matchers_block_matches?(given_args) ⇒ Boolean

Returns:

  • (Boolean)


36
37
38
# File 'lib/spec/mocks/argument_expectation.rb', line 36

def matchers_block_matches?(given_args)
  @matchers_block ? @matchers_block.call(*given_args) : nil
end

#matchers_match?(given_args) ⇒ Boolean

Returns:

  • (Boolean)


40
41
42
# File 'lib/spec/mocks/argument_expectation.rb', line 40

def matchers_match?(given_args)
  @matchers == given_args
end