2009-03-20 11:07:49 -04:00
|
|
|
# encoding: utf-8
|
|
|
|
require 'cases/helper'
|
|
|
|
|
|
|
|
require 'models/topic'
|
2009-03-20 18:21:27 -04:00
|
|
|
require 'models/person'
|
2009-03-20 11:07:49 -04:00
|
|
|
|
2010-06-27 16:11:09 -04:00
|
|
|
require 'bigdecimal'
|
|
|
|
|
2009-03-20 11:07:49 -04:00
|
|
|
class NumericalityValidationTest < ActiveModel::TestCase
|
|
|
|
|
2009-12-23 07:30:58 -05:00
|
|
|
def teardown
|
|
|
|
Topic.reset_callbacks(:validate)
|
|
|
|
end
|
2009-03-20 11:07:49 -04:00
|
|
|
|
|
|
|
NIL = [nil]
|
|
|
|
BLANK = ["", " ", " \t \r \n"]
|
2011-03-03 22:23:30 -05:00
|
|
|
BIGDECIMAL_STRINGS = %w(12345678901234567890.1234567890) # 30 significant digits
|
2009-03-20 11:07:49 -04:00
|
|
|
FLOAT_STRINGS = %w(0.0 +0.0 -0.0 10.0 10.5 -10.5 -0.0001 -090.1 90.1e1 -90.1e5 -90.1e-5 90e-5)
|
|
|
|
INTEGER_STRINGS = %w(0 +0 -0 10 +10 -10 0090 -090)
|
|
|
|
FLOATS = [0.0, 10.0, 10.5, -10.5, -0.0001] + FLOAT_STRINGS
|
|
|
|
INTEGERS = [0, 10, -10] + INTEGER_STRINGS
|
|
|
|
BIGDECIMAL = BIGDECIMAL_STRINGS.collect! { |bd| BigDecimal.new(bd) }
|
2010-05-17 20:24:53 -04:00
|
|
|
JUNK = ["not a number", "42 not a number", "0xdeadbeef", "0xinvalidhex", "0Xdeadbeef", "00-1", "--3", "+-3", "+3-1", "-+019.0", "12.12.13.12", "123\nnot a number"]
|
2009-03-20 11:07:49 -04:00
|
|
|
INFINITY = [1.0/0.0]
|
|
|
|
|
|
|
|
def test_default_validates_numericality_of
|
|
|
|
Topic.validates_numericality_of :approved
|
|
|
|
invalid!(NIL + BLANK + JUNK)
|
|
|
|
valid!(FLOATS + INTEGERS + BIGDECIMAL + INFINITY)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_validates_numericality_of_with_nil_allowed
|
|
|
|
Topic.validates_numericality_of :approved, :allow_nil => true
|
|
|
|
|
2010-05-08 16:27:49 -04:00
|
|
|
invalid!(JUNK + BLANK)
|
|
|
|
valid!(NIL + FLOATS + INTEGERS + BIGDECIMAL + INFINITY)
|
2009-03-20 11:07:49 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_validates_numericality_of_with_integer_only
|
|
|
|
Topic.validates_numericality_of :approved, :only_integer => true
|
|
|
|
|
|
|
|
invalid!(NIL + BLANK + JUNK + FLOATS + BIGDECIMAL + INFINITY)
|
|
|
|
valid!(INTEGERS)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_validates_numericality_of_with_integer_only_and_nil_allowed
|
|
|
|
Topic.validates_numericality_of :approved, :only_integer => true, :allow_nil => true
|
|
|
|
|
2010-05-08 16:27:49 -04:00
|
|
|
invalid!(JUNK + BLANK + FLOATS + BIGDECIMAL + INFINITY)
|
|
|
|
valid!(NIL + INTEGERS)
|
2009-03-20 11:07:49 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_validates_numericality_with_greater_than
|
|
|
|
Topic.validates_numericality_of :approved, :greater_than => 10
|
|
|
|
|
|
|
|
invalid!([-10, 10], 'must be greater than 10')
|
|
|
|
valid!([11])
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_validates_numericality_with_greater_than_or_equal
|
|
|
|
Topic.validates_numericality_of :approved, :greater_than_or_equal_to => 10
|
|
|
|
|
|
|
|
invalid!([-9, 9], 'must be greater than or equal to 10')
|
|
|
|
valid!([10])
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_validates_numericality_with_equal_to
|
|
|
|
Topic.validates_numericality_of :approved, :equal_to => 10
|
|
|
|
|
|
|
|
invalid!([-10, 11] + INFINITY, 'must be equal to 10')
|
|
|
|
valid!([10])
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_validates_numericality_with_less_than
|
|
|
|
Topic.validates_numericality_of :approved, :less_than => 10
|
|
|
|
|
|
|
|
invalid!([10], 'must be less than 10')
|
|
|
|
valid!([-9, 9])
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_validates_numericality_with_less_than_or_equal_to
|
|
|
|
Topic.validates_numericality_of :approved, :less_than_or_equal_to => 10
|
|
|
|
|
|
|
|
invalid!([11], 'must be less than or equal to 10')
|
|
|
|
valid!([-10, 10])
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_validates_numericality_with_odd
|
|
|
|
Topic.validates_numericality_of :approved, :odd => true
|
|
|
|
|
|
|
|
invalid!([-2, 2], 'must be odd')
|
|
|
|
valid!([-1, 1])
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_validates_numericality_with_even
|
|
|
|
Topic.validates_numericality_of :approved, :even => true
|
|
|
|
|
|
|
|
invalid!([-1, 1], 'must be even')
|
|
|
|
valid!([-2, 2])
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_validates_numericality_with_greater_than_less_than_and_even
|
|
|
|
Topic.validates_numericality_of :approved, :greater_than => 1, :less_than => 4, :even => true
|
|
|
|
|
|
|
|
invalid!([1, 3, 4])
|
|
|
|
valid!([2])
|
|
|
|
end
|
|
|
|
|
2009-08-31 14:34:46 -04:00
|
|
|
def test_validates_numericality_with_proc
|
|
|
|
Topic.send(:define_method, :min_approved, lambda { 5 })
|
|
|
|
Topic.validates_numericality_of :approved, :greater_than_or_equal_to => Proc.new {|topic| topic.min_approved }
|
|
|
|
|
|
|
|
invalid!([3, 4])
|
|
|
|
valid!([5, 6])
|
|
|
|
Topic.send(:remove_method, :min_approved)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_validates_numericality_with_symbol
|
|
|
|
Topic.send(:define_method, :max_approved, lambda { 5 })
|
|
|
|
Topic.validates_numericality_of :approved, :less_than_or_equal_to => :max_approved
|
|
|
|
|
|
|
|
invalid!([6])
|
|
|
|
valid!([4, 5])
|
|
|
|
Topic.send(:remove_method, :max_approved)
|
|
|
|
end
|
|
|
|
|
2009-03-20 11:07:49 -04:00
|
|
|
def test_validates_numericality_with_numeric_message
|
2010-05-03 02:44:32 -04:00
|
|
|
Topic.validates_numericality_of :approved, :less_than => 4, :message => "smaller than %{count}"
|
2009-03-20 11:07:49 -04:00
|
|
|
topic = Topic.new("title" => "numeric test", "approved" => 10)
|
|
|
|
|
|
|
|
assert !topic.valid?
|
2009-03-20 13:36:22 -04:00
|
|
|
assert_equal ["smaller than 4"], topic.errors[:approved]
|
2009-03-20 11:07:49 -04:00
|
|
|
|
2010-05-03 02:44:32 -04:00
|
|
|
Topic.validates_numericality_of :approved, :greater_than => 4, :message => "greater than %{count}"
|
2009-03-20 11:07:49 -04:00
|
|
|
topic = Topic.new("title" => "numeric test", "approved" => 1)
|
|
|
|
|
|
|
|
assert !topic.valid?
|
2009-03-20 13:36:22 -04:00
|
|
|
assert_equal ["greater than 4"], topic.errors[:approved]
|
2009-03-20 11:07:49 -04:00
|
|
|
end
|
|
|
|
|
2009-03-20 18:21:27 -04:00
|
|
|
def test_validates_numericality_of_for_ruby_class
|
2009-12-23 07:30:58 -05:00
|
|
|
Person.validates_numericality_of :karma, :allow_nil => false
|
2009-03-20 18:21:27 -04:00
|
|
|
|
2009-12-23 07:30:58 -05:00
|
|
|
p = Person.new
|
|
|
|
p.karma = "Pix"
|
|
|
|
assert p.invalid?
|
2009-03-20 18:21:27 -04:00
|
|
|
|
2009-12-23 07:30:58 -05:00
|
|
|
assert_equal ["is not a number"], p.errors[:karma]
|
2009-03-20 18:21:27 -04:00
|
|
|
|
2009-12-23 07:30:58 -05:00
|
|
|
p.karma = "1234"
|
|
|
|
assert p.valid?
|
|
|
|
ensure
|
|
|
|
Person.reset_callbacks(:validate)
|
2009-03-20 18:21:27 -04:00
|
|
|
end
|
|
|
|
|
2010-01-11 17:37:48 -05:00
|
|
|
def test_validates_numericality_with_invalid_args
|
|
|
|
assert_raise(ArgumentError){ Topic.validates_numericality_of :approved, :greater_than_or_equal_to => "foo" }
|
|
|
|
assert_raise(ArgumentError){ Topic.validates_numericality_of :approved, :less_than_or_equal_to => "foo" }
|
|
|
|
assert_raise(ArgumentError){ Topic.validates_numericality_of :approved, :greater_than => "foo" }
|
|
|
|
assert_raise(ArgumentError){ Topic.validates_numericality_of :approved, :less_than => "foo" }
|
|
|
|
assert_raise(ArgumentError){ Topic.validates_numericality_of :approved, :equal_to => "foo" }
|
|
|
|
end
|
|
|
|
|
2009-03-20 11:07:49 -04:00
|
|
|
private
|
|
|
|
|
|
|
|
def invalid!(values, error = nil)
|
|
|
|
with_each_topic_approved_value(values) do |topic, value|
|
2010-05-08 16:27:49 -04:00
|
|
|
assert topic.invalid?, "#{value.inspect} not rejected as a number"
|
2009-03-20 11:07:49 -04:00
|
|
|
assert topic.errors[:approved].any?, "FAILED for #{value.inspect}"
|
|
|
|
assert_equal error, topic.errors[:approved].first if error
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def valid!(values)
|
|
|
|
with_each_topic_approved_value(values) do |topic, value|
|
|
|
|
assert topic.valid?, "#{value.inspect} not accepted as a number"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def with_each_topic_approved_value(values)
|
|
|
|
topic = Topic.new(:title => "numeric test", :content => "whatever")
|
|
|
|
values.each do |value|
|
|
|
|
topic.approved = value
|
|
|
|
yield topic, value
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|