Sha256: 596fd55447d6858bff267943a583d759cc4d94ea836236450b9a22c2f390a157

Contents?: true

Size: 1.37 KB

Versions: 2

Compression:

Stored size: 1.37 KB

Contents

require 'timeout'

module RSpec
  module BecomeMatcher
    class Matcher
      DEFAULT_LIMIT = 3
      attr_reader :expected, :limit, :current, :expected_block, :was

      def initialize(expected, &block)
        @expected = expected
        @expected_block = block
        @limit = DEFAULT_LIMIT
      end

      def matches?(block)
        Timeout.timeout(limit) do
          @was = @current = block.call
          @current = begin
                       sleep 0.1
                       block.call
                     end until matching_now?
          true
        end
      rescue Timeout::Error
        false
      end

      def matching_now?
        if expected_block
          expected_block.call(current)
        else
          current == expected
        end
      end

      def in(limit)
        @limit = limit
        self
      end

      def failure_message
        <<~MESSAGE
        Expected #{was} to become #{expected || 'given block'} in #{limit.to_i} second, but not
        MESSAGE
      end

      def failure_message_when_negated
        <<~MESSAGE
        Expected #{was} not to become #{expected || 'given block'} in #{limit.to_i} second
        MESSAGE
      end

      def supports_block_expectations?
        true
      end
    end

    def become(expected=nil, &block)
      # TODO raise with no expected nor block
      Matcher.new(expected, &block)
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
rspec-become-matcher-0.1.1 lib/rspec/become_matcher.rb
rspec-become-matcher-0.1.0 lib/rspec/become_matcher.rb