length_validation_test.rb 12.2 KB
Newer Older
1
require "cases/helper"
2

3 4
require "models/topic"
require "models/person"
5 6

class LengthValidationTest < ActiveModel::TestCase
7
  def teardown
8
    Topic.clear_validators!
9
  end
10 11

  def test_validates_length_of_with_allow_nil
12
    Topic.validates_length_of(:title, is: 5, allow_nil: true)
13

14 15 16 17
    assert Topic.new("title" => "ab").invalid?
    assert Topic.new("title" => "").invalid?
    assert Topic.new("title" => nil).valid?
    assert Topic.new("title" => "abcde").valid?
18 19 20
  end

  def test_validates_length_of_with_allow_blank
21
    Topic.validates_length_of(:title, is: 5, allow_blank: true)
22

23 24 25 26
    assert Topic.new("title" => "ab").invalid?
    assert Topic.new("title" => "").valid?
    assert Topic.new("title" => nil).valid?
    assert Topic.new("title" => "abcde").valid?
27 28 29
  end

  def test_validates_length_of_using_minimum
30
    Topic.validates_length_of :title, minimum: 5
31

32
    t = Topic.new("title" => "valid", "content" => "whatever")
33 34 35
    assert t.valid?

    t.title = "not"
36
    assert t.invalid?
37 38
    assert t.errors[:title].any?
    assert_equal ["is too short (minimum is 5 characters)"], t.errors[:title]
39 40

    t.title = ""
41
    assert t.invalid?
42 43
    assert t.errors[:title].any?
    assert_equal ["is too short (minimum is 5 characters)"], t.errors[:title]
44 45

    t.title = nil
46
    assert t.invalid?
47
    assert t.errors[:title].any?
48 49 50
    assert_equal ["is too short (minimum is 5 characters)"], t.errors["title"]
  end

51
  def test_validates_length_of_using_maximum_should_allow_nil
52
    Topic.validates_length_of :title, maximum: 10
53
    t = Topic.new
54 55 56
    assert t.valid?
  end

57
  def test_optionally_validates_length_of_using_minimum
58
    Topic.validates_length_of :title, minimum: 5, allow_nil: true
59

60
    t = Topic.new("title" => "valid", "content" => "whatever")
61 62 63 64 65 66 67
    assert t.valid?

    t.title = nil
    assert t.valid?
  end

  def test_validates_length_of_using_maximum
68
    Topic.validates_length_of :title, maximum: 5
69

70
    t = Topic.new("title" => "valid", "content" => "whatever")
71 72 73
    assert t.valid?

    t.title = "notvalid"
74
    assert t.invalid?
75 76
    assert t.errors[:title].any?
    assert_equal ["is too long (maximum is 5 characters)"], t.errors[:title]
77 78 79 80 81 82

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

  def test_optionally_validates_length_of_using_maximum
83
    Topic.validates_length_of :title, maximum: 5, allow_nil: true
84

85
    t = Topic.new("title" => "valid", "content" => "whatever")
86 87 88 89 90 91 92
    assert t.valid?

    t.title = nil
    assert t.valid?
  end

  def test_validates_length_of_using_within
93
    Topic.validates_length_of(:title, :content, within: 3..5)
94 95

    t = Topic.new("title" => "a!", "content" => "I'm ooooooooh so very long")
96
    assert t.invalid?
97 98
    assert_equal ["is too short (minimum is 3 characters)"], t.errors[:title]
    assert_equal ["is too long (maximum is 5 characters)"], t.errors[:content]
99 100 101

    t.title = nil
    t.content = nil
102
    assert t.invalid?
103 104
    assert_equal ["is too short (minimum is 3 characters)"], t.errors[:title]
    assert_equal ["is too short (minimum is 3 characters)"], t.errors[:content]
105 106

    t.title = "abe"
107
    t.content = "mad"
108 109 110
    assert t.valid?
  end

111
  def test_validates_length_of_using_within_with_exclusive_range
112
    Topic.validates_length_of(:title, within: 4...10)
