2013-01-23 06:27:14 -05:00
|
|
|
require "fog/schema/data_validator"
|
|
|
|
|
2011-01-04 17:01:31 -05:00
|
|
|
# format related hackery
|
|
|
|
# allows both true.is_a?(Fog::Boolean) and false.is_a?(Fog::Boolean)
|
|
|
|
# allows both nil.is_a?(Fog::Nullable::String) and ''.is_a?(Fog::Nullable::String)
|
|
|
|
module Fog
|
|
|
|
module Boolean; end
|
|
|
|
module Nullable
|
2011-05-26 17:35:35 -04:00
|
|
|
module Boolean; end
|
2011-01-04 18:17:05 -05:00
|
|
|
module Integer; end
|
2011-01-04 17:01:31 -05:00
|
|
|
module String; end
|
2011-02-14 17:55:47 -05:00
|
|
|
module Time; end
|
2011-07-27 21:44:43 -04:00
|
|
|
module Float; end
|
2011-09-28 15:24:40 -04:00
|
|
|
module Hash; end
|
|
|
|
module Array; end
|
2011-01-04 17:01:31 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
[FalseClass, TrueClass].each {|klass| klass.send(:include, Fog::Boolean)}
|
2012-01-22 19:32:46 -05:00
|
|
|
[FalseClass, TrueClass, NilClass, Fog::Boolean].each {|klass| klass.send(:include, Fog::Nullable::Boolean)}
|
2011-01-04 18:17:05 -05:00
|
|
|
[NilClass, String].each {|klass| klass.send(:include, Fog::Nullable::String)}
|
2011-02-14 17:55:47 -05:00
|
|
|
[NilClass, Time].each {|klass| klass.send(:include, Fog::Nullable::Time)}
|
2011-01-04 18:17:05 -05:00
|
|
|
[Integer, NilClass].each {|klass| klass.send(:include, Fog::Nullable::Integer)}
|
2011-07-27 21:44:43 -04:00
|
|
|
[Float, NilClass].each {|klass| klass.send(:include, Fog::Nullable::Float)}
|
2011-09-28 15:24:40 -04:00
|
|
|
[Hash, NilClass].each {|klass| klass.send(:include, Fog::Nullable::Hash)}
|
|
|
|
[Array, NilClass].each {|klass| klass.send(:include, Fog::Nullable::Array)}
|
2011-01-04 17:01:31 -05:00
|
|
|
|
|
|
|
module Shindo
|
|
|
|
class Tests
|
2013-01-16 10:44:56 -05:00
|
|
|
# Generates a Shindo test that compares a hash schema to the result
|
|
|
|
# of the passed in block returning true if they match.
|
|
|
|
#
|
|
|
|
# The schema that is passed in is a Hash or Array of hashes that
|
|
|
|
# have Classes in place of values. When checking the schema the
|
|
|
|
# value should match the Class.
|
|
|
|
#
|
|
|
|
# Strict mode will fail if the data has additional keys. Setting
|
|
|
|
# +strict+ to +false+ will allow additional keys to appear.
|
|
|
|
#
|
2013-01-23 06:27:14 -05:00
|
|
|
# @param [Hash] schema A Hash schema
|
2013-01-16 10:44:56 -05:00
|
|
|
# @param [Hash] options Options to change validation rules
|
|
|
|
# @option options [Boolean] :allow_extra_keys
|
2013-01-23 06:27:14 -05:00
|
|
|
# If +true+ does not fail when keys are in the data that are
|
2013-01-16 10:44:56 -05:00
|
|
|
# not specified in the schema. This allows new values to
|
|
|
|
# appear in API output without breaking the check.
|
|
|
|
# @option options [Boolean] :allow_optional_rules
|
|
|
|
# If +true+ does not fail if extra keys are in the schema
|
|
|
|
# that do not match the data. Not recommended!
|
|
|
|
# @yield Data to check with schema
|
|
|
|
#
|
|
|
|
# @example Using in a test
|
2013-01-23 06:27:14 -05:00
|
|
|
# Shindo.tests("comparing welcome data against schema") do
|
|
|
|
# data = {:welcome => "Hello" }
|
|
|
|
# data_matches_schema(:welcome => String) { data }
|
2013-01-16 10:44:56 -05:00
|
|
|
# end
|
|
|
|
#
|
2013-01-23 06:27:14 -05:00
|
|
|
# comparing welcome data against schema
|
|
|
|
# + data matches schema
|
2013-01-16 10:44:56 -05:00
|
|
|
#
|
|
|
|
# @example Example schema
|
|
|
|
# {
|
|
|
|
# "id" => String,
|
|
|
|
# "ram" => Integer,
|
|
|
|
# "disks" => [
|
2013-01-23 09:54:47 -05:00
|
|
|
# {
|
|
|
|
# "size" => Float
|
|
|
|
# }
|
2013-01-16 10:44:56 -05:00
|
|
|
# ],
|
|
|
|
# "dns_name" => Fog::Nullable::String,
|
|
|
|
# "active" => Fog::Boolean,
|
|
|
|
# "created" => DateTime
|
|
|
|
# }
|
|
|
|
#
|
|
|
|
# @return [Boolean]
|
|
|
|
def data_matches_schema(schema, options = {})
|
|
|
|
test('data matches schema') do
|
2013-01-23 06:27:14 -05:00
|
|
|
validator = Fog::Schema::DataValidator.new
|
|
|
|
valid = validator.validate(yield, schema, options)
|
|
|
|
@message = validator.message unless valid
|
|
|
|
valid
|
2013-01-16 10:44:56 -05:00
|
|
|
end
|
|
|
|
end
|
2011-01-08 22:53:54 -05:00
|
|
|
|
2013-01-23 06:27:14 -05:00
|
|
|
# @deprecated #formats is deprecated. Use #data_matches_schema instead
|
2013-01-16 10:44:56 -05:00
|
|
|
def formats(format, strict = true)
|
2011-01-04 17:01:31 -05:00
|
|
|
test('has proper format') do
|
2013-01-16 10:44:56 -05:00
|
|
|
if strict
|
|
|
|
options = {:allow_extra_keys => false, :allow_optional_rules => true}
|
|
|
|
else
|
|
|
|
options = {:allow_extra_keys => true, :allow_optional_rules => true}
|
|
|
|
end
|
2013-01-23 06:27:14 -05:00
|
|
|
validator = Fog::Schema::DataValidator.new
|
|
|
|
valid = validator.validate(yield, format, options)
|
|
|
|
@message = validator.message unless valid
|
|
|
|
valid
|
2011-01-04 17:01:31 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|