forked from rubocop/rubocop
/
safe_navigation_chain_spec.rb
194 lines (171 loc) · 6.3 KB
/
safe_navigation_chain_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
# frozen_string_literal: true
RSpec.describe RuboCop::Cop::Lint::SafeNavigationChain, :config do
subject(:cop) { described_class.new(config) }
let(:cop_config) do
{ 'AcceptedMethods' => %w[present? blank? try presence] }
end
shared_examples 'accepts' do |name, code|
it "accepts usages of #{name}" do
expect_no_offenses(code)
end
end
[
['ordinary method chain', 'x.foo.bar.baz'],
['ordinary method chain with argument', 'x.foo(x).bar(y).baz(z)'],
['method chain with safe navigation only', 'x&.foo&.bar&.baz'],
['method chain with safe navigation only with argument',
'x&.foo(x)&.bar(y)&.baz(z)'],
['safe navigation at last only', 'x.foo.bar&.baz'],
['safe navigation at last only with argument', 'x.foo(x).bar(y)&.baz(z)'],
['safe navigation with == operator', 'x&.foo == bar'],
['safe navigation with === operator', 'x&.foo === bar'],
['safe navigation with || operator', 'x&.foo || bar'],
['safe navigation with && operator', 'x&.foo && bar'],
['safe navigation with | operator', 'x&.foo | bar'],
['safe navigation with & operator', 'x&.foo & bar'],
['safe navigation with `nil?` method', 'x&.foo.nil?'],
['safe navigation with `present?` method', 'x&.foo.present?'],
['safe navigation with `blank?` method', 'x&.foo.blank?'],
['safe navigation with `try` method', 'a&.b.try(:c)'],
['safe navigation with assignment method', 'x&.foo = bar'],
['safe navigation with self assignment method', 'x&.foo += bar'],
['safe navigation with `to_d` method', 'x&.foo.to_d']
].each do |name, code|
include_examples 'accepts', name, code
end
it 'registers an offense for ordinary method call exists after ' \
'safe navigation method call' do
expect_offense(<<~RUBY)
x&.foo.bar
^^^^ Do not chain ordinary method call after safe navigation operator.
RUBY
end
it 'registers an offense for ordinary method call exists after ' \
'safe navigation method call with an argument' do
expect_offense(<<~RUBY)
x&.foo(x).bar(y)
^^^^^^^ Do not chain ordinary method call after safe navigation operator.
RUBY
end
it 'registers an offense for ordinary method chain exists after ' \
'safe navigation method call' do
expect_offense(<<~RUBY)
something
x&.foo.bar.baz
^^^^^^^^ Do not chain ordinary method call after safe navigation operator.
RUBY
end
it 'registers an offense for ordinary method chain exists after ' \
'safe navigation method call with an argument' do
expect_offense(<<~RUBY)
x&.foo(x).bar(y).baz(z)
^^^^^^^^^^^^^^ Do not chain ordinary method call after safe navigation operator.
RUBY
end
it 'registers an offense for ordinary method chain exists after ' \
'safe navigation method call with a block-pass' do
expect_offense(<<~RUBY)
something
x&.select(&:foo).bar
^^^^ Do not chain ordinary method call after safe navigation operator.
RUBY
end
it 'registers an offense for ordinary method chain exists after ' \
'safe navigation method call with a block' do
expect_offense(<<~RUBY)
something
x&.select { |x| foo(x) }.bar
^^^^ Do not chain ordinary method call after safe navigation operator.
RUBY
end
context '>= Ruby 2.7', :ruby27 do
it 'registers an offense for ordinary method chain exists after ' \
'safe navigation method call with a block using numbered parameter' do
expect_offense(<<~RUBY)
something
x&.select { foo(_1) }.bar
^^^^ Do not chain ordinary method call after safe navigation operator.
RUBY
end
end
it 'registers an offense for safe navigation with < operator' do
expect_offense(<<~RUBY)
x&.foo < bar
^^^^^^ Do not chain ordinary method call after safe navigation operator.
RUBY
end
it 'registers an offense for safe navigation with > operator' do
expect_offense(<<~RUBY)
x&.foo > bar
^^^^^^ Do not chain ordinary method call after safe navigation operator.
RUBY
end
it 'registers an offense for safe navigation with <= operator' do
expect_offense(<<~RUBY)
x&.foo <= bar
^^^^^^^ Do not chain ordinary method call after safe navigation operator.
RUBY
end
it 'registers an offense for safe navigation with >= operator' do
expect_offense(<<~RUBY)
x&.foo >= bar
^^^^^^^ Do not chain ordinary method call after safe navigation operator.
RUBY
end
it 'registers an offense for safe navigation with + operator' do
expect_offense(<<~RUBY)
x&.foo + bar
^^^^^^ Do not chain ordinary method call after safe navigation operator.
RUBY
end
it 'registers an offense for safe navigation with [] operator' do
expect_offense(<<~RUBY)
x&.foo[bar]
^^^^^ Do not chain ordinary method call after safe navigation operator.
RUBY
end
it 'registers an offense for safe navigation with []= operator' do
expect_offense(<<~RUBY)
x&.foo[bar] = baz
^^^^^^^^^^^ Do not chain ordinary method call after safe navigation operator.
RUBY
end
context 'proper highlighting' do
it 'when there are methods before' do
expect_offense(<<~RUBY)
something
x&.foo.bar.baz
^^^^^^^^ Do not chain ordinary method call after safe navigation operator.
RUBY
end
it 'when there are methods after' do
expect_offense(<<~RUBY)
x&.foo.bar.baz
^^^^^^^^ Do not chain ordinary method call after safe navigation operator.
something
RUBY
end
it 'when in a method' do
expect_offense(<<~RUBY)
def something
x&.foo.bar.baz
^^^^^^^^ Do not chain ordinary method call after safe navigation operator.
end
RUBY
end
it 'when in a begin' do
expect_offense(<<~RUBY)
begin
x&.foo.bar.baz
^^^^^^^^ Do not chain ordinary method call after safe navigation operator.
end
RUBY
end
it 'when used with a modifier if' do
expect_offense(<<~RUBY)
x&.foo.bar.baz if something
^^^^^^^^ Do not chain ordinary method call after safe navigation operator.
RUBY
end
end
end