113 114 115 116 117

    t = Topic.new("title" => "9 chars!!")
    assert t.valid?

    t.title = "Now I'm 10"
118
    assert t.invalid?
119 120 121 122 123 124
    assert_equal ["is too long (maximum is 9 characters)"], t.errors[:title]

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

125
  def test_optionally_validates_length_of_using_within
126
    Topic.validates_length_of :title, :content, within: 3..5, allow_nil: true
127

128
    t = Topic.new("title" => "abc", "content" => "abcd")
129 130 131 132 133 134 135
    assert t.valid?

    t.title = nil
    assert t.valid?
  end

  def test_validates_length_of_using_is
136
    Topic.validates_length_of :title, is: 5
137

138
    t = Topic.new("title" => "valid", "content" => "whatever")
139 140 141
    assert t.valid?

    t.title = "notvalid"
142
    assert t.invalid?
143 144
    assert t.errors[:title].any?
    assert_equal ["is the wrong length (should be 5 characters)"], t.errors[:title]
145 146

    t.title = ""
147
    assert t.invalid?
148 149

    t.title = nil
150
    assert t.invalid?
151 152 153
  end

  def test_optionally_validates_length_of_using_is
154
    Topic.validates_length_of :title, is: 5, allow_nil: true
155

156
    t = Topic.new("title" => "valid", "content" => "whatever")
157 158 159 160 161 162 163
    assert t.valid?

    t.title = nil
    assert t.valid?
  end

  def test_validates_length_of_using_bignum
164 165
    bigmin = 2**30
    bigmax = 2**32
166 167
    bigrange = bigmin...bigmax
    assert_nothing_raised do
168 169 170 171 172
      Topic.validates_length_of :title, is: bigmin + 5
      Topic.validates_length_of :title, within: bigrange
      Topic.validates_length_of :title, in: bigrange
      Topic.validates_length_of :title, minimum: bigmin
      Topic.validates_length_of :title, maximum: bigmax
173 174 175 176
    end
  end

  def test_validates_length_of_nasty_params
177 178 179 180 181 182
    assert_raise(ArgumentError) { Topic.validates_length_of(:title, is: -6) }
    assert_raise(ArgumentError) { Topic.validates_length_of(:title, within: 6) }
    assert_raise(ArgumentError) { Topic.validates_length_of(:title, minimum: "a") }
    assert_raise(ArgumentError) { Topic.validates_length_of(:title, maximum: "a") }
    assert_raise(ArgumentError) { Topic.validates_length_of(:title, within: "a") }
    assert_raise(ArgumentError) { Topic.validates_length_of(:title, is: "a") }
183 184 185
  end

  def test_validates_length_of_custom_errors_for_minimum_with_message
186
    Topic.validates_length_of(:title, minimum: 5, message: "boo %{count}")
187 188
    t = Topic.new("title" => "uhoh", "content" => "whatever")
    assert t.invalid?
189 190
    assert t.errors[:title].any?
    assert_equal ["boo 5"], t.errors[:title]
191 192 193
  end

  def test_validates_length_of_custom_errors_for_minimum_with_too_short
194
    Topic.validates_length_of(:title, minimum: 5, too_short: "hoo %{count}")
195 196
    t = Topic.new("title" => "uhoh", "content" => "whatever")
    assert t.invalid?
197 198
    assert t.errors[:title].any?
    assert_equal ["hoo 5"], t.errors[:title]
199 200 201
  end

  def test_validates_length_of_custom_errors_for_maximum_with_message
202
    Topic.validates_length_of(:title, maximum: 5, message: "boo %{count}")
203 204
    t = Topic.new("title" => "uhohuhoh", "content" => "whatever")
    assert t.invalid?
205
    assert t.errors[:title].any?
206 207 208 209
    assert_equal ["boo 5"], t.errors[:title]
  end

  def test_validates_length_of_custom_errors_for_in
210
    Topic.validates_length_of(:title, in: 10..20, message: "hoo %{count}")
211 212
    t = Topic.new("title" => "uhohuhoh", "content" => "whatever")
    assert t.invalid?
