Sha256: 4aeaf2532ccda063eb04ab62aa1325d3a33a7b594927b924caa4840950f9b84c

Contents?: true

Size: 1.32 KB

Versions: 18

Compression:

Stored size: 1.32 KB

Contents

module Rspec
  module Mocks
    
    class ArgumentExpectation
      attr_reader :args
      
      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
      
      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
      
      def is_matcher?(obj)
        return obj.respond_to?(:matches?) & obj.respond_to?(:description)
      end
      
      def args_match?(given_args)
        match_any_args? || matchers_block_matches?(given_args) || matchers_match?(given_args)
      end
      
      def matchers_block_matches?(given_args)
        @matchers_block ? @matchers_block.call(*given_args) : nil
      end
      
      def matchers_match?(given_args)
        @matchers == given_args
      end
      
      def match_any_args?
        @match_any_args
      end
      
    end
    
  end
end

Version data entries

18 entries across 18 versions & 1 rubygems

Version Path
rspec-mocks-2.0.0.beta.8 lib/rspec/mocks/argument_expectation.rb
rspec-mocks-2.0.0.beta.7 lib/rspec/mocks/argument_expectation.rb
rspec-mocks-2.0.0.beta.6 lib/rspec/mocks/argument_expectation.rb
rspec-mocks-2.0.0.beta.5 lib/rspec/mocks/argument_expectation.rb
rspec-mocks-2.0.0.beta.4 lib/rspec/mocks/argument_expectation.rb
rspec-mocks-2.0.0.beta.3 lib/rspec/mocks/argument_expectation.rb
rspec-mocks-2.0.0.beta.2 lib/rspec/mocks/argument_expectation.rb
rspec-mocks-2.0.0.beta.1 lib/rspec/mocks/argument_expectation.rb
rspec-mocks-2.0.0.a10 lib/rspec/mocks/argument_expectation.rb
rspec-mocks-2.0.0.a9 lib/rspec/mocks/argument_expectation.rb
rspec-mocks-2.0.0.a8 lib/rspec/mocks/argument_expectation.rb
rspec-mocks-2.0.0.a7 lib/rspec/mocks/argument_expectation.rb
rspec-mocks-2.0.0.a6 lib/rspec/mocks/argument_expectation.rb
rspec-mocks-2.0.0.a5 lib/rspec/mocks/argument_expectation.rb
rspec-mocks-2.0.0.a4 lib/rspec/mocks/argument_expectation.rb
rspec-mocks-2.0.0.a3 lib/rspec/mocks/argument_expectation.rb
rspec-mocks-2.0.0.a2 lib/rspec/mocks/argument_expectation.rb
rspec-mocks-2.0.0.a1 lib/rspec/mocks/argument_expectation.rb