/
redundant_allow_nil_spec.rb
84 lines (72 loc) · 2.63 KB
/
redundant_allow_nil_spec.rb
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
# frozen_string_literal: true
RSpec.describe RuboCop::Cop::Rails::RedundantAllowNil do
subject(:cop) { described_class.new }
context 'when not using both `allow_nil` and `allow_blank`' do
it 'registers no offense' do
expect_no_offenses(<<-RUBY.strip_indent)
validates :email, presence: true
RUBY
end
end
context 'when using only `allow_nil`' do
it 'registers no offense' do
expect_no_offenses(<<-RUBY.strip_indent)
validates :email, allow_nil: true
RUBY
end
end
context 'when using only `allow_blank`' do
it 'registers no offense' do
expect_no_offenses(<<-RUBY.strip_indent)
validates :email, allow_blank: true
RUBY
end
end
context 'when both allow_nil and allow_blank are true' do
it 'registers an offense' do
expect_offense(<<-RUBY.strip_indent)
validates :title, length: { is: 5 }, allow_nil: true, allow_blank: true
^^^^^^^^^^^^^^^ `allow_nil` is redundant when `allow_blank` has the same value.
RUBY
expect_correction(<<-RUBY.strip_indent)
validates :title, length: { is: 5 }, allow_blank: true
RUBY
end
end
context 'when allow_nil is false and allow_blank is true' do
it 'registers an offense' do
expect_offense(<<-RUBY.strip_indent)
validates :title, allow_nil: false, allow_blank: true, length: { is: 5 }
^^^^^^^^^^^^^^^^ `allow_nil: false` is redundant when `allow_blank` is true.
RUBY
expect_correction(<<-RUBY.strip_indent)
validates :title, allow_blank: true, length: { is: 5 }
RUBY
end
end
context 'when allow_nil is true and allow_blank is false' do
it 'registers no offense' do
expect_no_offenses(<<-RUBY.strip_indent)
validates :title, length: { is: 5 }, allow_nil: true, allow_blank: false
RUBY
end
end
context 'when both allow_nil and allow_blank are false' do
it 'registers an offense' do
expect_offense(<<-RUBY.strip_indent)
validates :title, length: { is: 5 }, allow_blank: false, allow_nil: false
^^^^^^^^^^^^^^^^ `allow_nil` is redundant when `allow_blank` has the same value.
RUBY
expect_correction(<<-RUBY.strip_indent)
validates :title, length: { is: 5 }, allow_blank: false
RUBY
end
end
context 'when using string interpolation' do
it 'registers no offense' do
expect_no_offenses(<<-'RUBY'.strip_indent)
validates :details, "path_to_dynamic_validation/#{with_interpolation}": true
RUBY
end
end
end