Sha256: 6fc4d7b96c3aedab10d35ad57316cdef522264942cbec5112061f95e540c9095
Contents?: true
Size: 1.51 KB
Versions: 2
Compression:
Stored size: 1.51 KB
Contents
module ServiceContract module Assertions def assert_endpoint_response(data, endpoint, allow_nil = true) assert_data_matches_type(data, endpoint.response_type, endpoint.name, allow_nil) end def assert_data_matches_type(data, type, name, allow_nil = true) # Skip out if object is nil and allowed to be nil return true if data.nil? && allow_nil # basic type checking assert type.valid_type?(data), "expected `#{data}` (#{name}) to be one of #{type.valid_ruby_types}, instead got a #{data.class.name}" assert type.valid_value?(data), "#{data} is not an allowed value of type: #{type.name}" # check subtype if type.subtype data.each do |datum| assert_data_matches_type(datum, type.subtype, type.name, allow_nil) end end # check subfields 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, field.name, allow_nil) end end end end
Version data entries
2 entries across 2 versions & 1 rubygems
Version | Path |
---|---|
service_contract-0.6.0 | lib/service_contract/assertions.rb |
service_contract-0.5.1 | lib/service_contract/assertions.rb |