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
|
|
|
|
Topic.reset_callbacks(:validate)
|
|
|
|
end
|
2009-03-20 11:07:49 -04:00
|
|
|
|
|
|
|
def test_validate_format
|
|
|
|
Topic.validates_format_of(:title, :content, :with => /^Validation\smacros \w+!$/, :message => "is bad data")
|
|
|
|
|
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
|
|
|
|
Topic.validates_format_of(:title, :with => /^Validation\smacros \w+!$/, :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
|
|
|
|
Topic.validates_format_of(:title, :content, :with => /^[1-9][0-9]*$/, :message => "is bad data")
|
|
|
|
|
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
|
2010-05-03 02:44:32 -04:00
|
|
|
Topic.validates_format_of(:title, :with => /^Valid Title$/, :message => "can't be %{value}")
|
2010-05-08 16:27:49 -04:00
|
|
|
t = Topic.new(:title => 'Invalid title')
|
|
|
|
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
|
|
|
|
|
2009-08-09 22:47:32 -04:00
|
|
|
def test_validate_format_with_not_option
|
|
|
|
Topic.validates_format_of(:title, :without => /foo/, :message => "should not contain foo")
|
|
|
|
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
|
|
|
|
assert_raise(ArgumentError) { Topic.validates_format_of(:title, :with => /this/, :without => /that/) }
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_validates_format_of_when_with_isnt_a_regexp_should_raise_error
|
|
|
|
assert_raise(ArgumentError) { Topic.validates_format_of(:title, :with => "clearly not a regexp") }
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_validates_format_of_when_not_isnt_a_regexp_should_raise_error
|
|
|
|
assert_raise(ArgumentError) { Topic.validates_format_of(:title, :without => "clearly not a regexp") }
|
|
|
|
end
|
|
|
|
|
2011-03-11 00:35:23 -05:00
|
|
|
def test_validates_format_of_with_lambda
|
|
|
|
Topic.validates_format_of :content, :with => lambda{ |topic| topic.title == "digit" ? /\A\d+\Z/ : /\A\S+\Z/ }
|
|
|
|
|
|
|
|
p = Topic.new
|
|
|
|
p.title = "digit"
|
|
|
|
p.content = "Pixies"
|
|
|
|
assert p.invalid?
|
|
|
|
|
|
|
|
p.content = "1234"
|
|
|
|
assert p.valid?
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_validates_format_of_without_lambda
|
|
|
|
Topic.validates_format_of :content, :without => lambda{ |topic| topic.title == "characters" ? /\A\d+\Z/ : /\A\S+\Z/ }
|
|
|
|
|
|
|
|
p = Topic.new
|
|
|
|
p.title = "characters"
|
|
|
|
p.content = "1234"
|
|
|
|
assert p.invalid?
|
|
|
|
|
|
|
|
p.content = "Pixies"
|
|
|
|
assert p.valid?
|
|
|
|
end
|
|
|
|
|
2009-03-20 18:21:27 -04:00
|
|
|
def test_validates_format_of_for_ruby_class
|
2009-12-23 07:30:58 -05: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
|
|
|
|
Person.reset_callbacks(:validate)
|
2009-03-20 18:21:27 -04:00
|
|
|
end
|
2009-03-20 11:07:49 -04:00
|
|
|
end
|