Skip to content
This repository
tree: 35cb7722b2
Fetching contributors…

Cannot retrieve contributors at this time

file 121 lines (91 sloc) 3.857 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
# encoding: utf-8
require 'cases/helper'

require 'models/topic'
require 'models/person'

class InclusionValidationTest < ActiveModel::TestCase

  def teardown
    Topic.reset_callbacks(:validate)
  end

  def test_validates_inclusion_of_range
    Topic.validates_inclusion_of( :title, :in => 'aaa'..'bbb' )
    assert Topic.new("title" => "bbc", "content" => "abc").invalid?
    assert Topic.new("title" => "aa", "content" => "abc").invalid?
    assert Topic.new("title" => "aaa", "content" => "abc").valid?
    assert Topic.new("title" => "abc", "content" => "abc").valid?
    assert Topic.new("title" => "bbb", "content" => "abc").valid?
  end

  def test_validates_inclusion_of
    Topic.validates_inclusion_of( :title, :in => %w( a b c d e f g ) )

    assert Topic.new("title" => "a!", "content" => "abc").invalid?
    assert Topic.new("title" => "a b", "content" => "abc").invalid?
    assert Topic.new("title" => nil, "content" => "def").invalid?

    t = Topic.new("title" => "a", "content" => "I know you are but what am I?")
    assert t.valid?
    t.title = "uhoh"
    assert t.invalid?
    assert t.errors[:title].any?
    assert_equal ["is not included in the list"], t.errors[:title]

    assert_raise(ArgumentError) { Topic.validates_inclusion_of( :title, :in => nil ) }
    assert_raise(ArgumentError) { Topic.validates_inclusion_of( :title, :in => 0) }

    assert_nothing_raised(ArgumentError) { Topic.validates_inclusion_of( :title, :in => "hi!" ) }
    assert_nothing_raised(ArgumentError) { Topic.validates_inclusion_of( :title, :in => {} ) }
    assert_nothing_raised(ArgumentError) { Topic.validates_inclusion_of( :title, :in => [] ) }
  end

  def test_validates_inclusion_of_with_allow_nil
    Topic.validates_inclusion_of( :title, :in => %w( a b c d e f g ), :allow_nil => true )

    assert Topic.new("title" => "a!", "content" => "abc").invalid?
    assert Topic.new("title" => "", "content" => "abc").invalid?
    assert Topic.new("title" => nil, "content" => "abc").valid?
  end

  def test_validates_inclusion_of_with_formatted_message
    Topic.validates_inclusion_of( :title, :in => %w( a b c d e f g ), :message => "option %{value} is not in the list" )

    assert Topic.new("title" => "a", "content" => "abc").valid?

    t = Topic.new("title" => "uhoh", "content" => "abc")
    assert t.invalid?
    assert t.errors[:title].any?
    assert_equal ["option uhoh is not in the list"], t.errors[:title]
  end

  def test_validates_inclusion_of_with_within_option
    Topic.validates_inclusion_of( :title, :within => %w( a b c d e f g ) )

    assert Topic.new("title" => "a", "content" => "abc").valid?

    t = Topic.new("title" => "uhoh", "content" => "abc")
    assert t.invalid?
    assert t.errors[:title].any?
  end

  def test_validates_inclusion_of_for_ruby_class
    Person.validates_inclusion_of :karma, :in => %w( abe monkey )

    p = Person.new
    p.karma = "Lifo"
    assert p.invalid?

    assert_equal ["is not included in the list"], p.errors[:karma]

    p.karma = "monkey"
    assert p.valid?
  ensure
    Person.reset_callbacks(:validate)
  end

  def test_validates_inclusion_of_with_lambda
    Topic.validates_inclusion_of :title, :in => lambda{ |topic| topic.author_name == "sikachu" ? %w( monkey elephant ) : %w( abe wasabi ) }

    t = Topic.new
    t.title = "wasabi"
    t.author_name = "sikachu"
    assert t.invalid?

    t.title = "elephant"
    assert t.valid?
  end

  def test_validates_inclusion_of_with_symbol
    Person.validates_inclusion_of :karma, :in => :available_karmas

    p = Person.new
    p.karma = "Lifo"

    def p.available_karmas
      %w()
    end

    assert p.invalid?
    assert_equal ["is not included in the list"], p.errors[:karma]

    def p.available_karmas
      %w(Lifo)
    end

    assert p.valid?
  ensure
    Person.reset_callbacks(:validate)
  end
end
Something went wrong with that request. Please try again.