Class: RSpec::Mocks::ArgumentExpectation

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args, &block) ⇒ ArgumentExpectation

Returns a new instance of ArgumentExpectation.



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

def initialize(*args, &block)
  @args = args
  @matchers_block = args.empty? ? block : nil
  @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.



4
5
6
# File 'lib/rspec/mocks/argument_expectation.rb', line 4

def args
  @args
end

Instance Method Details

#args_match?(*args) ⇒ Boolean

Returns:

  • (Boolean)


31
32
33
# File 'lib/rspec/mocks/argument_expectation.rb', line 31

def args_match?(*args)
  match_any_args? || matchers_block_matches?(*args) || matchers_match?(*args)
end

#is_matcher?(obj) ⇒ Boolean

Returns:

  • (Boolean)


27
28
29
# File 'lib/rspec/mocks/argument_expectation.rb', line 27

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

#match_any_args?Boolean

Returns:

  • (Boolean)


43
44
45
# File 'lib/rspec/mocks/argument_expectation.rb', line 43

def match_any_args?
  @match_any_args
end

#matcher_for(arg) ⇒ Object



21
22
23
24
25
# File 'lib/rspec/mocks/argument_expectation.rb', line 21

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?(*args) ⇒ Boolean

Returns:

  • (Boolean)


35
36
37
# File 'lib/rspec/mocks/argument_expectation.rb', line 35

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

#matchers_match?(*args) ⇒ Boolean

Returns:

  • (Boolean)


39
40
41
# File 'lib/rspec/mocks/argument_expectation.rb', line 39

def matchers_match?(*args)
  @matchers == args
end