Sha256: 08a7d9254f74594d36eed9111fbdd529307379203d4d77218f4afa8af03f30f1

Contents?: true

Size: 1.19 KB

Versions: 3

Compression:

Stored size: 1.19 KB

Contents

module ServiceContract
  module Assertions

    def assert_endpoint_response(data, endpoint, allow_nil = true)
      assert_data_matches_type(data, endpoint.response_type, allow_nil)
    end

    def assert_data_matches_type(data, type, allow_nil = true)
      if type.array?
        assert data.is_a?(Array), "expected #{type.name} to be an Array; instead got: #{data.inspect} (#{data.class.name})"
        data.each do |datum|
          assert_data_matches_type(datum, type.subtype, allow_nil)
        end
      elsif type.complex?

        # type should have fields
        type.fields.each do |field|

          # ensure the field is present
          value = data.fetch(field.name) do
            data.fetch(field.name.to_sym) do
              assert false, "expected #{type.name} to have attribute: #{field.name}"
            end
          end

          # check the data type
          assert_data_matches_type(value, field.type, allow_nil)
        end
      else
        # type is a scalar
        assert (allow_nil && data.nil?) || type.valid_ruby_types.any?{|klass| data.is_a?(klass)}, "expected scalar type #{type.inspect} or nil; instead got: #{data.inspect} (#{data.class.name})"
      end
    end

  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
service_contract-0.1.1 lib/service_contract/assertions.rb
service_contract-0.1.0 lib/service_contract/assertions.rb
service_contract-0.0.10 lib/service_contract/assertions.rb