Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

refactor validation macro

  • Loading branch information...
commit d981fc115ed22d6cabb715ff8045851730fe5fdf 1 parent 87cd5bd
@achiu achiu authored
Showing with 22 additions and 30 deletions.
  1. +21 −29 lib/riot-mongoid/has_validation.rb
  2. +1 −1  test/has_validation_test.rb
View
50 lib/riot-mongoid/has_validation.rb
@@ -2,41 +2,33 @@ module RiotMongoid
class HasValidationAssertion < Riot::AssertionMacro
register :has_validation
- def evaluate(model, *validation_macro_info)
- validation_type, validation_field, options = validation_macro_info
- type = validation_type.to_s
+ def evaluate(model, type, field, options = {})
+ fail_msg = "expected #{model.class.to_s} to have validation '#{type} :#{field}'"
+ pass_msg = "#{model.class.to_s} has validation '#{type} :#{field}'"
+ opt_msg = " with options #{options.inspect}"
- %w{validates_ _of}.each { |part| type.gsub!(%r{#{part}},'') }
+ type = type.to_s.match(/validates_(.*)_of/)[1] rescue type.to_s
- validation = model._validators[validation_field].detect do |valid|
- valid.class.name =~ /#{type.camelize}/
- end
-
- options ||= {}
-
- case
- when validation_field.nil? || validation_type.nil?
- fail("validation field, type and potential options must be specified with this assertion macro")
-
- when validation.nil?
- fail("expected #{model} to have validation on #{validation_field} of type #{validation_type}")
+ validation = model._validators[field].detect { |v| v.class.name =~ /#{type.camelize}/ }
- when (validation.class.name =~ %r{Length} and options[:within])
- range = options[:within].to_a
- if (validation.options[:minimum] == range.first) and (validation.options[:maximum] == range.last)
- pass("#{model} has '#{validation_type}' validation '#{validation_field}' with options #{options.inspect}")
- else
- fail("expected #{model} to have options #{options.inspect} on validation #{validation_type}")
- end
+ return fail(fail_msg) if validation.nil?
+ # special case to check for validates_length_of :within option
+ if validation.class.name =~ %r{Length} and options[:within]
+ range = options[:within].to_a
+ if (validation.options[:minimum] == range.first) and (validation.options[:maximum] == range.last)
+ return pass(pass_msg + opt_msg)
else
- options_valid = options.all? { |key,value| validation.options[key] == value }
- if options_valid
- pass("#{model} has '#{validation_type}' validation '#{validation_field}' with options #{options.inspect}")
- else
- fail("expected #{model} to have options #{options.inspect} on validation #{validation_type}")
- end
+ return fail(fail_msg + opt_msg)
+ end
end
+
+ unless options.empty?
+ return fail(fail_msg + opt_msg) unless options.all? { |k,v| validation.options[k] == v }
+ return pass(pass_msg + opt_msg)
+ end
+
+ pass pass_msg
end
end
end
View
2  test/has_validation_test.rb
@@ -41,7 +41,7 @@ def validate_each(record, attribute, value)
asserts "returns useful message" do
RiotMongoid::HasValidationAssertion.new.evaluate(topic, :validates_presence_of, :name).last
- end.matches(/has 'validates_presence_of' validation 'name'/)
+ end.matches(/Class has validation 'validates_presence_of :name'/)
asserts "passes when the validation options is specified using within" do
RiotMongoid::HasValidationAssertion.new.evaluate(topic, :validates_length_of, :surname, :within => 4..40).first
Please sign in to comment.
Something went wrong with that request. Please try again.