213 214 215
    assert t.errors[:title].any?
    assert_equal ["hoo 10"], t.errors["title"]

216 217
    t = Topic.new("title" => "uhohuhohuhohuhohuhohuhohuhohuhoh", "content" => "whatever")
    assert t.invalid?
218 219 220 221 222
    assert t.errors[:title].any?
    assert_equal ["hoo 20"], t.errors["title"]
  end

  def test_validates_length_of_custom_errors_for_maximum_with_too_long
223
    Topic.validates_length_of(:title, maximum: 5, too_long: "hoo %{count}")
224 225
    t = Topic.new("title" => "uhohuhoh", "content" => "whatever")
    assert t.invalid?
226 227 228 229
    assert t.errors[:title].any?
    assert_equal ["hoo 5"], t.errors["title"]
  end

230
  def test_validates_length_of_custom_errors_for_both_too_short_and_too_long
231
    Topic.validates_length_of :title, minimum: 3, maximum: 5, too_short: "too short", too_long: "too long"
232

233
    t = Topic.new(title: "a")
234 235
    assert t.invalid?
    assert t.errors[:title].any?
236
    assert_equal ["too short"], t.errors["title"]
237

238
    t = Topic.new(title: "aaaaaa")
239 240
    assert t.invalid?
    assert t.errors[:title].any?
241
    assert_equal ["too long"], t.errors["title"]
242 243
  end

244
  def test_validates_length_of_custom_errors_for_is_with_message
245
    Topic.validates_length_of(:title, is: 5, message: "boo %{count}")
246 247
    t = Topic.new("title" => "uhohuhoh", "content" => "whatever")
    assert t.invalid?
248 249 250 251 252
    assert t.errors[:title].any?
    assert_equal ["boo 5"], t.errors["title"]
  end

  def test_validates_length_of_custom_errors_for_is_with_wrong_length
253
    Topic.validates_length_of(:title, is: 5, wrong_length: "hoo %{count}")
254 255
    t = Topic.new("title" => "uhohuhoh", "content" => "whatever")
    assert t.invalid?
256 257 258 259 260
    assert t.errors[:title].any?
    assert_equal ["hoo 5"], t.errors["title"]
  end

  def test_validates_length_of_using_minimum_utf8
261
    Topic.validates_length_of :title, minimum: 5
262

J
José Valim 已提交
263 264
    t = Topic.new("title" => "一二三四五", "content" => "whatever")
    assert t.valid?
265

J
José Valim 已提交
266 267 268 269
    t.title = "一二三四"
    assert t.invalid?
    assert t.errors[:title].any?
    assert_equal ["is too short (minimum is 5 characters)"], t.errors["title"]
270 271 272
  end

  def test_validates_length_of_using_maximum_utf8
273
    Topic.validates_length_of :title, maximum: 5
274

J
José Valim 已提交
275 276
    t = Topic.new("title" => "一二三四五", "content" => "whatever")
    assert t.valid?
277

J
José Valim 已提交
278 279 280 281
    t.title = "一二34五六"
    assert t.invalid?
    assert t.errors[:title].any?
    assert_equal ["is too long (maximum is 5 characters)"], t.errors["title"]
282 283 284
  end

  def test_validates_length_of_using_within_utf8
285
    Topic.validates_length_of(:title, :content, within: 3..5)
J
José Valim 已提交
286 287 288 289 290 291

    t = Topic.new("title" => "一二", "content" => "12三四五六七")
    assert t.invalid?
    assert_equal ["is too short (minimum is 3 characters)"], t.errors[:title]
    assert_equal ["is too long (maximum is 5 characters)"], t.errors[:content]
    t.title = "一二三"
292
    t.content = "12三"
J
José Valim 已提交
293
    assert t.valid?
294 295 296
  end

  def test_optionally_validates_length_of_using_within_utf8
297
    Topic.validates_length_of :title, within: 3..5, allow_nil: true
298

299
    t = Topic.new(title: "一二三四五")
J
José Valim 已提交
300
    assert t.valid?, t.errors.inspect
301

302
    t = Topic.new(title: "一二三")
