Sha256: 3ef16b066769d4548e37fbba6c668389470d5dc41200019142eb7833320e9972

Contents?: true

Size: 1.71 KB

Versions: 23

Compression:

Stored size: 1.71 KB

Contents

#!/usr/bin/env ruby

#---
# Copyright 2003-2013 by Jim Weirich (jim.weirich@gmail.com).
# All rights reserved.
#
# Permission is granted for use, copying, modification, distribution,
# and distribution of modified versions of this work as long as the
# above copyright notice is included.
#+++

class FlexMock

  # A CallValidator checks the list of call records for a particular
  # method name and arguments.
  class CallValidator

    # Does the +calls+ list record a method named +method_name+ with
    # +args+.  Options include:
    #
    # * :times => n    -- If given, the call should match exactly +n+ times.
    # * :and => []     -- A list of argument validations to be run on each
    #                     matching invocation.
    # * :on_count => n -- If given, the :and validations on only run on the
    #                     nth invocation.
    #
    def received?(calls, method_name, args, options)
      count = 0
      calls.each { |call_record|
        if call_record.matches?(method_name, args, options)
          count += 1
          run_additional_validations(call_record, count, options)
        end
      }
      count_matches?(count, options[:times])
    end

    private

    def additionals(options)
      ands = options[:and]
      if ands.nil?
        []
      elsif ands.is_a?(Proc)
        [ands]
      else
        ands
      end
    end

    def run_additional_validations(call_record, count, options)
      if options[:on_count].nil? || count == options[:on_count]
        additionals(options).each do |add|
          add.call(*call_record.args)
        end
      end
    end

    def count_matches?(count, times)
      if times
        count == times
      else
        count > 0
      end
    end
  end

end

Version data entries

23 entries across 23 versions & 1 rubygems

Version Path
flexmock-2.4.2 lib/flexmock/call_validator.rb
flexmock-2.4.1 lib/flexmock/call_validator.rb
flexmock-2.4.0 lib/flexmock/call_validator.rb
flexmock-2.3.8 lib/flexmock/call_validator.rb
flexmock-2.3.6 lib/flexmock/call_validator.rb
flexmock-2.3.5 lib/flexmock/call_validator.rb
flexmock-2.3.4 lib/flexmock/call_validator.rb
flexmock-2.3.3 lib/flexmock/call_validator.rb
flexmock-2.3.2 lib/flexmock/call_validator.rb
flexmock-2.3.1 lib/flexmock/call_validator.rb
flexmock-2.3.0 lib/flexmock/call_validator.rb
flexmock-2.2.1 lib/flexmock/call_validator.rb
flexmock-2.2.0 lib/flexmock/call_validator.rb
flexmock-2.1.0 lib/flexmock/call_validator.rb
flexmock-2.0.6 lib/flexmock/call_validator.rb
flexmock-2.0.5 lib/flexmock/call_validator.rb
flexmock-2.0.4 lib/flexmock/call_validator.rb
flexmock-2.0.3 lib/flexmock/call_validator.rb
flexmock-2.0.2 lib/flexmock/call_validator.rb
flexmock-2.0.1 lib/flexmock/call_validator.rb