forked from rubocop/rubocop
/
validation_spec.rb
149 lines (116 loc) · 3.48 KB
/
validation_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
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
RSpec.describe RuboCop::Cop::Rails::Validation do
subject(:cop) { described_class.new }
it 'accepts new style validations' do
expect_no_offenses('validates :name')
end
described_class::DENYLIST.each_with_index do |validation, number|
it "registers an offense for #{validation}" do
inspect_source("#{validation} :name")
expect(cop.offenses.size).to eq(1)
end
it "outputs the correct message for #{validation}" do
inspect_source("#{validation} :name")
expect(cop.offenses.first.message)
.to include(described_class::ALLOWLIST[number])
end
end
describe '#autocorrect' do
shared_examples 'auto-corrects' do
it 'auto-corrects' do
expect(autocorrect_source(source)).to eq(auto_corrected_source)
end
end
shared_examples 'does not auto-correct' do
it 'does not auto-correct' do
expect(autocorrect_source(source)).to eq(source)
end
end
described_class::TYPES.each do |type|
context "with validates_#{type}_of" do
let(:auto_corrected_source) do
"validates :full_name, :birth_date, #{type}: true"
end
let(:source) do
"validates_#{type}_of :full_name, :birth_date"
end
include_examples 'auto-corrects'
end
end
context 'with single attribute name' do
let(:auto_corrected_source) do
'validates :a, numericality: true'
end
let(:source) do
'validates_numericality_of :a'
end
include_examples 'auto-corrects'
end
context 'with multi attribute names' do
let(:auto_corrected_source) do
'validates :a, :b, numericality: true'
end
let(:source) do
'validates_numericality_of :a, :b'
end
include_examples 'auto-corrects'
end
context 'with non-braced hash literal' do
let(:auto_corrected_source) do
'validates :a, :b, numericality: { minimum: 1 }'
end
let(:source) do
'validates_numericality_of :a, :b, minimum: 1'
end
include_examples 'auto-corrects'
end
context 'with braced hash literal' do
let(:auto_corrected_source) do
'validates :a, :b, numericality: { minimum: 1 }'
end
let(:source) do
'validates_numericality_of :a, :b, { minimum: 1 }'
end
include_examples 'auto-corrects'
end
context 'with splat' do
let(:auto_corrected_source) do
'validates :a, *b, numericality: true'
end
let(:source) do
'validates_numericality_of :a, *b'
end
include_examples 'auto-corrects'
end
context 'with splat and options' do
let(:auto_corrected_source) do
'validates :a, *b, :c, numericality: { minimum: 1 }'
end
let(:source) do
'validates_numericality_of :a, *b, :c, minimum: 1'
end
include_examples 'auto-corrects'
end
context 'with trailing send node' do
let(:source) do
'validates_numericality_of :a, b'
end
include_examples 'does not auto-correct'
end
context 'with trailing constant' do
let(:source) do
'validates_numericality_of :a, B'
end
include_examples 'does not auto-correct'
end
context 'with trailing local variable' do
let(:source) do
<<-RUBY.strip_indent
b = { minimum: 1 }
validates_numericality_of :a, b
RUBY
end
include_examples 'does not auto-correct'
end
end
end