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 PresenceValidationTest < ActiveModel::TestCase
|
|
|
|
|
2009-12-23 07:30:58 -05:00
|
|
|
def teardown
|
2014-01-27 05:22:26 -05:00
|
|
|
Topic.clear_validators!
|
2009-12-23 07:30:58 -05:00
|
|
|
end
|
2009-03-20 11:07:49 -04:00
|
|
|
|
|
|
|
def test_validate_format
|
2013-05-01 20:10:06 -04:00
|
|
|
Topic.validates_format_of(:title, :content, with: /\AValidation\smacros \w+!\z/, message: "is bad data")
|
2009-03-20 11:07:49 -04:00
|
|
|
|
2010-05-08 16:27:49 -04:00
|
|
|
t = Topic.new("title" => "i'm incorrect", "content" => "Validation macros rule!")
|
|
|
|
assert t.invalid?, "Shouldn't be valid"
|
2009-03-20 11:07:49 -04:00
|
|
|
assert_equal ["is bad data"], t.errors[:title]
|
|
|
|
assert t.errors[:content].empty?
|
|
|
|
|
|
|
|
t.title = "Validation macros rule!"
|
|
|
|
|
2010-05-08 16:27:49 -04:00
|
|
|
assert t.valid?
|
2009-03-20 11:07:49 -04:00
|
|
|
assert t.errors[:title].empty?
|
|
|
|
|
|
|
|
assert_raise(ArgumentError) { Topic.validates_format_of(:title, :content) }
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_validate_format_with_allow_blank
|
2013-05-01 20:10:06 -04:00
|
|
|
Topic.validates_format_of(:title, with: /\AValidation\smacros \w+!\z/, allow_blank: true)
|
2010-05-08 16:27:49 -04:00
|
|
|
assert Topic.new("title" => "Shouldn't be valid").invalid?
|
|
|
|
assert Topic.new("title" => "").valid?
|
|
|
|
assert Topic.new("title" => nil).valid?
|
|
|
|
assert Topic.new("title" => "Validation macros rule!").valid?
|
2009-03-20 11:07:49 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
# testing ticket #3142
|
|
|
|
def test_validate_format_numeric
|
2013-05-01 20:10:06 -04:00
|
|
|
Topic.validates_format_of(:title, :content, with: /\A[1-9][0-9]*\z/, message: "is bad data")
|
2009-03-20 11:07:49 -04:00
|
|
|
|
2010-05-08 16:27:49 -04:00
|
|
|
t = Topic.new("title" => "72x", "content" => "6789")
|
|
|
|
assert t.invalid?, "Shouldn't be valid"
|
|
|
|
|
2009-03-20 11:07:49 -04:00
|
|
|
assert_equal ["is bad data"], t.errors[:title]
|
|
|
|
assert t.errors[:content].empty?
|
|
|
|
|
|
|
|
t.title = "-11"
|
2010-05-08 16:27:49 -04:00
|
|
|
assert t.invalid?, "Shouldn't be valid"
|
2009-03-20 11:07:49 -04:00
|
|
|
|
|
|
|
t.title = "03"
|
2010-05-08 16:27:49 -04:00
|
|
|
assert t.invalid?, "Shouldn't be valid"
|
2009-03-20 11:07:49 -04:00
|
|
|
|
|
|
|
t.title = "z44"
|
2010-05-08 16:27:49 -04:00
|
|
|
assert t.invalid?, "Shouldn't be valid"
|
2009-03-20 11:07:49 -04:00
|
|
|
|
|
|
|
t.title = "5v7"
|
2010-05-08 16:27:49 -04:00
|
|
|
assert t.invalid?, "Shouldn't be valid"
|
2009-03-20 11:07:49 -04:00
|
|
|
|
|
|
|
t.title = "1"
|
|
|
|
|
2010-05-08 16:27:49 -04:00
|
|
|
assert t.valid?
|
2009-03-20 11:07:49 -04:00
|
|
|
assert t.errors[:title].empty?
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_validate_format_with_formatted_message
|
2013-05-01 20:10:06 -04:00
|
|
|
Topic.validates_format_of(:title, with: /\AValid Title\z/, message: "can't be %{value}")
|
|
|
|
t = Topic.new(title: 'Invalid title')
|
2010-05-08 16:27:49 -04:00
|
|
|
assert t.invalid?
|
2009-03-20 13:36:22 -04:00
|
|
|
assert_equal ["can't be Invalid title"], t.errors[:title]
|
2009-03-20 11:07:49 -04:00
|
|
|
end
|
2014-01-27 05:22:26 -05:00
|
|
|
|
2012-06-07 18:08:51 -04:00
|
|
|
def test_validate_format_of_with_multiline_regexp_should_raise_error
|
2013-05-01 20:10:06 -04:00
|
|
|
assert_raise(ArgumentError) { Topic.validates_format_of(:title, with: /^Valid Title$/) }
|
2012-06-07 18:08:51 -04:00
|
|
|
end
|
2014-01-27 05:22:26 -05:00
|
|
|
|
2012-06-07 18:08:51 -04:00
|
|
|
def test_validate_format_of_with_multiline_regexp_and_option
|
|
|
|
assert_nothing_raised(ArgumentError) do
|
2013-05-01 20:10:06 -04:00
|
|
|
Topic.validates_format_of(:title, with: /^Valid Title$/, multiline: true)
|
2012-06-07 18:08:51 -04:00
|
|
|
end
|
|
|
|
end
|
2009-03-20 11:07:49 -04:00
|
|
|
|
2009-08-09 22:47:32 -04:00
|
|
|
def test_validate_format_with_not_option
|
2013-05-01 20:10:06 -04:00
|
|
|
Topic.validates_format_of(:title, without: /foo/, message: "should not contain foo")
|
2009-08-09 22:47:32 -04:00
|
|
|
t = Topic.new
|
|
|
|
|
|
|
|
t.title = "foobar"
|
|
|
|
t.valid?
|
|
|
|
assert_equal ["should not contain foo"], t.errors[:title]
|
|
|
|
|
|
|
|
t.title = "something else"
|
|
|
|
t.valid?
|
|
|
|
assert_equal [], t.errors[:title]
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_validate_format_of_without_any_regexp_should_raise_error
|
|
|
|
assert_raise(ArgumentError) { Topic.validates_format_of(:title) }
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_validates_format_of_with_both_regexps_should_raise_error
|
2013-05-01 20:10:06 -04:00
|
|
|
assert_raise(ArgumentError) { Topic.validates_format_of(:title, with: /this/, without: /that/) }
|
2009-08-09 22:47:32 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_validates_format_of_when_with_isnt_a_regexp_should_raise_error
|
2013-05-01 20:10:06 -04:00
|
|
|
assert_raise(ArgumentError) { Topic.validates_format_of(:title, with: "clearly not a regexp") }
|
2009-08-09 22:47:32 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_validates_format_of_when_not_isnt_a_regexp_should_raise_error
|
2013-05-01 20:10:06 -04:00
|
|
|
assert_raise(ArgumentError) { Topic.validates_format_of(:title, without: "clearly not a regexp") }
|
2009-08-09 22:47:32 -04:00
|
|
|
end
|
|
|
|
|
2011-03-11 00:35:23 -05:00
|
|
|
def test_validates_format_of_with_lambda
|
2013-05-01 20:10:06 -04:00
|
|
|
Topic.validates_format_of :content, with: lambda { |topic| topic.title == "digit" ? /\A\d+\Z/ : /\A\S+\Z/ }
|
2011-03-11 00:35:23 -05:00
|
|
|
|
2011-11-18 11:51:05 -05:00
|
|
|
t = Topic.new
|
|
|
|
t.title = "digit"
|
|
|
|
t.content = "Pixies"
|
|
|
|
assert t.invalid?
|
2011-03-11 00:35:23 -05:00
|
|
|
|
2011-11-18 11:51:05 -05:00
|
|
|
t.content = "1234"
|
|
|
|
assert t.valid?
|
2011-03-11 00:35:23 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_validates_format_of_without_lambda
|
2013-05-01 20:10:06 -04:00
|
|
|
Topic.validates_format_of :content, without: lambda { |topic| topic.title == "characters" ? /\A\d+\Z/ : /\A\S+\Z/ }
|
2011-03-11 00:35:23 -05:00
|
|
|
|
2011-11-18 11:51:05 -05:00
|
|
|
t = Topic.new
|
|
|
|
t.title = "characters"
|
|
|
|
t.content = "1234"
|
|
|
|
assert t.invalid?
|
2011-03-11 00:35:23 -05:00
|
|
|
|
2011-11-18 11:51:05 -05:00
|
|
|
t.content = "Pixies"
|
|
|
|
assert t.valid?
|
2011-03-11 00:35:23 -05:00
|
|
|
end
|
|
|
|
|
2009-03-20 18:21:27 -04:00
|
|
|
def test_validates_format_of_for_ruby_class
|
2013-05-01 20:10:06 -04:00
|
|
|
Person.validates_format_of :karma, with: /\A\d+\Z/
|
2009-03-20 18:21:27 -04:00
|
|
|
|
2009-12-23 07:30:58 -05:00
|
|
|
p = Person.new
|
|
|
|
p.karma = "Pixies"
|
|
|
|
assert p.invalid?
|
2009-03-20 18:21:27 -04:00
|
|
|
|
2009-12-23 07:30:58 -05:00
|
|
|
assert_equal ["is invalid"], 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
|
2014-01-27 05:22:26 -05:00
|
|
|
Person.clear_validators!
|
2009-03-20 18:21:27 -04:00
|
|
|
end
|
2009-03-20 11:07:49 -04:00
|
|
|
end
|