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)}
|
2011-05-26 17:35:35 -04:00
|
|
|
[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
|
|
|
|
|
2011-09-28 15:24:40 -04:00
|
|
|
def formats(format, strict=true)
|
2011-01-08 22:53:54 -05:00
|
|
|
raise ArgumentError, 'format is nil' unless format
|
|
|
|
|
2011-01-04 17:01:31 -05:00
|
|
|
test('has proper format') do
|
2011-09-28 15:24:40 -04:00
|
|
|
formats_kernel(instance_eval(&Proc.new), format, true, strict)
|
2011-01-04 17:01:31 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2011-09-28 15:24:40 -04:00
|
|
|
def formats_kernel(original_data, original_format, original = true, strict = true)
|
2011-01-04 17:01:31 -05:00
|
|
|
valid = true
|
|
|
|
data = original_data.dup
|
|
|
|
format = original_format.dup
|
|
|
|
if format.is_a?(Array)
|
|
|
|
data = {:element => data}
|
|
|
|
format = {:element => format}
|
|
|
|
end
|
|
|
|
for key, value in format
|
|
|
|
datum = data.delete(key)
|
|
|
|
format.delete(key)
|
|
|
|
case value
|
|
|
|
when Array
|
2011-09-23 17:58:39 -04:00
|
|
|
valid &&= datum.is_a?(Array) || p("#{key.inspect} not Array: #{datum.inspect}")
|
2011-01-21 18:32:46 -05:00
|
|
|
if datum.is_a?(Array) && !value.empty?
|
2011-01-04 17:01:31 -05:00
|
|
|
for element in datum
|
|
|
|
type = value.first
|
|
|
|
if type.is_a?(Hash)
|
2011-09-28 15:24:40 -04:00
|
|
|
valid &&= formats_kernel({:element => element}, {:element => type}, false, strict)
|
2011-01-04 17:01:31 -05:00
|
|
|
else
|
|
|
|
valid &&= element.is_a?(type)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
when Hash
|
2011-09-23 17:58:39 -04:00
|
|
|
valid &&= datum.is_a?(Hash) || p("#{key.inspect} not Hash: #{datum.inspect}")
|
2011-09-28 15:24:40 -04:00
|
|
|
valid &&= formats_kernel(datum, value, false, strict)
|
2011-01-04 17:01:31 -05:00
|
|
|
else
|
2011-07-19 16:10:26 -04:00
|
|
|
p "#{key.inspect} not #{value.inspect}: #{datum.inspect}" unless datum.is_a?(value)
|
2011-01-04 17:01:31 -05:00
|
|
|
valid &&= datum.is_a?(value)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
p data unless data.empty?
|
|
|
|
p format unless format.empty?
|
2011-09-28 15:24:40 -04:00
|
|
|
if strict
|
|
|
|
valid &&= data.empty? && format.empty?
|
|
|
|
else
|
|
|
|
valid &&= format.empty?
|
|
|
|
end
|
2011-01-04 17:01:31 -05:00
|
|
|
if !valid && original
|
|
|
|
@message = "#{original_data.inspect} does not match #{original_format.inspect}"
|
|
|
|
end
|
|
|
|
valid
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
end
|