aboutsummaryrefslogtreecommitdiffstats
path: root/activemodel/test/cases/validations/format_validation_test.rb
blob: feae66602ceb54bfdf06ee320260542dae33a11b (plain) (blame)
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# frozen_string_literal: true

require "cases/helper"

require "models/topic"
require "models/person"

class FormatValidationTest < ActiveSupport::TestCase
  def teardown
    Topic.clear_validators!
  end

  def test_validate_format
    Topic.validates_format_of(:title, :content, with: /\AValidation\smacros \w+!\z/, message: "is bad data")

    t = Topic.new("title" => "i'm incorrect", "content" => "Validation macros rule!")
    assert t.invalid?, "Shouldn't be valid"
    assert_equal ["is bad data"], t.errors[:title]
    assert_empty t.errors[:content]

    t.title = "Validation macros rule!"

    assert_predicate t, :valid?
    assert_empty t.errors[:title]

    assert_raise(ArgumentError) { Topic.validates_format_of(:title, :content) }
  end

  def test_validate_format_with_allow_blank
    Topic.validates_format_of(:title, with: /\AValidation\smacros \w+!\z/, allow_blank: true)
    assert_predicate Topic.new("title" => "Shouldn't be valid"), :invalid?
    assert_predicate Topic.new("title" => ""), :valid?
    assert_predicate Topic.new("title" => nil), :valid?
    assert_predicate Topic.new("title" => "Validation macros rule!"), :valid?
  end

  # testing ticket #3142
  def test_validate_format_numeric
    Topic.validates_format_of(:title, :content, with: /\A[1-9][0-9]*\z/, message: "is bad data")

    t = Topic.new("title" => "72x", "content" => "6789")
    assert t.invalid?, "Shouldn't be valid"

    assert_equal ["is bad data"], t.errors[:title]
    assert_empty t.errors[:content]

    t.title = "-11"
    assert t.invalid?, "Shouldn't be valid"

    t.title = "03"
    assert t.invalid?, "Shouldn't be valid"

    t.title = "z44"
    assert t.invalid?, "Shouldn't be valid"

    t.title = "5v7"
    assert t.invalid?, "Shouldn't be valid"

    t.title = "1"

    assert_predicate t, :valid?
    assert_empty t.errors[:title]
  end

  def test_validate_format_with_formatted_message
    Topic.validates_format_of(:title, with: /\AValid Title\z/, message: "can't be %{value}")
    t = Topic.new(title: "Invalid title")
    assert_predicate t, :invalid?
    assert_equal ["can't be Invalid title"], t.errors[:title]
  end

  def test_validate_format_of_with_multiline_regexp_should_raise_error
    assert_raise(ArgumentError) { Topic.validates_format_of(:title, with: /^Valid Title$/) }
  end

  def test_validate_format_of_with_multiline_regexp_and_option
    assert_nothing_raised do
      Topic.validates_format_of(:title, with: /^Valid Title$/, multiline: true)
    end
  end

  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

  def test_validates_format_of_with_lambda
    Topic.validates_format_of :content, with: lambda { |topic| topic.title == "digit" ? /\A\d+\z/ : /\A\S+\z/ }

    t = Topic.new
    t.title = "digit"
    t.content = "Pixies"
    assert_predicate t, :invalid?

    t.content = "1234"
    assert_predicate t, :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/ }

    t = Topic.new
    t.title = "characters"
    t.content = "1234"
    assert_predicate t, :invalid?

    t.content = "Pixies"
    assert_predicate t, :valid?
  end

  def test_validates_format_of_for_ruby_class
    Person.validates_format_of :karma, with: /\A\d+\z/

    p = Person.new
    p.karma = "Pixies"
    assert_predicate p, :invalid?

    assert_equal ["is invalid"], p.errors[:karma]

    p.karma = "1234"
    assert_predicate p, :valid?
  ensure
    Person.clear_validators!
  end
end