Sha256: 638e2f4cf7fbaf34b7d60f5489a2ded5494ff5f7574643cf12fe566b8a934b7f

Contents?: true

Size: 957 Bytes

Versions: 6

Compression:

Stored size: 957 Bytes

Contents

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

6 entries across 6 versions & 1 rubygems

Version Path
fortnox-api-0.5.2 spec/support/matchers/type/have_sized_string_matcher.rb
fortnox-api-0.5.1 spec/support/matchers/type/have_sized_string_matcher.rb
fortnox-api-0.5.0 spec/support/matchers/type/have_sized_string_matcher.rb
fortnox-api-0.4.0 spec/support/matchers/type/have_sized_string_matcher.rb
fortnox-api-0.3.0 spec/support/matchers/type/have_sized_string_matcher.rb
fortnox-api-0.2.0 spec/support/matchers/type/have_sized_string_matcher.rb