From 838ae35d63c34872d46bee8b006796ebdd9c7722 Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Sun, 6 Mar 2005 12:43:23 +0000 Subject: Added validates_numericality_of #716 [skanthak/c.r.mcgrath] git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@842 5ecf4fe2-1ee6-0310-87b1-e25e094e27de --- activerecord/test/validations_test.rb | 38 +++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) (limited to 'activerecord/test/validations_test.rb') diff --git a/activerecord/test/validations_test.rb b/activerecord/test/validations_test.rb index 9d7b8b987a..913c72ea3c 100755 --- a/activerecord/test/validations_test.rb +++ b/activerecord/test/validations_test.rb @@ -604,4 +604,42 @@ class ValidationsTest < Test::Unit::TestCase assert !r.valid? assert_equal r.errors.on(:topic).first, "This string contains 'single' and \"double\" quotes" end + + def test_validates_numericality_of_with_string + Topic.validates_numericality_of( :replies_count ) + ["not a number","42 not a number","0xdeadbeef","00-1","-+019.0","12.12.13.12",nil].each do |v| + t = Topic.create("title" => "numeric test", "content" => "whatever", "replies_count" => "not a number") + assert !t.valid?, "#{v} not rejected as a number" + assert t.errors.on(:replies_count) + end + end + + def test_validates_numericality_of + Topic.validates_numericality_of( :replies_count ) + ["10", "10.0", "10.5", "-10.5", "-0.0001","0090","-090","-090.1"].each do |v| + t = Topic.create("title" => "numeric test", "content" => "whatever", "replies_count" => v) + assert t.valid?, "#{v} not recognized as a number" + # we cannot check this as replies_count is actually an integer field + #assert_in_delta v.to_f, t.replies_count, 0.0000001 + end + end + + def test_validates_numericality_of_int_with_string + Topic.validates_numericality_of( :replies_count, :only_integer => true ) + ["not a number","42 not a number","0xdeadbeef","0-1","--3","+-3","+3-1",nil].each do |v| + t = Topic.create("title" => "numeric test", "content" => "whatever", "replies_count" => v) + assert !t.valid?, "#{v} not rejected as integer" + assert t.errors.on(:replies_count) + end + end + + def test_validates_numericality_of_int + Topic.validates_numericality_of( :replies_count, :only_integer => true ) + ["42", "+42", "-42", "042", "0042", "-042", 42].each do |v| + t = Topic.create("title" => "numeric test", "content" => "whatever", "replies_count" => v) + assert t.valid?, "#{v} not recognized as integer" + assert_equal v.to_i, t.replies_count + end + end + end -- cgit v1.2.3