2010-03-28 20:04:38 -04:00
|
|
|
require File.expand_path(File.join(File.dirname(__FILE__), '..', 'lib', 'fog'))
|
|
|
|
require File.expand_path(File.join(File.dirname(__FILE__), '..', 'lib', 'fog', 'bin'))
|
|
|
|
|
2010-04-05 01:11:28 -04:00
|
|
|
# TODO: Currently is true even if some of the keys in format do not appear
|
|
|
|
def validate_format(original_data, format)
|
2010-03-28 20:04:38 -04:00
|
|
|
valid = true
|
|
|
|
data = original_data.dup
|
|
|
|
for key, value in format
|
|
|
|
valid &&= data.has_key?(key)
|
|
|
|
datum = data.delete(key)
|
|
|
|
case value
|
|
|
|
when Array
|
|
|
|
valid &&= datum.is_a?(Array)
|
|
|
|
for element in datum
|
|
|
|
type = value.first
|
|
|
|
if type.is_a?(Hash)
|
2010-04-05 01:11:28 -04:00
|
|
|
valid &&= validate_format({:element => element}, {:element => type})
|
2010-03-28 20:04:38 -04:00
|
|
|
else
|
|
|
|
valid &&= element.is_a?(type)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
when Hash
|
|
|
|
valid &&= datum.is_a?(Hash)
|
2010-04-05 01:11:28 -04:00
|
|
|
valid &&= validate_format(datum, value)
|
2010-03-28 20:04:38 -04:00
|
|
|
else
|
|
|
|
valid &&= datum.is_a?(value)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
valid &&= data.empty?
|
2010-04-05 01:11:28 -04:00
|
|
|
unless valid
|
|
|
|
@formatador.display_line("[red]#{original_data.inspect} does not match #{format.inspect}[/]")
|
|
|
|
end
|
|
|
|
valid
|
2010-03-28 20:04:38 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def wait_for(timeout = 600, &block)
|
|
|
|
start = Time.now
|
|
|
|
until instance_eval(&block)
|
|
|
|
if Time.now - start > timeout
|
|
|
|
break
|
|
|
|
end
|
|
|
|
sleep(1)
|
|
|
|
end
|
|
|
|
end
|