Sha256: 46c6d3d3beafd9668a9e855d06422568e179a4784cf34c6686262563d2e0cee0

Contents?: true

Size: 1.28 KB

Versions: 6

Compression:

Stored size: 1.28 KB

Contents

module RR
  module Expectations
    class TimesCalledExpectation #:nodoc:
      attr_reader :double, :times_called
      attr_accessor :matcher

      def initialize(double, matcher=nil)
        @double = double
        @matcher = matcher
        @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
        "#{double.formatted_name}\n#{@matcher.error_message(@times_called)}"
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
rr-0.4.5 lib/rr/expectations/times_called_expectation.rb
rr-0.4.3 lib/rr/expectations/times_called_expectation.rb
rr-0.4.6 lib/rr/expectations/times_called_expectation.rb
rr-0.4.4 lib/rr/expectations/times_called_expectation.rb
rr-0.4.7 lib/rr/expectations/times_called_expectation.rb
rr-0.4.8 lib/rr/expectations/times_called_expectation.rb