J
José Valim 已提交
303
    assert t.valid?, t.errors.inspect
304

J
José Valim 已提交
305 306
    t.title = nil
    assert t.valid?, t.errors.inspect
307 308 309
  end

  def test_validates_length_of_using_is_utf8
310
    Topic.validates_length_of :title, is: 5
311

J
José Valim 已提交
312 313
    t = Topic.new("title" => "一二345", "content" => "whatever")
    assert t.valid?
314

J
José Valim 已提交
315 316 317 318
    t.title = "一二345六"
    assert t.invalid?
    assert t.errors[:title].any?
    assert_equal ["is the wrong length (should be 5 characters)"], t.errors["title"]
319 320
  end

321
  def test_validates_length_of_for_integer
322
    Topic.validates_length_of(:approved, is: 4)
S
Santiago Pastorino 已提交
323

324
    t = Topic.new("title" => "uhohuhoh", "content" => "whatever", approved: 1)
A
Andriy Tyurnikov 已提交
325 326
    assert t.invalid?
    assert t.errors[:approved].any?
S
Santiago Pastorino 已提交
327

328
    t = Topic.new("title" => "uhohuhoh", "content" => "whatever", approved: 1234)
A
Andriy Tyurnikov 已提交
329 330
    assert t.valid?
  end
331

332
  def test_validates_length_of_for_ruby_class
333
    Person.validates_length_of :karma, minimum: 5
334

335 336 337
    p = Person.new
    p.karma = "Pix"
    assert p.invalid?
338

339
    assert_equal ["is too short (minimum is 5 characters)"], p.errors[:karma]
340

341 342 343
    p.karma = "The Smiths"
    assert p.valid?
  ensure
344
    Person.clear_validators!
345
  end
N
Niels Ganser 已提交
346 347

  def test_validates_length_of_for_infinite_maxima
348
    Topic.validates_length_of(:title, within: 5..Float::INFINITY)
N
Niels Ganser 已提交
349 350 351 352 353 354 355 356

    t = Topic.new("title" => "1234")
    assert t.invalid?
    assert t.errors[:title].any?

    t.title = "12345"
    assert t.valid?

357
    Topic.validates_length_of(:author_name, maximum: Float::INFINITY)
N
Niels Ganser 已提交
358 359 360 361 362 363

    assert t.valid?

    t.author_name = "A very long author name that should still be valid." * 100
    assert t.valid?
  end
364 365

  def test_validates_length_of_using_maximum_should_not_allow_nil_when_nil_not_allowed
366
    Topic.validates_length_of :title, maximum: 10, allow_nil: false
367 368 369 370 371
    t = Topic.new
    assert t.invalid?
  end

  def test_validates_length_of_using_maximum_should_not_allow_nil_and_empty_string_when_blank_not_allowed
372
    Topic.validates_length_of :title, maximum: 10, allow_blank: false
373 374 375 376 377 378 379 380
    t = Topic.new
    assert t.invalid?

    t.title = ""
    assert t.invalid?
  end

  def test_validates_length_of_using_both_minimum_and_maximum_should_not_allow_nil
381
    Topic.validates_length_of :title, minimum: 5, maximum: 10
382 383 384 385 386
    t = Topic.new
    assert t.invalid?
  end

  def test_validates_length_of_using_minimum_0_should_not_allow_nil
387
    Topic.validates_length_of :title, minimum: 0
388 389 390 391 392 393 394 395
    t = Topic.new
    assert t.invalid?

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

  def test_validates_length_of_using_is_0_should_not_allow_nil
396
    Topic.validates_length_of :title, is: 0
397 398 399 400 401 402
    t = Topic.new
    assert t.invalid?

    t.title = ""
    assert t.valid?
  end
D
David 已提交
403 404

  def test_validates_with_diff_in_option
405
    Topic.validates_length_of(:title, is: 5)
406
    Topic.validates_length_of(:title, is: 5, if: Proc.new { false })
D
David 已提交
407 408 409 410

    assert Topic.new("title" => "david").valid?
    assert Topic.new("title" => "david2").invalid?
  end
411
end