1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00
rails--rails/activemodel/test/cases/validations/exclusion_validation_test.rb
Prem Sichanugrist f6540211b5 Add :use_include option to allow user to explicitly use Range#include? method in Ruby 1.9
In Ruby 1.9 we're currently use `Range#cover?` to fix the performance problem. However, there might be the case that you want to use `Range#include?` instead. This patch will give you that option.
2011-04-10 18:49:28 +08:00

76 lines
2 KiB
Ruby

# encoding: utf-8
require 'cases/helper'
require 'models/topic'
require 'models/person'
class ExclusionValidationTest < ActiveModel::TestCase
def teardown
Topic.reset_callbacks(:validate)
end
def test_validates_exclusion_of
Topic.validates_exclusion_of( :title, :in => %w( abe monkey ) )
assert Topic.new("title" => "something", "content" => "abc").valid?
assert Topic.new("title" => "monkey", "content" => "abc").invalid?
end
def test_validates_exclusion_of_with_formatted_message
Topic.validates_exclusion_of( :title, :in => %w( abe monkey ), :message => "option %{value} is restricted" )
assert Topic.new("title" => "something", "content" => "abc")
t = Topic.new("title" => "monkey")
assert t.invalid?
assert t.errors[:title].any?
assert_equal ["option monkey is restricted"], t.errors[:title]
end
def test_validates_exclusion_of_for_ruby_class
Person.validates_exclusion_of :karma, :in => %w( abe monkey )
p = Person.new
p.karma = "abe"
assert p.invalid?
assert_equal ["is reserved"], p.errors[:karma]
p.karma = "Lifo"
assert p.valid?
ensure
Person.reset_callbacks(:validate)
end
def test_validates_exclusion_of_with_lambda
Topic.validates_exclusion_of :title, :in => lambda{ |topic| topic.author_name == "sikachu" ? %w( monkey elephant ) : %w( abe wasabi ) }
p = Topic.new
p.title = "elephant"
p.author_name = "sikachu"
assert p.invalid?
p.title = "wasabi"
assert p.valid?
end
def test_validates_exclustion_with_invalid_lambda_return
Topic.validates_exclusion_of :title, :in => lambda{ |topic| false }
p = Topic.new
p.title = "wasabi"
p.author_name = "sikachu"
assert_raise(ArgumentError){ p.valid? }
end
def test_validates_inclusion_with_explicit_include
range = (1..100)
Topic.validates_exclusion_of :title, :in => range, :use_include => true
range.expects(:include?).returns(false)
t = Topic.new
t.title = 102
assert t.valid?
end
end