Sha256: 66e142adf4a461e20d20d77d9a33c7803ff2cd13a9303e0fc29ed2744ddc1ec9

Contents?: true

Size: 1.6 KB

Versions: 8

Compression:

Stored size: 1.6 KB

Contents

# frozen_string_literal: true

module Matchers
  module Type
    def have_nullable_date(attribute, valid_value, invalid_value)
      HaveNullableDateMatcher.new(attribute, valid_value, invalid_value)
    end

    class HaveNullableDateMatcher < HaveNullableMatcher
      def initialize(attribute, valid_value, invalid_value)
        @attribute = attribute
        @valid_value = valid_value
        @invalid_value = invalid_value
        @expected_error = ArgumentError
        @expected_error_message = 'invalid date'
        @failure_description = ''
      end

      def matches?(klass)
        @klass = klass

        accepts_nil? && accepts_valid_value? && rejects_invalid_value?
      end

      def description
        "have nullable attribute #{@attribute.inspect}"
      end

      def failure_message
        "Expected class to have nullable attribute #{@attribute.inspect}" << @failure_description
      end

      private

      def accepts_nil?
        @klass.new(@attribute => nil)
      end

      def accepts_valid_value?
        model = @klass.new(@attribute => @valid_value)
        model.send(@attribute) == @valid_value
      end

      def rejects_invalid_value?
        @klass.new(@attribute => @invalid_value)

        @failure_description << " (Expected #{@expected_error}, but got none)"
        false
      rescue @expected_error => error
        return true if error.message == @expected_error_message

        fail_message = "Expected error message to include #{expected_message.inspect}, "\
                       "but was #{error.message.inspect}"
        raise(fail_message)
      end
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
fortnox-api-0.8.0 spec/support/matchers/type/have_nullable_date_matcher.rb
fortnox-api-0.7.2 spec/support/matchers/type/have_nullable_date_matcher.rb
fortnox-api-0.7.1 spec/support/matchers/type/have_nullable_date_matcher.rb
fortnox-api-0.7.0 spec/support/matchers/type/have_nullable_date_matcher.rb
fortnox-api-0.6.3 spec/support/matchers/type/have_nullable_date_matcher.rb
fortnox-api-0.6.2 spec/support/matchers/type/have_nullable_date_matcher.rb
fortnox-api-0.6.1 spec/support/matchers/type/have_nullable_date_matcher.rb
fortnox-api-0.6.0 spec/support/matchers/type/have_nullable_date_matcher.rb