Sha256: a7d287de6c29ec62bb34f0894213478a4d1b25d329f0ca60be980788770cc530
Contents?: true
Size: 1.61 KB
Versions: 2
Compression:
Stored size: 1.61 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? # Skip out of the complex object is nil and allowed to be nil return true if data.nil? && allow_nil # type should have fields type.fields.each do |field| #Does data contain attributes that the contract doesn't specify? data_extra_attrs = (data.keys.map(&:to_sym) - type.fields.map{|n| n.name.to_sym}) assert_equal 0, data_extra_attrs.size, "#{type.name} contains attributes not described in contract: #{data_extra_attrs.join(',')}" # 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
2 entries across 2 versions & 1 rubygems
Version | Path |
---|---|
service_contract-0.3.0 | lib/service_contract/assertions.rb |
service_contract-0.2.1 | lib/service_contract/assertions.rb |