Sha256: 7435017007e1e27322e4340f9b2bef35350937422b3ecd490a158bd9895a5db0

Contents?: true

Size: 1.37 KB

Versions: 8

Compression:

Stored size: 1.37 KB

Contents

# frozen_string_literal: true

module Matchers
  module Type
    def have_nullable_string(attribute, valid_hash = {})
      HaveNullableStringMatcher.new(attribute, valid_hash)
    end

    class HaveNullableStringMatcher < AttributeMatcher
      def initialize(attribute, valid_hash)
        super(attribute, valid_hash, 'nullable string')
      end

      def matches?(klass)
        super

        accepts_nil? && accepts_string? && rejects_non_string?
      end

      private

      def accepts_nil?
        model = @klass.new(@valid_hash.merge(@attribute => nil))
        model.send(@attribute).nil?
      end

      def accepts_string?
        valid_string = 'A string'
        model = @klass.new(@valid_hash.merge(@attribute => valid_string))
        model.send(@attribute) == 'A string'
      end

      def rejects_non_string?
        non_string = 10.0
        @klass.new(@valid_hash.merge(@attribute => non_string))
      rescue Fortnox::API::InvalidAttributeValueError => error
        expected_message = "#{non_string.inspect} (#{non_string.class}) "\
                           "has invalid type for #{@attribute.inspect}"
        return true if error.message.include?(expected_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_string_matcher.rb
fortnox-api-0.7.2 spec/support/matchers/type/have_nullable_string_matcher.rb
fortnox-api-0.7.1 spec/support/matchers/type/have_nullable_string_matcher.rb
fortnox-api-0.7.0 spec/support/matchers/type/have_nullable_string_matcher.rb
fortnox-api-0.6.3 spec/support/matchers/type/have_nullable_string_matcher.rb
fortnox-api-0.6.2 spec/support/matchers/type/have_nullable_string_matcher.rb
fortnox-api-0.6.1 spec/support/matchers/type/have_nullable_string_matcher.rb
fortnox-api-0.6.0 spec/support/matchers/type/have_nullable_string_matcher.rb