Sha256: 7233ba9027a401666e9b951c9cead803777a0341c4572e2ce92a0f2d7f24cd0b

Contents?: true

Size: 917 Bytes

Versions: 2

Compression:

Stored size: 917 Bytes

Contents

require 'not_a_mock/matchers/call_matcher'

module NotAMock
  module Matchers
    # Matcher for +once+, +twice+, and
    #   exactly(n).times
    class TimesMatcher < CallMatcher
  
      def initialize(times, parent = nil)
        super parent
        @times = times
      end
  
      def matches_without_parents?
        @calls = @parent.calls
        @calls.length == @times
      end
  
      def failure_message_without_parents
        if matched?
          ", #{times_in_english(@parent.calls.length)}"
        else
          ", but #{times_in_english(@parent.calls.length, true)}"
        end
      end
  
      def times
        self
      end
  
    private

      def times_in_english(times, only = false)
        case times
          when 1
            only ? "only once" : "once"
          when 2
            "twice"
          else
            "#{times} times"
        end
      end
  
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
not_a_mock-1.0.1 lib/not_a_mock/matchers/times_matcher.rb
not_a_mock-1.0.0 lib/not_a_mock/matchers/times_matcher.rb