Sha256: a3ed3d6ab1cc01a229eaae66c61c49bbe0654aa6164fadca4be003b5bea6d892

Contents?: true

Size: 1.42 KB

Versions: 11

Compression:

Stored size: 1.42 KB

Contents

module RR
  module Expectations
    class TimesCalledExpectation
      attr_reader :matcher, :times_called
      
      def initialize(matcher=nil, &time_condition_block)
        raise ArgumentError, "Cannot pass in both an argument and a block" if matcher && time_condition_block
        matcher_value = matcher || time_condition_block
        @matcher = TimesCalledMatchers::TimesCalledMatcher.create(matcher_value)
        @times_called = 0
        @verify_backtrace = caller[1..-1]
      end

      def attempt?
        @matcher.attempt?(@times_called)
      end

      def attempt!
        @times_called += 1
        verify_input_error unless @matcher.possible_match?(@times_called)
        return
      end

      def verify
        return false unless @matcher.is_a?(TimesCalledMatchers::TimesCalledMatcher)
        return @matcher.matches?(@times_called)
      end

      def verify!
        unless verify
          if @verify_backtrace
            error = Errors::TimesCalledError.new(error_message)
            error.backtrace = @verify_backtrace
            raise error
          else
            raise Errors::TimesCalledError, error_message
          end
        end
      end

      def terminal?
        @matcher.terminal?
      end

      protected
      def verify_input_error
        raise Errors::TimesCalledError, error_message
      end

      def error_message
        @matcher.error_message(@times_called)
      end
    end
  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
rr-0.1.14 lib/rr/expectations/times_called_expectation.rb
rr-0.1.15 lib/rr/expectations/times_called_expectation.rb
rr-0.2.3 lib/rr/expectations/times_called_expectation.rb
rr-0.3.2 lib/rr/expectations/times_called_expectation.rb
rr-0.2.1 lib/rr/expectations/times_called_expectation.rb
rr-0.3.3 lib/rr/expectations/times_called_expectation.rb
rr-0.3.0 lib/rr/expectations/times_called_expectation.rb
rr-0.2.5 lib/rr/expectations/times_called_expectation.rb
rr-0.2.4 lib/rr/expectations/times_called_expectation.rb
rr-0.3.1 lib/rr/expectations/times_called_expectation.rb
rr-0.2.2 lib/rr/expectations/times_called_expectation.rb