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
|
|
|
|
|
|
|
class LengthValidationTest < 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
|
|
|
|
|
|
|
def test_validates_length_of_with_allow_nil
|
2013-02-04 06:59:22 -05:00
|
|
|
Topic.validates_length_of( :title, is: 5, allow_nil: true )
|
2009-03-20 11:07:49 -04:00
|
|
|
|
2010-05-08 16:27:49 -04:00
|
|
|
assert Topic.new("title" => "ab").invalid?
|
|
|
|
assert Topic.new("title" => "").invalid?
|
|
|
|
assert Topic.new("title" => nil).valid?
|
|
|
|
assert Topic.new("title" => "abcde").valid?
|
2009-03-20 11:07:49 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_validates_length_of_with_allow_blank
|
2013-02-04 06:59:22 -05:00
|
|
|
Topic.validates_length_of( :title, is: 5, allow_blank: true )
|
2009-03-20 11:07:49 -04:00
|
|
|
|
2010-05-08 16:27:49 -04:00
|
|
|
assert Topic.new("title" => "ab").invalid?
|
|
|
|
assert Topic.new("title" => "").valid?
|
|
|
|
assert Topic.new("title" => nil).valid?
|
|
|
|
assert Topic.new("title" => "abcde").valid?
|
2009-03-20 11:07:49 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_validates_length_of_using_minimum
|
2013-02-04 06:59:22 -05:00
|
|
|
Topic.validates_length_of :title, minimum: 5
|
2009-03-20 11:07:49 -04:00
|
|
|
|
2010-05-08 16:27:49 -04:00
|
|
|
t = Topic.new("title" => "valid", "content" => "whatever")
|
2009-03-20 11:07:49 -04:00
|
|
|
assert t.valid?
|
|
|
|
|
|
|
|
t.title = "not"
|
2010-05-08 16:27:49 -04:00
|
|
|
assert t.invalid?
|
2009-03-20 13:36:22 -04:00
|
|
|
assert t.errors[:title].any?
|
|
|
|
assert_equal ["is too short (minimum is 5 characters)"], t.errors[:title]
|
2009-03-20 11:07:49 -04:00
|
|
|
|
|
|
|
t.title = ""
|
2010-05-08 16:27:49 -04:00
|
|
|
assert t.invalid?
|
2009-03-20 13:36:22 -04:00
|
|
|
assert t.errors[:title].any?
|
|
|
|
assert_equal ["is too short (minimum is 5 characters)"], t.errors[:title]
|
2009-03-20 11:07:49 -04:00
|
|
|
|
|
|
|
t.title = nil
|
2010-05-08 16:27:49 -04:00
|
|
|
assert t.invalid?
|
2009-03-20 13:36:22 -04:00
|
|
|
assert t.errors[:title].any?
|
2009-03-20 11:07:49 -04:00
|
|
|
assert_equal ["is too short (minimum is 5 characters)"], t.errors["title"]
|
|
|
|
end
|
|
|
|
|
2009-08-05 21:17:59 -04:00
|
|
|
def test_validates_length_of_using_maximum_should_allow_nil
|
2013-02-04 06:59:22 -05:00
|
|
|
Topic.validates_length_of :title, maximum: 10
|
2010-05-08 16:27:49 -04:00
|
|
|
t = Topic.new
|
2009-08-05 21:17:59 -04:00
|
|
|
assert t.valid?
|
|
|
|
end
|
|
|
|
|
2009-03-20 11:07:49 -04:00
|
|
|
def test_optionally_validates_length_of_using_minimum
|
2013-02-04 06:59:22 -05:00
|
|
|
Topic.validates_length_of :title, minimum: 5, allow_nil: true
|
2009-03-20 11:07:49 -04:00
|
|
|
|
2010-05-08 16:27:49 -04:00
|
|
|
t = Topic.new("title" => "valid", "content" => "whatever")
|
2009-03-20 11:07:49 -04:00
|
|
|
assert t.valid?
|
|
|
|
|
|
|
|
t.title = nil
|
|
|
|
assert t.valid?
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_validates_length_of_using_maximum
|
2013-02-04 06:59:22 -05:00
|
|
|
Topic.validates_length_of :title, maximum: 5
|
2009-03-20 11:07:49 -04:00
|
|
|
|
2010-05-08 16:27:49 -04:00
|
|
|
t = Topic.new("title" => "valid", "content" => "whatever")
|
2009-03-20 11:07:49 -04:00
|
|
|
assert t.valid?
|
|
|
|
|
|
|
|
t.title = "notvalid"
|
2010-05-08 16:27:49 -04:00
|
|
|
assert t.invalid?
|
2009-03-20 13:36:22 -04:00
|
|
|
assert t.errors[:title].any?
|
|
|
|
assert_equal ["is too long (maximum is 5 characters)"], t.errors[:title]
|
2009-03-20 11:07:49 -04:00
|
|
|
|
|
|
|
t.title = ""
|
|
|
|
assert t.valid?
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_optionally_validates_length_of_using_maximum
|
2013-02-04 06:59:22 -05:00
|
|
|
Topic.validates_length_of :title, maximum: 5, allow_nil: true
|
2009-03-20 11:07:49 -04:00
|
|
|
|
2010-05-08 16:27:49 -04:00
|
|
|
t = Topic.new("title" => "valid", "content" => "whatever")
|
2009-03-20 11:07:49 -04:00
|
|
|
assert t.valid?
|
|
|
|
|
|
|
|
t.title = nil
|
|
|
|
assert t.valid?
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_validates_length_of_using_within
|
2013-02-04 06:59:22 -05:00
|
|
|
Topic.validates_length_of(:title, :content, within: 3..5)
|
2009-03-20 11:07:49 -04:00
|
|
|
|
|
|
|
t = Topic.new("title" => "a!", "content" => "I'm ooooooooh so very long")
|
2010-05-08 16:27:49 -04:00
|
|
|
assert t.invalid?
|
2009-03-20 13:36:22 -04:00
|
|
|
assert_equal ["is too short (minimum is 3 characters)"], t.errors[:title]
|
|
|
|
assert_equal ["is too long (maximum is 5 characters)"], t.errors[:content]
|
2009-03-20 11:07:49 -04:00
|
|
|
|
|
|
|
t.title = nil
|
|
|
|
t.content = nil
|
2010-05-08 16:27:49 -04:00
|
|
|
assert t.invalid?
|
2009-03-20 13:36:22 -04:00
|
|
|
assert_equal ["is too short (minimum is 3 characters)"], t.errors[:title]
|
|
|
|
assert_equal ["is too short (minimum is 3 characters)"], t.errors[:content]
|
2009-03-20 11:07:49 -04:00
|
|
|
|
|
|
|
t.title = "abe"
|
|
|
|
t.content = "mad"
|
|
|
|
assert t.valid?
|
|
|
|
end
|
|
|
|
|
2009-08-08 17:35:03 -04:00
|
|
|
def test_validates_length_of_using_within_with_exclusive_range
|
2013-02-04 06:59:22 -05:00
|
|
|
Topic.validates_length_of(:title, within: 4...10)
|
2009-08-08 17:35:03 -04:00
|
|
|
|
|
|
|
t = Topic.new("title" => "9 chars!!")
|
|
|
|
assert t.valid?
|
|
|
|
|
|
|
|
t.title = "Now I'm 10"
|
2010-05-08 16:27:49 -04:00
|
|
|
assert t.invalid?
|
2009-08-08 17:35:03 -04:00
|
|
|
assert_equal ["is too long (maximum is 9 characters)"], t.errors[:title]
|
|
|
|
|
|
|
|
t.title = "Four"
|
|
|
|
assert t.valid?
|
|
|
|
end
|
|
|
|
|
2009-03-20 11:07:49 -04:00
|
|
|
def test_optionally_validates_length_of_using_within
|
2013-02-04 06:59:22 -05:00
|
|
|
Topic.validates_length_of :title, :content, within: 3..5, allow_nil: true
|
2009-03-20 11:07:49 -04:00
|
|
|
|
2010-05-08 16:27:49 -04:00
|
|
|
t = Topic.new('title' => 'abc', 'content' => 'abcd')
|
2009-03-20 11:07:49 -04:00
|
|
|
assert t.valid?
|
|
|
|
|
|
|
|
t.title = nil
|
|
|
|
assert t.valid?
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_validates_length_of_using_is
|
2013-02-04 06:59:22 -05:00
|
|
|
Topic.validates_length_of :title, is: 5
|
2009-03-20 11:07:49 -04:00
|
|
|
|
2010-05-08 16:27:49 -04:00
|
|
|
t = Topic.new("title" => "valid", "content" => "whatever")
|
2009-03-20 11:07:49 -04:00
|
|
|
assert t.valid?
|
|
|
|
|
|
|
|
t.title = "notvalid"
|
2010-05-08 16:27:49 -04:00
|
|
|
assert t.invalid?
|
2009-03-20 13:36:22 -04:00
|
|
|
assert t.errors[:title].any?
|
|
|
|
assert_equal ["is the wrong length (should be 5 characters)"], t.errors[:title]
|
2009-03-20 11:07:49 -04:00
|
|
|
|
|
|
|
t.title = ""
|
2010-05-08 16:27:49 -04:00
|
|
|
assert t.invalid?
|
2009-03-20 11:07:49 -04:00
|
|
|
|
|
|
|
t.title = nil
|
2010-05-08 16:27:49 -04:00
|
|
|
assert t.invalid?
|
2009-03-20 11:07:49 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_optionally_validates_length_of_using_is
|
2013-02-04 06:59:22 -05:00
|
|
|
Topic.validates_length_of :title, is: 5, allow_nil: true
|
2009-03-20 11:07:49 -04:00
|
|
|
|
2010-05-08 16:27:49 -04:00
|
|
|
t = Topic.new("title" => "valid", "content" => "whatever")
|
2009-03-20 11:07:49 -04:00
|
|
|
assert t.valid?
|
|
|
|
|
|
|
|
t.title = nil
|
|
|
|
assert t.valid?
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_validates_length_of_using_bignum
|
|
|
|
bigmin = 2 ** 30
|
|
|
|
bigmax = 2 ** 32
|
|
|
|
bigrange = bigmin...bigmax
|
|
|
|
assert_nothing_raised do
|
2013-02-04 06:59:22 -05:00
|
|
|
Topic.validates_length_of :title, is: bigmin + 5
|
|
|
|
Topic.validates_length_of :title, within: bigrange
|
|
|
|
Topic.validates_length_of :title, in: bigrange
|
|
|
|
Topic.validates_length_of :title, minimum: bigmin
|
|
|
|
Topic.validates_length_of :title, maximum: bigmax
|
2009-03-20 11:07:49 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_validates_length_of_nasty_params
|
2013-02-04 06:59:22 -05:00
|
|
|
assert_raise(ArgumentError) { Topic.validates_length_of(:title, is: -6) }
|
|
|
|
assert_raise(ArgumentError) { Topic.validates_length_of(:title, within: 6) }
|
|
|
|
assert_raise(ArgumentError) { Topic.validates_length_of(:title, minimum: "a") }
|
|
|
|
assert_raise(ArgumentError) { Topic.validates_length_of(:title, maximum: "a") }
|
|
|
|
assert_raise(ArgumentError) { Topic.validates_length_of(:title, within: "a") }
|
|
|
|
assert_raise(ArgumentError) { Topic.validates_length_of(:title, is: "a") }
|
2009-03-20 11:07:49 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_validates_length_of_custom_errors_for_minimum_with_message
|
2013-02-04 06:59:22 -05:00
|
|
|
Topic.validates_length_of( :title, minimum: 5, message: "boo %{count}" )
|
2010-05-08 16:27:49 -04:00
|
|
|
t = Topic.new("title" => "uhoh", "content" => "whatever")
|
|
|
|
assert t.invalid?
|
2009-03-20 13:36:22 -04:00
|
|
|
assert t.errors[:title].any?
|
|
|
|
assert_equal ["boo 5"], t.errors[:title]
|
2009-03-20 11:07:49 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_validates_length_of_custom_errors_for_minimum_with_too_short
|
2013-02-04 06:59:22 -05:00
|
|
|
Topic.validates_length_of( :title, minimum: 5, too_short: "hoo %{count}" )
|
2010-05-08 16:27:49 -04:00
|
|
|
t = Topic.new("title" => "uhoh", "content" => "whatever")
|
|
|
|
assert t.invalid?
|
2009-03-20 13:36:22 -04:00
|
|
|
assert t.errors[:title].any?
|
|
|
|
assert_equal ["hoo 5"], t.errors[:title]
|
2009-03-20 11:07:49 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_validates_length_of_custom_errors_for_maximum_with_message
|
2013-02-04 06:59:22 -05:00
|
|
|
Topic.validates_length_of( :title, maximum: 5, message: "boo %{count}" )
|
2010-05-08 16:27:49 -04:00
|
|
|
t = Topic.new("title" => "uhohuhoh", "content" => "whatever")
|
|
|
|
assert t.invalid?
|
2009-03-20 13:36:22 -04:00
|
|
|
assert t.errors[:title].any?
|
2009-03-20 11:07:49 -04:00
|
|
|
assert_equal ["boo 5"], t.errors[:title]
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_validates_length_of_custom_errors_for_in
|
2013-02-04 06:59:22 -05:00
|
|
|
Topic.validates_length_of(:title, in: 10..20, message: "hoo %{count}")
|
2010-05-08 16:27:49 -04:00
|
|
|
t = Topic.new("title" => "uhohuhoh", "content" => "whatever")
|
|
|
|
assert t.invalid?
|
2009-03-20 11:07:49 -04:00
|
|
|
assert t.errors[:title].any?
|
|
|
|
assert_equal ["hoo 10"], t.errors["title"]
|
|
|
|
|
2010-05-08 16:27:49 -04:00
|
|
|
t = Topic.new("title" => "uhohuhohuhohuhohuhohuhohuhohuhoh", "content" => "whatever")
|
|
|
|
assert t.invalid?
|
2009-03-20 11:07:49 -04:00
|
|
|
assert t.errors[:title].any?
|
|
|
|
assert_equal ["hoo 20"], t.errors["title"]
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_validates_length_of_custom_errors_for_maximum_with_too_long
|
2013-02-04 06:59:22 -05:00
|
|
|
Topic.validates_length_of( :title, maximum: 5, too_long: "hoo %{count}" )
|
2010-05-08 16:27:49 -04:00
|
|
|
t = Topic.new("title" => "uhohuhoh", "content" => "whatever")
|
|
|
|
assert t.invalid?
|
2009-03-20 11:07:49 -04:00
|
|
|
assert t.errors[:title].any?
|
|
|
|
assert_equal ["hoo 5"], t.errors["title"]
|
|
|
|
end
|
|
|
|
|
2010-08-03 06:58:55 -04:00
|
|
|
def test_validates_length_of_custom_errors_for_both_too_short_and_too_long
|
2013-02-04 06:59:22 -05:00
|
|
|
Topic.validates_length_of :title, minimum: 3, maximum: 5, too_short: 'too short', too_long: 'too long'
|
2010-08-03 06:58:55 -04:00
|
|
|
|
2013-02-04 06:59:22 -05:00
|
|
|
t = Topic.new(title: 'a')
|
2010-08-03 06:58:55 -04:00
|
|
|
assert t.invalid?
|
|
|
|
assert t.errors[:title].any?
|
|
|
|
assert_equal ['too short'], t.errors['title']
|
|
|
|
|
2013-02-04 06:59:22 -05:00
|
|
|
t = Topic.new(title: 'aaaaaa')
|
2010-08-03 06:58:55 -04:00
|
|
|
assert t.invalid?
|
|
|
|
assert t.errors[:title].any?
|
|
|
|
assert_equal ['too long'], t.errors['title']
|
|
|
|
end
|
|
|
|
|
2009-03-20 11:07:49 -04:00
|
|
|
def test_validates_length_of_custom_errors_for_is_with_message
|
2013-02-04 06:59:22 -05:00
|
|
|
Topic.validates_length_of( :title, is: 5, message: "boo %{count}" )
|
2010-05-08 16:27:49 -04:00
|
|
|
t = Topic.new("title" => "uhohuhoh", "content" => "whatever")
|
|
|
|
assert t.invalid?
|
2009-03-20 11:07:49 -04:00
|
|
|
assert t.errors[:title].any?
|
|
|
|
assert_equal ["boo 5"], t.errors["title"]
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_validates_length_of_custom_errors_for_is_with_wrong_length
|
2013-02-04 06:59:22 -05:00
|
|
|
Topic.validates_length_of( :title, is: 5, wrong_length: "hoo %{count}" )
|
2010-05-08 16:27:49 -04:00
|
|
|
t = Topic.new("title" => "uhohuhoh", "content" => "whatever")
|
|
|
|
assert t.invalid?
|
2009-03-20 11:07:49 -04:00
|
|
|
assert t.errors[:title].any?
|
|
|
|
assert_equal ["hoo 5"], t.errors["title"]
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_validates_length_of_using_minimum_utf8
|
2013-02-04 06:59:22 -05:00
|
|
|
Topic.validates_length_of :title, minimum: 5
|
2009-03-20 11:07:49 -04:00
|
|
|
|
2011-12-20 12:33:28 -05:00
|
|
|
t = Topic.new("title" => "一二三四五", "content" => "whatever")
|
|
|
|
assert t.valid?
|
2009-03-20 11:07:49 -04:00
|
|
|
|
2011-12-20 12:33:28 -05:00
|
|
|
t.title = "一二三四"
|
|
|
|
assert t.invalid?
|
|
|
|
assert t.errors[:title].any?
|
|
|
|
assert_equal ["is too short (minimum is 5 characters)"], t.errors["title"]
|
2009-03-20 11:07:49 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_validates_length_of_using_maximum_utf8
|
2013-02-04 06:59:22 -05:00
|
|
|
Topic.validates_length_of :title, maximum: 5
|
2009-03-20 11:07:49 -04:00
|
|
|
|
2011-12-20 12:33:28 -05:00
|
|
|
t = Topic.new("title" => "一二三四五", "content" => "whatever")
|
|
|
|
assert t.valid?
|
2009-03-20 11:07:49 -04:00
|
|
|
|
2011-12-20 12:33:28 -05:00
|
|
|
t.title = "一二34五六"
|
|
|
|
assert t.invalid?
|
|
|
|
assert t.errors[:title].any?
|
|
|
|
assert_equal ["is too long (maximum is 5 characters)"], t.errors["title"]
|
2009-03-20 11:07:49 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_validates_length_of_using_within_utf8
|
2013-02-04 06:59:22 -05:00
|
|
|
Topic.validates_length_of(:title, :content, within: 3..5)
|
2011-12-20 12:33:28 -05:00
|
|
|
|
|
|
|
t = Topic.new("title" => "一二", "content" => "12三四五六七")
|
|
|
|
assert t.invalid?
|
|
|
|
assert_equal ["is too short (minimum is 3 characters)"], t.errors[:title]
|
|
|
|
assert_equal ["is too long (maximum is 5 characters)"], t.errors[:content]
|
|
|
|
t.title = "一二三"
|
|
|
|
t.content = "12三"
|
|
|
|
assert t.valid?
|
2009-03-20 11:07:49 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_optionally_validates_length_of_using_within_utf8
|
2013-02-04 06:59:22 -05:00
|
|
|
Topic.validates_length_of :title, within: 3..5, allow_nil: true
|
2009-03-20 11:07:49 -04:00
|
|
|
|
2013-02-04 06:59:22 -05:00
|
|
|
t = Topic.new(title: "一二三四五")
|
2011-12-20 12:33:28 -05:00
|
|
|
assert t.valid?, t.errors.inspect
|
2009-03-20 11:07:49 -04:00
|
|
|
|
2013-02-04 06:59:22 -05:00
|
|
|
t = Topic.new(title: "一二三")
|
2011-12-20 12:33:28 -05:00
|
|
|
assert t.valid?, t.errors.inspect
|
2009-03-20 11:07:49 -04:00
|
|
|
|
2011-12-20 12:33:28 -05:00
|
|
|
t.title = nil
|
|
|
|
assert t.valid?, t.errors.inspect
|
2009-03-20 11:07:49 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_validates_length_of_using_is_utf8
|
2013-02-04 06:59:22 -05:00
|
|
|
Topic.validates_length_of :title, is: 5
|
2009-03-20 11:07:49 -04:00
|
|
|
|
2011-12-20 12:33:28 -05:00
|
|
|
t = Topic.new("title" => "一二345", "content" => "whatever")
|
|
|
|
assert t.valid?
|
2009-03-20 11:07:49 -04:00
|
|
|
|
2011-12-20 12:33:28 -05:00
|
|
|
t.title = "一二345六"
|
|
|
|
assert t.invalid?
|
|
|
|
assert t.errors[:title].any?
|
|
|
|
assert_equal ["is the wrong length (should be 5 characters)"], t.errors["title"]
|
2009-03-20 11:07:49 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_validates_length_of_with_block
|
2013-02-04 06:59:22 -05:00
|
|
|
Topic.validates_length_of :content, minimum: 5, too_short: "Your essay must be at least %{count} words.",
|
|
|
|
tokenizer: lambda {|str| str.scan(/\w+/) }
|
|
|
|
t = Topic.new(content: "this content should be long enough")
|
2009-03-20 11:07:49 -04:00
|
|
|
assert t.valid?
|
|
|
|
|
|
|
|
t.content = "not long enough"
|
2010-05-08 16:27:49 -04:00
|
|
|
assert t.invalid?
|
2009-03-20 11:07:49 -04:00
|
|
|
assert t.errors[:content].any?
|
|
|
|
assert_equal ["Your essay must be at least 5 words."], t.errors[:content]
|
|
|
|
end
|
2011-03-12 18:10:19 -05:00
|
|
|
|
2011-03-10 12:50:55 -05:00
|
|
|
def test_validates_length_of_for_fixnum
|
2013-02-04 06:59:22 -05:00
|
|
|
Topic.validates_length_of(:approved, is: 4)
|
2011-03-12 18:10:19 -05:00
|
|
|
|
2013-02-04 06:59:22 -05:00
|
|
|
t = Topic.new("title" => "uhohuhoh", "content" => "whatever", approved: 1)
|
2011-03-10 12:50:55 -05:00
|
|
|
assert t.invalid?
|
|
|
|
assert t.errors[:approved].any?
|
2011-03-12 18:10:19 -05:00
|
|
|
|
2013-02-04 06:59:22 -05:00
|
|
|
t = Topic.new("title" => "uhohuhoh", "content" => "whatever", approved: 1234)
|
2011-03-10 12:50:55 -05:00
|
|
|
assert t.valid?
|
|
|
|
end
|
2009-03-20 11:07:49 -04:00
|
|
|
|
2009-03-20 18:21:27 -04:00
|
|
|
def test_validates_length_of_for_ruby_class
|
2013-02-04 06:59:22 -05:00
|
|
|
Person.validates_length_of :karma, minimum: 5
|
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 too short (minimum is 5 characters)"], p.errors[:karma]
|
2009-03-20 18:21:27 -04:00
|
|
|
|
2009-12-23 07:30:58 -05:00
|
|
|
p.karma = "The Smiths"
|
|
|
|
assert p.valid?
|
|
|
|
ensure
|
|
|
|
Person.reset_callbacks(:validate)
|
2009-03-20 18:21:27 -04:00
|
|
|
end
|
2012-02-06 06:56:38 -05:00
|
|
|
|
|
|
|
def test_validates_length_of_for_infinite_maxima
|
2013-02-04 06:59:22 -05:00
|
|
|
Topic.validates_length_of(:title, within: 5..Float::INFINITY)
|
2012-02-06 06:56:38 -05:00
|
|
|
|
|
|
|
t = Topic.new("title" => "1234")
|
|
|
|
assert t.invalid?
|
|
|
|
assert t.errors[:title].any?
|
|
|
|
|
|
|
|
t.title = "12345"
|
|
|
|
assert t.valid?
|
|
|
|
|
2013-02-04 06:59:22 -05:00
|
|
|
Topic.validates_length_of(:author_name, maximum: Float::INFINITY)
|
2012-02-06 06:56:38 -05:00
|
|
|
|
|
|
|
assert t.valid?
|
|
|
|
|
|
|
|
t.author_name = "A very long author name that should still be valid." * 100
|
|
|
|
assert t.valid?
|
|
|
|
end
|
2012-08-07 07:39:21 -04:00
|
|
|
|
|
|
|
def test_validates_length_of_using_maximum_should_not_allow_nil_when_nil_not_allowed
|
2013-02-04 06:59:22 -05:00
|
|
|
Topic.validates_length_of :title, maximum: 10, allow_nil: false
|
2012-08-07 07:39:21 -04:00
|
|
|
t = Topic.new
|
|
|
|
assert t.invalid?
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_validates_length_of_using_maximum_should_not_allow_nil_and_empty_string_when_blank_not_allowed
|
2013-02-04 06:59:22 -05:00
|
|
|
Topic.validates_length_of :title, maximum: 10, allow_blank: false
|
2012-08-07 07:39:21 -04:00
|
|
|
t = Topic.new
|
|
|
|
assert t.invalid?
|
|
|
|
|
|
|
|
t.title = ""
|
|
|
|
assert t.invalid?
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_validates_length_of_using_both_minimum_and_maximum_should_not_allow_nil
|
2013-02-04 06:59:22 -05:00
|
|
|
Topic.validates_length_of :title, minimum: 5, maximum: 10
|
2012-08-07 07:39:21 -04:00
|
|
|
t = Topic.new
|
|
|
|
assert t.invalid?
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_validates_length_of_using_minimum_0_should_not_allow_nil
|
2013-02-04 06:59:22 -05:00
|
|
|
Topic.validates_length_of :title, minimum: 0
|
2012-08-07 07:39:21 -04:00
|
|
|
t = Topic.new
|
|
|
|
assert t.invalid?
|
|
|
|
|
|
|
|
t.title = ""
|
|
|
|
assert t.valid?
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_validates_length_of_using_is_0_should_not_allow_nil
|
2013-02-04 06:59:22 -05:00
|
|
|
Topic.validates_length_of :title, is: 0
|
2012-08-07 07:39:21 -04:00
|
|
|
t = Topic.new
|
|
|
|
assert t.invalid?
|
|
|
|
|
|
|
|
t.title = ""
|
|
|
|
assert t.valid?
|
|
|
|
end
|
2013-02-02 00:00:52 -05:00
|
|
|
|
|
|
|
def test_validates_with_diff_in_option
|
2013-02-04 06:59:22 -05:00
|
|
|
Topic.validates_length_of(:title, is: 5)
|
|
|
|
Topic.validates_length_of(:title, is: 5, if: Proc.new { false } )
|
2013-02-02 00:00:52 -05:00
|
|
|
|
|
|
|
assert Topic.new("title" => "david").valid?
|
|
|
|
assert Topic.new("title" => "david2").invalid?
|
|
|
|
end
|
2009-03-20 11:07:49 -04:00
|
|
|
end
|