Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

fixes

  • Loading branch information...
commit 12f14fc2da3aa88f54ada451a0852c4a9b5b7c21 1 parent 8ae6a29
@fxposter fxposter authored committed
View
94 test/functional/test_validatable.rb
@@ -267,39 +267,41 @@
instance.errors.on(:namen)
end
- expect "can't be empty" do
- child_class = Class.new do
- include Validatable
- attr_accessor :name, :address
- validates_presence_of :name
- end
- klass = Class.new do
- include Validatable
- include_errors_from :child, :if => lambda { true }
- define_method :child do
- child_class.new
+ [[:if, true], [:unless, false]].each do |option, value|
+ expect "can't be empty" do
+ child_class = Class.new do
+ include Validatable
+ attr_accessor :name, :address
+ validates_presence_of :name
+ end
+ klass = Class.new do
+ include Validatable
+ include_errors_from :child, option => lambda { value }
+ define_method :child do
+ child_class.new
+ end
end
+ instance = klass.new
+ instance.valid?
+ instance.errors.on(:name)
end
- instance = klass.new
- instance.valid?
- instance.errors.on(:name)
- end
- expect true do
- child_class = Class.new do
- include Validatable
- attr_accessor :name, :address
- validates_presence_of :name
- end
- klass = Class.new do
- include Validatable
- include_errors_from :child, :if => lambda { false }
- define_method :child do
- child_class.new
+ expect value do
+ child_class = Class.new do
+ include Validatable
+ attr_accessor :name, :address
+ validates_presence_of :name
+ end
+ klass = Class.new do
+ include Validatable
+ include_errors_from :child, option => lambda { !value }
+ define_method :child do
+ child_class.new
+ end
end
+ instance = klass.new
+ instance.valid?
end
- instance = klass.new
- instance.valid?
end
test ':if with symbol should work' do
@@ -338,6 +340,42 @@ def name_required?
assert_equal "can't be empty", instance.errors.on(:name)
end
+ test ':unless with symbol should work' do
+ klass = Class.new do
+ include Validatable
+ attr_accessor :name, :use_email_instead_of_name
+ validates_presence_of :name, :unless => :use_email_instead_of_name?
+
+ def use_email_instead_of_name?
+ use_email_instead_of_name
+ end
+ end
+ instance = klass.new
+ instance.use_email_instead_of_name = true
+ assert instance.valid?
+ instance.use_email_instead_of_name = false
+ assert !instance.valid?
+ assert_equal "can't be empty", instance.errors.on(:name)
+ end
+
+ test ':unless with string should work' do
+ klass = Class.new do
+ include Validatable
+ attr_accessor :name, :use_email_instead_of_name
+ validates_presence_of :name, :unless => "use_email_instead_of_name?"
+
+ def use_email_instead_of_name?
+ use_email_instead_of_name
+ end
+ end
+ instance = klass.new
+ instance.use_email_instead_of_name = true
+ assert instance.valid?
+ instance.use_email_instead_of_name = false
+ assert !instance.valid?
+ assert_equal "can't be empty", instance.errors.on(:name)
+ 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
View
2  test/unit/test_validates_acceptance_of.rb
@@ -38,7 +38,7 @@
end
expect true do
- options = {:message => nil, :if => nil, :times => nil, :level => nil, :groups => nil}
+ options = {:message => nil, :if => nil, :unless => nil, :times => nil, :level => nil, :groups => nil}
Validatable::ValidatesAcceptanceOf.new(stub_everything, :test).must_understand(options)
end
View
2  test/unit/test_validates_confirmation_of.rb
@@ -69,7 +69,7 @@
end
expect true do
- options = { :message => nil, :if => nil, :times => nil, :level => nil, :groups => nil, :case_sensitive => nil }
+ options = { :message => nil, :if => nil, :unless => nil, :times => nil, :level => nil, :groups => nil, :case_sensitive => nil }
Validatable::ValidatesConfirmationOf.new(stub_everything, :test).must_understand(options)
end
View
2  test/unit/test_validates_format_of.rb
@@ -32,7 +32,7 @@
end
expect true do
- options = [:message, :if, :times, :level, :groups, :with, :key]
+ options = [:message, :if, :unless, :times, :level, :groups, :with, :key]
Validatable::ValidatesFormatOf.new(stub_everything, :test, options.to_blank_options_hash).must_understand(options.to_blank_options_hash)
end
View
2  test/unit/test_validates_length_of.rb
@@ -73,7 +73,7 @@
end
expect true do
- options = [:message, :if, :times, :level, :groups, :maximum, :minimum, :is, :within, :allow_nil]
+ options = [:message, :if, :unless, :times, :level, :groups, :maximum, :minimum, :is, :within, :allow_nil]
Validatable::ValidatesLengthOf.new(stub_everything, :test).must_understand(options.to_blank_options_hash)
end
View
2  test/unit/test_validates_numericality_of.rb
@@ -69,7 +69,7 @@
end
expect true do
- options = [:message, :if, :times, :level, :groups, :only_integer]
+ options = [:message, :if, :unless, :times, :level, :groups, :only_integer]
Validatable::ValidatesNumericalityOf.new(stub_everything, :test).must_understand(options.to_blank_options_hash)
end
View
2  test/unit/test_validates_presence_of.rb
@@ -28,7 +28,7 @@
end
expect true do
- options = {:message => nil, :if => nil, :times => nil, :level => nil, :groups => nil}
+ options = {:message => nil, :if => nil, :unless => nil, :times => nil, :level => nil, :groups => nil}
Validatable::ValidatesPresenceOf.new(stub_everything, :test).must_understand(options)
end
View
2  test/unit/test_validates_true_for.rb
@@ -17,7 +17,7 @@
end
expect true do
- options = [:message, :if, :times, :level, :groups, :logic, :key]
+ options = [:message, :if, :unless, :times, :level, :groups, :logic, :key]
Validatable::ValidatesTrueFor.new(stub_everything, :name, options.to_blank_options_hash).must_understand(options.to_blank_options_hash)
end
View
2  test/unit/test_validation_base.rb
@@ -45,7 +45,7 @@
end
expect true do
- options = {:message => nil, :if => nil, :times => nil, :level => nil, :groups => nil, :key => nil}
+ options = {:message => nil, :if => nil, :unless => nil, :times => nil, :level => nil, :groups => nil, :key => nil}
Validatable::ValidationBase.new(stub_everything, :base).must_understand(options)
end
Please sign in to comment.
Something went wrong with that request. Please try again.