|
1 | 1 | # frozen_string_literal: true
|
2 | 2 |
|
3 | 3 | RSpec.describe RuboCop::Cop::Rails::Validation, :config do
|
4 |
| - it 'accepts new style validations' do |
5 |
| - expect_no_offenses('validates :name') |
6 |
| - end |
7 |
| - |
8 |
| - described_class::RESTRICT_ON_SEND.each_with_index do |validation, number| |
9 |
| - it "registers an offense for #{validation}" do |
10 |
| - offenses = inspect_source("#{validation} :name") |
11 |
| - expect(offenses.first.message.include?(described_class::ALLOWLIST[number])).to be(true) |
| 4 | + described_class::TYPES.each do |type| |
| 5 | + it "registers an offense for with validates_#{type}_of" do |
| 6 | + type = 'length' if type == 'size' |
| 7 | + expect_offense(<<~RUBY, type: type) |
| 8 | + validates_#{type}_of :full_name, :birth_date |
| 9 | + ^^^^^^^^^^^{type}^^^ Prefer the new style validations `validates :column, #{type}: value` over `validates_#{type}_of`. |
| 10 | + RUBY |
| 11 | + |
| 12 | + expect_correction(<<~RUBY) |
| 13 | + validates :full_name, :birth_date, #{type}: true |
| 14 | + RUBY |
12 | 15 | end
|
13 |
| - end |
14 | 16 |
|
15 |
| - describe '#autocorrect' do |
16 |
| - shared_examples 'autocorrects' do |
17 |
| - it 'autocorrects' do |
18 |
| - expect(autocorrect_source(source)).to eq(autocorrected_source) |
19 |
| - end |
20 |
| - end |
| 17 | + it "registers an offense for with validates_#{type}_of when method arguments are enclosed in parentheses" do |
| 18 | + type = 'length' if type == 'size' |
| 19 | + expect_offense(<<~RUBY, type: type) |
| 20 | + validates_#{type}_of(:full_name, :birth_date) |
| 21 | + ^^^^^^^^^^^{type}^^^ Prefer the new style validations `validates :column, #{type}: value` over `validates_#{type}_of`. |
| 22 | + RUBY |
21 | 23 |
|
22 |
| - shared_examples 'does not autocorrect' do |
23 |
| - it 'does not autocorrect' do |
24 |
| - expect(autocorrect_source(source)).to eq(source) |
25 |
| - end |
| 24 | + expect_correction(<<~RUBY) |
| 25 | + validates(:full_name, :birth_date, #{type}: true) |
| 26 | + RUBY |
26 | 27 | end
|
27 | 28 |
|
28 |
| - described_class::TYPES.each do |type| |
29 |
| - context "with validates_#{type}_of" do |
30 |
| - let(:autocorrected_source) do |
31 |
| - type = 'length' if type == 'size' |
32 |
| - |
33 |
| - "validates :full_name, :birth_date, #{type}: true" |
34 |
| - end |
35 |
| - |
36 |
| - let(:source) do |
37 |
| - "validates_#{type}_of :full_name, :birth_date" |
38 |
| - end |
39 |
| - |
40 |
| - include_examples 'autocorrects' |
41 |
| - end |
42 |
| - |
43 |
| - context "with validates_#{type}_of when method arguments are enclosed in parentheses" do |
44 |
| - let(:autocorrected_source) do |
45 |
| - type = 'length' if type == 'size' |
46 |
| - |
47 |
| - "validates(:full_name, :birth_date, #{type}: true)" |
48 |
| - end |
49 |
| - |
50 |
| - let(:source) do |
51 |
| - "validates_#{type}_of(:full_name, :birth_date)" |
52 |
| - end |
53 |
| - |
54 |
| - include_examples 'autocorrects' |
55 |
| - end |
56 |
| - |
57 |
| - context "with validates_#{type}_of when attributes are specified with array literal" do |
58 |
| - let(:autocorrected_source) do |
59 |
| - type = 'length' if type == 'size' |
60 |
| - |
61 |
| - "validates :full_name, :birth_date, #{type}: true" |
62 |
| - end |
63 |
| - |
64 |
| - let(:source) do |
65 |
| - "validates_#{type}_of [:full_name, :birth_date]" |
66 |
| - end |
67 |
| - |
68 |
| - include_examples 'autocorrects' |
69 |
| - end |
70 |
| - |
71 |
| - context "with validates_#{type}_of when attributes are specified with frozen array literal" do |
72 |
| - let(:autocorrected_source) do |
73 |
| - type = 'length' if type == 'size' |
74 |
| - |
75 |
| - "validates :full_name, :birth_date, #{type}: true" |
76 |
| - end |
77 |
| - |
78 |
| - let(:source) do |
79 |
| - "validates_#{type}_of [:full_name, :birth_date].freeze" |
80 |
| - end |
81 |
| - |
82 |
| - include_examples 'autocorrects' |
83 |
| - end |
84 |
| - |
85 |
| - context "with validates_#{type}_of when attributes are specified with symbol array literal" do |
86 |
| - let(:autocorrected_source) do |
87 |
| - type = 'length' if type == 'size' |
88 |
| - |
89 |
| - "validates :full_name, :birth_date, #{type}: true" |
90 |
| - end |
91 |
| - |
92 |
| - let(:source) do |
93 |
| - "validates_#{type}_of %i[full_name birth_date]" |
94 |
| - end |
95 |
| - |
96 |
| - include_examples 'autocorrects' |
97 |
| - end |
98 |
| - |
99 |
| - context "with validates_#{type}_of when attributes are specified with frozen symbol array literal" do |
100 |
| - let(:autocorrected_source) do |
101 |
| - type = 'length' if type == 'size' |
| 29 | + it "registers an offense for with validates_#{type}_of when attributes are specified with array literal" do |
| 30 | + type = 'length' if type == 'size' |
| 31 | + expect_offense(<<~RUBY, type: type) |
| 32 | + validates_#{type}_of [:full_name, :birth_date] |
| 33 | + ^^^^^^^^^^^{type}^^^ Prefer the new style validations `validates :column, #{type}: value` over `validates_#{type}_of`. |
| 34 | + RUBY |
102 | 35 |
|
103 |
| - "validates :full_name, :birth_date, #{type}: true" |
104 |
| - end |
105 |
| - |
106 |
| - let(:source) do |
107 |
| - "validates_#{type}_of %i[full_name birth_date].freeze" |
108 |
| - end |
109 |
| - |
110 |
| - include_examples 'autocorrects' |
111 |
| - end |
| 36 | + expect_correction(<<~RUBY) |
| 37 | + validates :full_name, :birth_date, #{type}: true |
| 38 | + RUBY |
112 | 39 | end
|
113 | 40 |
|
114 |
| - context 'with single attribute name' do |
115 |
| - let(:autocorrected_source) do |
116 |
| - 'validates :a, numericality: true' |
117 |
| - end |
118 |
| - |
119 |
| - let(:source) do |
120 |
| - 'validates_numericality_of :a' |
121 |
| - end |
| 41 | + it "registers an offense for with validates_#{type}_of when attributes are specified with frozen array literal" do |
| 42 | + type = 'length' if type == 'size' |
| 43 | + expect_offense(<<~RUBY, type: type) |
| 44 | + validates_#{type}_of [:full_name, :birth_date].freeze |
| 45 | + ^^^^^^^^^^^{type}^^^ Prefer the new style validations `validates :column, #{type}: value` over `validates_#{type}_of`. |
| 46 | + RUBY |
122 | 47 |
|
123 |
| - include_examples 'autocorrects' |
| 48 | + expect_correction(<<~RUBY) |
| 49 | + validates :full_name, :birth_date, #{type}: true |
| 50 | + RUBY |
124 | 51 | end
|
125 | 52 |
|
126 |
| - context 'with multi attribute names' do |
127 |
| - let(:autocorrected_source) do |
128 |
| - 'validates :a, :b, numericality: true' |
129 |
| - end |
| 53 | + it "registers an offense for with validates_#{type}_of when attributes are specified with symbol array literal" do |
| 54 | + type = 'length' if type == 'size' |
| 55 | + expect_offense(<<~RUBY, type: type) |
| 56 | + validates_#{type}_of %i[full_name birth_date] |
| 57 | + ^^^^^^^^^^^{type}^^^ Prefer the new style validations `validates :column, #{type}: value` over `validates_#{type}_of`. |
| 58 | + RUBY |
130 | 59 |
|
131 |
| - let(:source) do |
132 |
| - 'validates_numericality_of :a, :b' |
133 |
| - end |
134 |
| - |
135 |
| - include_examples 'autocorrects' |
| 60 | + expect_correction(<<~RUBY) |
| 61 | + validates :full_name, :birth_date, #{type}: true |
| 62 | + RUBY |
136 | 63 | end
|
137 | 64 |
|
138 |
| - context 'with non-braced hash literal' do |
139 |
| - let(:autocorrected_source) do |
140 |
| - 'validates :a, :b, numericality: { minimum: 1 }' |
141 |
| - end |
142 |
| - |
143 |
| - let(:source) do |
144 |
| - 'validates_numericality_of :a, :b, minimum: 1' |
145 |
| - end |
146 |
| - |
147 |
| - include_examples 'autocorrects' |
| 65 | + it "registers an offense for with validates_#{type}_of when " \ |
| 66 | + 'attributes are specified with frozen symbol array literal' do |
| 67 | + type = 'length' if type == 'size' |
| 68 | + expect_offense(<<~RUBY, type: type) |
| 69 | + validates_#{type}_of %i[full_name birth_date].freeze |
| 70 | + ^^^^^^^^^^^{type}^^^ Prefer the new style validations `validates :column, #{type}: value` over `validates_#{type}_of`. |
| 71 | + RUBY |
| 72 | + |
| 73 | + expect_correction(<<~RUBY) |
| 74 | + validates :full_name, :birth_date, #{type}: true |
| 75 | + RUBY |
148 | 76 | end
|
| 77 | + end |
149 | 78 |
|
150 |
| - context 'with braced hash literal' do |
151 |
| - let(:autocorrected_source) do |
152 |
| - 'validates :a, :b, numericality: { minimum: 1 }' |
153 |
| - end |
| 79 | + it 'registers an offense with single attribute name' do |
| 80 | + expect_offense(<<~RUBY) |
| 81 | + validates_numericality_of :a |
| 82 | + ^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer the new style [...] |
| 83 | + RUBY |
154 | 84 |
|
155 |
| - let(:source) do |
156 |
| - 'validates_numericality_of :a, :b, { minimum: 1 }' |
157 |
| - end |
| 85 | + expect_correction(<<~RUBY) |
| 86 | + validates :a, numericality: true |
| 87 | + RUBY |
| 88 | + end |
158 | 89 |
|
159 |
| - include_examples 'autocorrects' |
160 |
| - end |
| 90 | + it 'registers an offense with multi attribute names' do |
| 91 | + expect_offense(<<~RUBY) |
| 92 | + validates_numericality_of :a, :b |
| 93 | + ^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer the new style [...] |
| 94 | + RUBY |
161 | 95 |
|
162 |
| - context 'with a proc' do |
163 |
| - let(:autocorrected_source) do |
164 |
| - 'validates :a, :b, comparison: { greater_than: -> { Time.zone.today } }' |
165 |
| - end |
| 96 | + expect_correction(<<~RUBY) |
| 97 | + validates :a, :b, numericality: true |
| 98 | + RUBY |
| 99 | + end |
166 | 100 |
|
167 |
| - let(:source) do |
168 |
| - 'validates_comparison_of :a, :b, greater_than: -> { Time.zone.today }' |
169 |
| - end |
| 101 | + it 'registers an offense with non-braced hash literal' do |
| 102 | + expect_offense(<<~RUBY) |
| 103 | + validates_numericality_of :a, :b, minimum: 1 |
| 104 | + ^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer the new style [...] |
| 105 | + RUBY |
170 | 106 |
|
171 |
| - include_examples 'autocorrects' |
172 |
| - end |
| 107 | + expect_correction(<<~RUBY) |
| 108 | + validates :a, :b, numericality: { minimum: 1 } |
| 109 | + RUBY |
| 110 | + end |
173 | 111 |
|
174 |
| - context 'with splat' do |
175 |
| - let(:autocorrected_source) do |
176 |
| - 'validates :a, *b, numericality: true' |
177 |
| - end |
| 112 | + it 'registers an offense with braced hash literal' do |
| 113 | + expect_offense(<<~RUBY) |
| 114 | + validates_numericality_of :a, :b, { minimum: 1 } |
| 115 | + ^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer the new style [...] |
| 116 | + RUBY |
178 | 117 |
|
179 |
| - let(:source) do |
180 |
| - 'validates_numericality_of :a, *b' |
181 |
| - end |
| 118 | + expect_correction(<<~RUBY) |
| 119 | + validates :a, :b, numericality: { minimum: 1 } |
| 120 | + RUBY |
| 121 | + end |
182 | 122 |
|
183 |
| - include_examples 'autocorrects' |
184 |
| - end |
| 123 | + it 'registers an offense with a proc' do |
| 124 | + expect_offense(<<~RUBY) |
| 125 | + validates_comparison_of :a, :b, greater_than: -> { Time.zone.today } |
| 126 | + ^^^^^^^^^^^^^^^^^^^^^^^ Prefer the new style [...] |
| 127 | + RUBY |
185 | 128 |
|
186 |
| - context 'with splat and options' do |
187 |
| - let(:autocorrected_source) do |
188 |
| - 'validates :a, *b, :c, numericality: { minimum: 1 }' |
189 |
| - end |
| 129 | + expect_correction(<<~RUBY) |
| 130 | + validates :a, :b, comparison: { greater_than: -> { Time.zone.today } } |
| 131 | + RUBY |
| 132 | + end |
190 | 133 |
|
191 |
| - let(:source) do |
192 |
| - 'validates_numericality_of :a, *b, :c, minimum: 1' |
193 |
| - end |
| 134 | + it 'registers an offense with a splat' do |
| 135 | + expect_offense(<<~RUBY) |
| 136 | + validates_numericality_of :a, *b |
| 137 | + ^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer the new style [...] |
| 138 | + RUBY |
194 | 139 |
|
195 |
| - include_examples 'autocorrects' |
196 |
| - end |
| 140 | + expect_correction(<<~RUBY) |
| 141 | + validates :a, *b, numericality: true |
| 142 | + RUBY |
| 143 | + end |
197 | 144 |
|
198 |
| - context 'with trailing send node' do |
199 |
| - let(:source) do |
200 |
| - 'validates_numericality_of :a, b' |
201 |
| - end |
| 145 | + it 'registers an offense with a splat and options' do |
| 146 | + expect_offense(<<~RUBY) |
| 147 | + validates_numericality_of :a, *b, :c, minimum: 1 |
| 148 | + ^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer the new style [...] |
| 149 | + RUBY |
202 | 150 |
|
203 |
| - include_examples 'does not autocorrect' |
204 |
| - end |
| 151 | + expect_correction(<<~RUBY) |
| 152 | + validates :a, *b, :c, numericality: { minimum: 1 } |
| 153 | + RUBY |
| 154 | + end |
205 | 155 |
|
206 |
| - context 'with trailing constant' do |
207 |
| - let(:source) do |
208 |
| - 'validates_numericality_of :a, B' |
209 |
| - end |
| 156 | + it 'registers no offense with trailing send node' do |
| 157 | + expect_no_offenses(<<~RUBY) |
| 158 | + validates_numericality_of :a, b |
| 159 | + RUBY |
| 160 | + end |
210 | 161 |
|
211 |
| - include_examples 'does not autocorrect' |
212 |
| - end |
| 162 | + it 'registers no offense with trailing constant' do |
| 163 | + expect_no_offenses(<<~RUBY) |
| 164 | + validates_numericality_of :a, B |
| 165 | + RUBY |
| 166 | + end |
213 | 167 |
|
214 |
| - context 'with trailing local variable' do |
215 |
| - let(:source) do |
216 |
| - <<~RUBY |
217 |
| - b = { minimum: 1 } |
218 |
| - validates_numericality_of :a, b |
219 |
| - RUBY |
220 |
| - end |
| 168 | + it 'registers no offense with trailing local variable' do |
| 169 | + expect_no_offenses(<<~RUBY) |
| 170 | + b = { minimum: 1 } |
| 171 | + validates_numericality_of :a, b |
| 172 | + RUBY |
| 173 | + end |
221 | 174 |
|
222 |
| - include_examples 'does not autocorrect' |
223 |
| - end |
| 175 | + it 'registers no offense when no arguments are passed' do |
| 176 | + expect_no_offenses(<<~RUBY) |
| 177 | + validates_numericality_of |
| 178 | + RUBY |
224 | 179 | end
|
225 | 180 | end
|
0 commit comments