length_validation_test.rb 12.3 KB
Newer Older
1 2
# frozen_string_literal: true

3
require "cases/helper"
4

5 6
require "models/topic"
require "models/person"
7 8

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

  def test_validates_length_of_with_allow_nil
14
    Topic.validates_length_of(:title, is: 5, allow_nil: true)
15

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

  def test_validates_length_of_with_allow_blank
23
    Topic.validates_length_of(:title, is: 5, allow_blank: true)
24

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

  def test_validates_length_of_using_minimum
32
    Topic.validates_length_of :title, minimum: 5
33

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

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

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

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

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

59
  def test_optionally_validates_length_of_using_minimum
60
    Topic.validates_length_of :title, minimum: 5, allow_nil: true
61

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

    t.title = nil
    assert t.valid?
  end

  def test_validates_length_of_using_maximum
70
    Topic.validates_length_of :title, maximum: 5
71

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

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

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

  def test_optionally_validates_length_of_using_maximum
85
    Topic.validates_length_of :title, maximum: 5, allow_nil: true
86

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

    t.title = nil
    assert t.valid?
  end

  def test_validates_length_of_using_within
95
    Topic.validates_length_of(:title, :content, within: 3..5)
96 97

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

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

    t.title = "abe"
109
    t.content = "mad"
110 111 112
    assert t.valid?
  end

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

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

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

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

127
  def test_optionally_validates_length_of_using_within
128
    Topic.validates_length_of :title, :content, within: 3..5, allow_nil: true
129

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

    t.title = nil
    assert t.valid?
  end

  def test_validates_length_of_using_is
138
    Topic.validates_length_of :title, is: 5
139

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

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

    t.title = ""
149
    assert t.invalid?
150 151

    t.title = nil
152
    assert t.invalid?
153 154 155
  end

  def test_optionally_validates_length_of_using_is
156
    Topic.validates_length_of :title, is: 5, allow_nil: true
157

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

    t.title = nil
    assert t.valid?
  end

  def test_validates_length_of_using_bignum
166 167
    bigmin = 2**30
    bigmax = 2**32
168 169
    bigrange = bigmin...bigmax
    assert_nothing_raised do
170 171 172 173 174
      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
175 176 177 178
    end
  end

  def test_validates_length_of_nasty_params
179 180 181 182 183 184
    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") }
185 186 187
  end

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

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

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

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

218 219
    t = Topic.new("title" => "uhohuhohuhohuhohuhohuhohuhohuhoh", "content" => "whatever")
    assert t.invalid?
220 221 222 223 224
    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
225
    Topic.validates_length_of(:title, maximum: 5, too_long: "hoo %{count}")
226 227
    t = Topic.new("title" => "uhohuhoh", "content" => "whatever")
    assert t.invalid?
228 229 230 231
    assert t.errors[:title].any?
    assert_equal ["hoo 5"], t.errors["title"]
  end

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

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

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

246
  def test_validates_length_of_custom_errors_for_is_with_message
247
    Topic.validates_length_of(:title, is: 5, message: "boo %{count}")
248 249
    t = Topic.new("title" => "uhohuhoh", "content" => "whatever")
    assert t.invalid?
250 251 252 253 254
    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
255
    Topic.validates_length_of(:title, is: 5, wrong_length: "hoo %{count}")
256 257
    t = Topic.new("title" => "uhohuhoh", "content" => "whatever")
    assert t.invalid?
258 259 260 261 262
    assert t.errors[:title].any?
    assert_equal ["hoo 5"], t.errors["title"]
  end

  def test_validates_length_of_using_minimum_utf8
263
    Topic.validates_length_of :title, minimum: 5
264

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

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

  def test_validates_length_of_using_maximum_utf8
275
    Topic.validates_length_of :title, maximum: 5
276

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

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

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

    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 = "一二三"
294
    t.content = "12三"
J
José Valim 已提交
295
    assert t.valid?
296 297 298
  end

  def test_optionally_validates_length_of_using_within_utf8
299
    Topic.validates_length_of :title, within: 3..5, allow_nil: true
300

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

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

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

  def test_validates_length_of_using_is_utf8
312
    Topic.validates_length_of :title, is: 5
313

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

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

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

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

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

334
  def test_validates_length_of_for_ruby_class
335
    Person.validates_length_of :karma, minimum: 5
336

337 338 339
    p = Person.new
    p.karma = "Pix"
    assert p.invalid?
340

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

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

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

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

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

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

    assert t.valid?

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

  def test_validates_length_of_using_maximum_should_not_allow_nil_when_nil_not_allowed
368
    Topic.validates_length_of :title, maximum: 10, allow_nil: false
369 370 371 372 373
    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
374
    Topic.validates_length_of :title, maximum: 10, allow_blank: false
375 376 377 378 379 380 381 382
    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
383
    Topic.validates_length_of :title, minimum: 5, maximum: 10
384 385 386 387 388
    t = Topic.new
    assert t.invalid?
  end

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

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

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

    t.title = ""
    assert t.valid?
  end
D
David 已提交
405 406

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

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