1
0
Fork 0
mirror of https://github.com/fog/fog.git synced 2022-11-09 13:51:43 -05:00
fog--fog/tests/helpers/formats_helper.rb

99 lines
3.5 KiB
Ruby
Raw Normal View History

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
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
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)}
[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)}
[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
# 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.
#
# @param [Hash] schema A Hash schema
# @param [Hash] options Options to change validation rules
# @option options [Boolean] :allow_extra_keys
# If +true+ does not fail when keys are in the data that are
# 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
# Shindo.tests("comparing welcome data against schema") do
# data = {:welcome => "Hello" }
# data_matches_schema(:welcome => String) { data }
# end
#
# comparing welcome data against schema
# + data matches schema
#
# @example Example schema
# {
# "id" => String,
# "ram" => Integer,
# "disks" => [
# {
# "size" => Float
# }
# ],
# "dns_name" => Fog::Nullable::String,
# "active" => Fog::Boolean,
# "created" => DateTime
# }
#
# @return [Boolean]
def data_matches_schema(schema, options = {})
test('data matches schema') do
validator = Fog::Schema::DataValidator.new
valid = validator.validate(yield, schema, options)
@message = validator.message unless valid
valid
end
end
2011-01-08 22:53:54 -05:00
# @deprecated #formats is deprecated. Use #data_matches_schema instead
def formats(format, strict = true)
2011-01-04 17:01:31 -05:00
test('has proper format') do
if strict
options = {:allow_extra_keys => false, :allow_optional_rules => true}
else
options = {:allow_extra_keys => true, :allow_optional_rules => true}
end
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