/
underscore_prefixed_variable_name_spec.rb
170 lines (152 loc) · 4.66 KB
/
underscore_prefixed_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
# frozen_string_literal: true
RSpec.describe RuboCop::Cop::Lint::UnderscorePrefixedVariableName, :config do
subject(:cop) { described_class.new(config) }
let(:cop_config) { { 'AllowKeywordBlockArguments' => false } }
context 'when an underscore-prefixed variable is used' do
it 'registers an offense' do
expect_offense(<<-RUBY.strip_indent)
def some_method
_foo = 1
^^^^ Do not use prefix `_` for a variable that is used.
puts _foo
end
RUBY
end
end
context 'when non-underscore-prefixed variable is used' do
it 'does not register an offense' do
expect_no_offenses(<<-RUBY.strip_indent)
def some_method
foo = 1
puts foo
end
RUBY
end
end
context 'when an underscore-prefixed variable is reassigned' do
it 'does not register an offense' do
expect_no_offenses(<<-RUBY.strip_indent)
def some_method
_foo = 1
_foo = 2
end
RUBY
end
end
context 'when an underscore-prefixed method argument is used' do
it 'registers an offense' do
expect_offense(<<-RUBY.strip_indent)
def some_method(_foo)
^^^^ Do not use prefix `_` for a variable that is used.
puts _foo
end
RUBY
end
end
context 'when an underscore-prefixed block argument is used' do
[true, false].each do |config|
let(:cop_config) { { 'AllowKeywordBlockArguments' => config } }
it 'registers an offense' do
expect_offense(<<-RUBY.strip_indent)
1.times do |_foo|
^^^^ Do not use prefix `_` for a variable that is used.
puts _foo
end
RUBY
end
end
end
context 'when an underscore-prefixed keyword block argument is used' do
it 'registers an offense' do
expect_offense(<<-RUBY.strip_indent)
define_method(:foo) do |_foo: 'default'|
^^^^ Do not use prefix `_` for a variable that is used.
puts _foo
end
RUBY
end
context 'when AllowKeywordBlockArguments is set' do
let(:cop_config) { { 'AllowKeywordBlockArguments' => true } }
it 'does not register an offense' do
expect_no_offenses(<<-RUBY.strip_indent)
define_method(:foo) do |_foo: 'default'|
puts _foo
end
RUBY
end
end
end
context 'when an underscore-prefixed variable in top-level scope is used' do
it 'registers an offense' do
expect_offense(<<-RUBY.strip_indent)
_foo = 1
^^^^ Do not use prefix `_` for a variable that is used.
puts _foo
RUBY
end
end
context 'when an underscore-prefixed variable is captured by a block' do
it 'accepts' do
expect_no_offenses(<<-RUBY.strip_indent)
_foo = 1
1.times do
_foo = 2
end
RUBY
end
end
context 'when an underscore-prefixed named capture variable is used' do
it 'registers an offense' do
expect_offense(<<-RUBY.strip_indent)
/(?<_foo>\\w+)/ =~ 'FOO'
^^^^^^^^^^^^^^ Do not use prefix `_` for a variable that is used.
puts _foo
RUBY
end
end
%w[super binding].each do |keyword|
context "in a method calling `#{keyword}` without arguments" do
context 'when an underscore-prefixed argument is not used explicitly' do
it 'does not register an offense' do
expect_no_offenses(<<-RUBY.strip_indent)
def some_method(*_)
#{keyword}
end
RUBY
end
end
context 'when an underscore-prefixed argument is used explicitly' do
it 'registers an offense' do
expect_offense(<<-RUBY.strip_indent)
def some_method(*_)
^ Do not use prefix `_` for a variable that is used.
#{keyword}
puts _
end
RUBY
end
end
end
context "in a method calling `#{keyword}` with arguments" do
context 'when an underscore-prefixed argument is not used' do
it 'does not register an offense' do
expect_no_offenses(<<-RUBY.strip_indent)
def some_method(*_)
#{keyword}(:something)
end
RUBY
end
end
context 'when an underscore-prefixed argument is used explicitly' do
it 'registers an offense' do
expect_offense(<<-RUBY.strip_indent)
def some_method(*_)
^ Do not use prefix `_` for a variable that is used.
#{keyword}(*_)
end
RUBY
end
end
end
end
end