forked from rubocop/rubocop
/
memoized_instance_variable_name_spec.rb
267 lines (238 loc) · 7.6 KB
/
memoized_instance_variable_name_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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
# frozen_string_literal: true
RSpec.describe RuboCop::Cop::Naming::MemoizedInstanceVariableName, :config do
subject(:cop) { described_class.new(config) }
context 'with default EnforcedStyleForLeadingUnderscores => disallowed' do
let(:cop_config) do
{ 'EnforcedStyleForLeadingUnderscores' => 'disallowed' }
end
context 'memoized variable does not match method name' do
it 'registers an offense' do
expect_offense(<<-RUBY.strip_indent)
def x
@my_var ||= :foo
^^^^^^^ Memoized variable `@my_var` does not match method name `x`. Use `@x` instead.
end
RUBY
end
end
context 'memoized variable does not match class method name' do
it 'registers an offense' do
expect_offense(<<-RUBY.strip_indent)
def self.x
@my_var ||= :foo
^^^^^^^ Memoized variable `@my_var` does not match method name `x`. Use `@x` instead.
end
RUBY
end
end
context 'memoized variable does not match method name during assignment' do
it 'registers an offense' do
expect_offense(<<-RUBY.strip_indent)
foo = def x
@y ||= :foo
^^ Memoized variable `@y` does not match method name `x`. Use `@x` instead.
end
RUBY
end
end
context 'memoized variable does not match method name for block' do
it 'registers an offense' do
expect_offense(<<-RUBY.strip_indent)
def x
@y ||= begin
^^ Memoized variable `@y` does not match method name `x`. Use `@x` instead.
:foo
end
end
RUBY
end
end
context 'memoized variable after other code does not match method name' do
it 'registers an offense' do
expect_offense(<<-RUBY.strip_indent)
def foo
helper_variable = something_we_need_to_calculate_foo
@bar ||= calculate_expensive_thing(helper_variable)
^^^^ Memoized variable `@bar` does not match method name `foo`. Use `@foo` instead.
end
RUBY
end
it 'registers an offense for a predicate method' do
expect_offense(<<-RUBY.strip_indent)
def foo?
helper_variable = something_we_need_to_calculate_foo
@bar ||= calculate_expensive_thing(helper_variable)
^^^^ Memoized variable `@bar` does not match method name `foo?`. Use `@foo` instead.
end
RUBY
end
it 'registers an offense for a bang method' do
expect_offense(<<-RUBY.strip_indent)
def foo!
helper_variable = something_we_need_to_calculate_foo
@bar ||= calculate_expensive_thing(helper_variable)
^^^^ Memoized variable `@bar` does not match method name `foo!`. Use `@foo` instead.
end
RUBY
end
end
context 'memoized variable matches method name' do
it 'does not register an offense' do
expect_no_offenses(<<-RUBY.strip_indent)
def x
@x ||= :foo
end
RUBY
end
it 'does not registers an offense when method has leading `_`' do
expect_no_offenses(<<-RUBY.strip_indent)
def _foo
@foo ||= :foo
end
RUBY
end
it 'does not register an offense with a leading `_` for both names' do
expect_no_offenses(<<-RUBY.strip_indent)
def _foo
@_foo ||= :foo
end
RUBY
end
context 'memoized variable matches method name during assignment' do
it 'does not register an offense' do
expect_no_offenses(<<-RUBY.strip_indent)
foo = def y
@y ||= :foo
end
RUBY
end
end
context 'memoized variable matches method name for block' do
it 'does not register an offense' do
expect_no_offenses(<<-RUBY.strip_indent)
def z
@z ||= begin
:foo
end
end
RUBY
end
end
context 'non-memoized variable does not match method name' do
it 'does not register an offense' do
expect_no_offenses(<<-RUBY.strip_indent)
def a
x ||= :foo
end
RUBY
end
end
context 'memoized variable matches predicate method name' do
it 'does not register an offense' do
expect_no_offenses(<<-RUBY.strip_indent)
def a?
@a ||= :foo
end
RUBY
end
end
context 'memoized variable matches bang method name' do
it 'does not register an offense' do
expect_no_offenses(<<-RUBY.strip_indent)
def a!
@a ||= :foo
end
RUBY
end
end
context 'code follows memoized variable assignment' do
it 'does not register an offense' do
expect_no_offenses(<<-RUBY.strip_indent)
def a
@b ||= :foo
call_something_else
end
RUBY
end
context 'memoized variable after other code' do
it 'does not register an offense' do
expect_no_offenses(<<-RUBY.strip_indent)
def foo
helper_variable = something_we_need_to_calculate_foo
@foo ||= calculate_expensive_thing(helper_variable)
end
RUBY
end
end
context 'instance variables in initialize methods' do
it 'does not register an offense' do
expect_no_offenses(<<-RUBY.strip_indent)
def initialize
@files_with_offenses ||= {}
end
RUBY
end
end
end
end
end
context 'EnforcedStyleForLeadingUnderscores: required' do
let(:cop_config) { { 'EnforcedStyleForLeadingUnderscores' => 'required' } }
it 'registers an offense when names match but missing a leading _' do
expect_offense(<<-RUBY.strip_indent)
def foo
@foo ||= :foo
^^^^ Memoized variable `@foo` does not start with `_`. Use `@_foo` instead.
end
RUBY
end
it 'registers an offense when it has leading `_` but names do not match' do
expect_offense(<<-RUBY.strip_indent)
def foo
@_my_var ||= :foo
^^^^^^^^ Memoized variable `@_my_var` does not match method name `foo`. Use `@_foo` instead.
end
RUBY
end
it 'does not register an offense with a leading `_` for both names' do
expect_no_offenses(<<-RUBY.strip_indent)
def _foo
@_foo ||= :foo
end
RUBY
end
end
context 'EnforcedStyleForLeadingUnderscores: optional' do
let(:cop_config) { { 'EnforcedStyleForLeadingUnderscores' => 'optional' } }
context 'memoized variable matches method name' do
it 'does not register an offense with a leading underscore' do
expect_no_offenses(<<-RUBY.strip_indent)
def x
@_x ||= :foo
end
RUBY
end
it 'does not register an offense without a leading underscore' do
expect_no_offenses(<<-RUBY.strip_indent)
def x
@x ||= :foo
end
RUBY
end
it 'does not register an offense with a leading `_` for both names' do
expect_no_offenses(<<-RUBY.strip_indent)
def _x
@_x ||= :foo
end
RUBY
end
it 'does not register an offense with a leading `_` for method name' do
expect_no_offenses(<<-RUBY.strip_indent)
def _x
@x ||= :foo
end
RUBY
end
end
end
end