Sha256: f7d6dc3e35a6626da9a2abd686b9528aef8ba29601ce29a87a1a5a0b982897f9

Contents?: true

Size: 947 Bytes

Versions: 12

Compression:

Stored size: 947 Bytes

Contents

# frozen_string_literal: true

module Matchers
  module Type
    def have_sized_string(attribute, max_size, valid_hash = {})
      HaveSizedStringMatcher.new(attribute, max_size, valid_hash)
    end

    class HaveSizedStringMatcher < AttributeMatcher
      def initialize(attribute, max_size, valid_hash)
        super(attribute, valid_hash, 'sized string')
        @max_size = max_size
      end

      def matches?(klass)
        @klass = klass

        accepts_max_size? && rejects_too_long_string?
      end

      private

      def accepts_max_size?
        @klass.new(@valid_hash.merge(@attribute => 'a' * @max_size))
      end

      def rejects_too_long_string?
        too_long = @max_size + 1
        too_long_string = 'a' * too_long
        expect_error("Exception missing for too long string (#{too_long} characters)") do
          @klass.new(@valid_hash.merge(@attribute => too_long_string))
        end
      end
    end
  end
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
fortnox-api-0.9.1 spec/support/matchers/type/have_sized_string_matcher.rb
fortnox-api-0.8.2 spec/support/matchers/type/have_sized_string_matcher.rb
fortnox-api-0.8.1 spec/support/matchers/type/have_sized_string_matcher.rb
fortnox-api-0.9.0 spec/support/matchers/type/have_sized_string_matcher.rb
fortnox-api-0.8.0 spec/support/matchers/type/have_sized_string_matcher.rb
fortnox-api-0.7.2 spec/support/matchers/type/have_sized_string_matcher.rb
fortnox-api-0.7.1 spec/support/matchers/type/have_sized_string_matcher.rb
fortnox-api-0.7.0 spec/support/matchers/type/have_sized_string_matcher.rb
fortnox-api-0.6.3 spec/support/matchers/type/have_sized_string_matcher.rb
fortnox-api-0.6.2 spec/support/matchers/type/have_sized_string_matcher.rb
fortnox-api-0.6.1 spec/support/matchers/type/have_sized_string_matcher.rb
fortnox-api-0.6.0 spec/support/matchers/type/have_sized_string_matcher.rb