Sha256: 273ba8744634f8dd2ff0c7b1c0ddc899cceb135a9dcaf0a79c1eee3611156543

Contents?: true

Size: 1.1 KB

Versions: 3

Compression:

Stored size: 1.1 KB

Contents

module RR
  module Expectations
    class TimesCalledExpectation
      attr_reader :times, :times_called
      
      def initialize(times=nil, &time_condition_block)
        raise ArgumentError, "Cannot pass in both an argument and a block" if times && time_condition_block
        @times = times || time_condition_block
        @times_called = 0
      end

      def verify_input
        @times_called += 1
        verify_input_error if @times.is_a?(Integer) && @times_called > @times
        verify_input_error if @times.is_a?(Range) && @times_called > @times.end
        return
      end

      def verify
        return true if @times.is_a?(Integer) && @times == @times_called
        return true if @times.is_a?(Proc) && @times.call(@times_called)
        return true if @times.is_a?(Range) && @times.include?(@times_called)
        return false
      end

      def verify!
        raise Errors::TimesCalledError unless verify
      end

      protected
      def verify_input_error
        raise Errors::TimesCalledError, "Called #{@times_called.inspect} times. Expected #{@times.inspect}"
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
rr-0.1.2 lib/rr/expectations/times_called_expectation.rb
rr-0.1.1 lib/rr/expectations/times_called_expectation.rb
rr-0.1.3 lib/rr/expectations/times_called_expectation.rb