Sha256: 03eff9883fef61f92fd30f4100ab30c4c75e5e387ec5af0c08456df474c71ea6

Contents?: true

Size: 975 Bytes

Versions: 6

Compression:

Stored size: 975 Bytes

Contents

module Matchers
  module Type
    class TypeMatcher < AttributeMatcher
      def initialize( attr, valid_hash, attr_name, valid_value, invalid_value )
        super( attr, valid_hash, attr_name )

        @valid_value = valid_value
        @invalid_value = invalid_value
      end

      def matches?( klass )
        super

        correct_type? && rejects_invalid_value? && accepts_valid_value?
      end

      private

        def correct_type?
          return true if expected_type?

          @errors << "Attribute #{ @attribute.inspect } was expected to be of type #{ @attribute_type }, but was #{ @actual_type }"
          return false
        end

        def accepts_valid_value?
          @klass.new( @valid_hash.merge( @attribute => @valid_value ) )
        end

        def rejects_invalid_value?
          expect_error(@expected_error) do
            @klass.new( @valid_hash.merge( @attribute => @invalid_value ) )
          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/type_matcher.rb
fortnox-api-0.5.1 spec/support/matchers/type/type_matcher.rb
fortnox-api-0.5.0 spec/support/matchers/type/type_matcher.rb
fortnox-api-0.4.0 spec/support/matchers/type/type_matcher.rb
fortnox-api-0.3.0 spec/support/matchers/type/type_matcher.rb
fortnox-api-0.2.0 spec/support/matchers/type/type_matcher.rb