test/functional/validatable_test.rb in validatable-1.3.4 vs test/functional/validatable_test.rb in validatable-1.4.0

- old
+ new

@@ -1,23 +1,48 @@ require File.expand_path(File.dirname(__FILE__) + '/../test_helper') module Functional class ValidatableTest < Test::Unit::TestCase - test "validations are only executed once" do - if_condition = mock - if_condition.expects(:where?).times 2 - klass = Class.new do + + expect ArgumentError do + Class.new do include Validatable + attr_accessor :name + validates_presence_of :name, :times => 1 + validates_presence_of :name, :times => 1 + end + end + + expect ArgumentError do + Class.new do + include Validatable + attr_accessor :name + validates_presence_of :name, :times => 1, :key => 'anything' + validates_presence_of :name, :times => 1, :key => 'anything' + end + end + + expect "is invalid" do + child_class = Class.new do + include Validatable attr_accessor :name, :address - validates_presence_of :name, :if => lambda { if_condition.where? } - validates_presence_of :address, :if => lambda { if_condition.where? } + validates_presence_of :name + validates_format_of :address, :with => /.+/ end + klass = Class.new do + include Validatable + include_validations_for :child + define_method :child do + child_class.new + end + end instance = klass.new instance.valid? + instance.errors.on(:address) end - test "given a child class with validations, when parent class is validated, then the error is in the parent objects error collection" do + expect "can't be empty" do child_class = Class.new do include Validatable attr_accessor :name, :address validates_presence_of :name validates_format_of :address, :with => /.+/ @@ -29,35 +54,30 @@ child_class.new end end instance = klass.new instance.valid? - assert_equal "is invalid", instance.errors.on(:address) - assert_equal "can't be empty", instance.errors.on(:name) + instance.errors.on(:name) end test "when child validations have errors, level 2 and higher parent validations are not performed" do child_class = Class.new do include Validatable attr_accessor :name validates_presence_of :name end + klass = Class.new do include Validatable extend Forwardable - def_delegator :child, :name - validates_true_for :name, :logic => lambda { false }, :level => 2, :message => "invalid message" - include_validations_for :child - define_method :child do @child ||= child_class.new end - end instance = klass.new instance.valid? assert_equal "can't be empty", instance.errors.on(:name) end @@ -65,19 +85,16 @@ test "when child validations have errors, level 1 parent validations are still performed" do child_class = Class.new do include Validatable attr_accessor :name validates_presence_of :name - end + klass = Class.new do include Validatable - validates_true_for :address, :logic => lambda { false }, :level => 1, :message => "invalid message" - include_validations_for :child - define_method :child do @child ||= child_class.new end end @@ -85,11 +102,11 @@ instance.valid? assert_equal "can't be empty", instance.errors.on(:name) assert_equal "invalid message", instance.errors.on(:address) end - test "given a child class with validations, the error is in the parent objects error collection as the mapped attribute" do + expect "can't be empty" do child_class = Class.new do include Validatable attr_accessor :name, :address validates_presence_of :name end @@ -100,14 +117,14 @@ child_class.new end end instance = klass.new instance.valid? - assert_equal "can't be empty", instance.errors.on(:namen) + instance.errors.on(:namen) end - test "given a child class with validations, the error is in the parent objects error collection when the 'if' evals to true" do + expect "can't be empty" do child_class = Class.new do include Validatable attr_accessor :name, :address validates_presence_of :name end @@ -118,14 +135,14 @@ child_class.new end end instance = klass.new instance.valid? - assert_equal "can't be empty", instance.errors.on(:name) + instance.errors.on(:name) end - test "given a child class with validations, the error is not in the parent objects error collection when the if evals to false" do + expect true do child_class = Class.new do include Validatable attr_accessor :name, :address validates_presence_of :name end @@ -135,11 +152,11 @@ define_method :child do child_class.new end end instance = klass.new - assert_equal true, instance.valid? + instance.valid? end test "classes only have valid_for_* methods for groups that appear in their validations" do class_with_group_one = Class.new do include Validatable @@ -164,11 +181,11 @@ end instance = klass.new assert_equal nil, instance.errors.on(:name) end - test "after validate is called following a validation" do + expect "can't be empty twice changed message" do klass = Class.new do include Validatable validates_presence_of :name attr_accessor :name end @@ -183,43 +200,43 @@ instance.errors.add(attribute, " twice") end end instance = klass.new instance.valid? - assert_equal "can't be empty twice changed message", instance.errors.on(:name).join Validatable::ValidatesPresenceOf.after_validations.clear Validatable::ValidationBase.after_validations.clear + instance.errors.on(:name).join end - test "matching groups are used as validations when multiple groups are given to valid" do + expect false do klass = Class.new do include Validatable validates_presence_of :name, :groups => :group_one attr_accessor :name end instance = klass.new - assert_equal false, instance.valid_for_group_one? + instance.valid_for_group_one? end - test "matching groups are used as validations when validations are part of multiple groups" do + expect false do klass = Class.new do include Validatable validates_presence_of :name, :groups => [:group_one, :group_two] attr_accessor :name end instance = klass.new - assert_equal false, instance.valid_for_group_one? + instance.valid_for_group_one? end - test "no group given then all validations are used" do + expect false do klass = Class.new do include Validatable validates_presence_of :name, :groups => :group_one attr_accessor :name end instance = klass.new - assert_equal false, instance.valid? + instance.valid? end expect true do klass = Class.new do include Validatable @@ -240,10 +257,10 @@ instance1 = klass.new instance1.valid? instance2 = klass.new instance2.valid? end - + expect "name message" do klass = Class.new do include Validatable validates_presence_of :name, :level => 1, :message => "name message" validates_presence_of :address, :level => 2 \ No newline at end of file