forked from rubocop/rubocop
-
Notifications
You must be signed in to change notification settings - Fork 2
/
collection_compact_spec.rb
160 lines (138 loc) · 4.92 KB
/
collection_compact_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
# frozen_string_literal: true
RSpec.describe RuboCop::Cop::Style::CollectionCompact, :config do
it 'registers an offense and corrects when using `reject` on array to reject nils' do
expect_offense(<<~RUBY)
array.reject { |e| e.nil? }
^^^^^^^^^^^^^^^^^^^^^ Use `compact` instead of `reject { |e| e.nil? }`.
array.reject! { |e| e.nil? }
^^^^^^^^^^^^^^^^^^^^^^ Use `compact!` instead of `reject! { |e| e.nil? }`.
RUBY
expect_correction(<<~RUBY)
array.compact
array.compact!
RUBY
end
it 'registers an offense and corrects when using `reject` with block pass arg on array to reject nils' do
expect_offense(<<~RUBY)
array.reject(&:nil?)
^^^^^^^^^^^^^^ Use `compact` instead of `reject(&:nil?)`.
array.reject!(&:nil?)
^^^^^^^^^^^^^^^ Use `compact!` instead of `reject!(&:nil?)`.
RUBY
expect_correction(<<~RUBY)
array.compact
array.compact!
RUBY
end
it 'registers an offense and corrects when using `reject` with block pass arg and no parentheses' do
expect_offense(<<~RUBY)
array.reject &:nil?
^^^^^^^^^^^^^ Use `compact` instead of `reject &:nil?`.
RUBY
expect_correction(<<~RUBY)
array.compact
RUBY
end
it 'registers an offense and corrects when using `reject` on hash to reject nils' do
expect_offense(<<~RUBY)
hash.reject { |k, v| v.nil? }
^^^^^^^^^^^^^^^^^^^^^^^^ Use `compact` instead of `reject { |k, v| v.nil? }`.
hash.reject! { |k, v| v.nil? }
^^^^^^^^^^^^^^^^^^^^^^^^^ Use `compact!` instead of `reject! { |k, v| v.nil? }`.
RUBY
expect_correction(<<~RUBY)
hash.compact
hash.compact!
RUBY
end
it 'registers an offense and corrects when using `select/select!` to reject nils' do
expect_offense(<<~RUBY)
array.select { |e| !e.nil? }
^^^^^^^^^^^^^^^^^^^^^^ Use `compact` instead of `select { |e| !e.nil? }`.
hash.select! { |k, v| !v.nil? }
^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `compact!` instead of `select! { |k, v| !v.nil? }`.
RUBY
expect_correction(<<~RUBY)
array.compact
hash.compact!
RUBY
end
it 'registers an offense and corrects when using `reject` and receiver is a variable' do
expect_offense(<<~RUBY)
def foo(params)
params.reject { |_k, v| v.nil? }
^^^^^^^^^^^^^^^^^^^^^^^^^ Use `compact` instead of `reject { |_k, v| v.nil? }`.
end
RUBY
expect_correction(<<~RUBY)
def foo(params)
params.compact
end
RUBY
end
it 'does not register an offense when using `reject` to not to rejecting nils' do
expect_no_offenses(<<~RUBY)
array.reject { |e| e.odd? }
RUBY
end
it 'does not register an offense when using `compact/compact!`' do
expect_no_offenses(<<~RUBY)
array.compact
array.compact!
RUBY
end
context 'when without receiver' do
it 'does not register an offense and corrects when using `reject` on array to reject nils' do
expect_no_offenses(<<~RUBY)
reject { |e| e.nil? }
reject! { |e| e.nil? }
RUBY
end
it 'does not register an offense and corrects when using `select/select!` to reject nils' do
expect_no_offenses(<<~RUBY)
select { |e| !e.nil? }
select! { |k, v| !v.nil? }
RUBY
end
end
context 'Ruby >= 3.1', :ruby31 do
it 'registers an offense and corrects when using `to_enum.reject` on array to reject nils' do
expect_offense(<<~RUBY)
array.to_enum.reject { |e| e.nil? }
^^^^^^^^^^^^^^^^^^^^^ Use `compact` instead of `reject { |e| e.nil? }`.
array.to_enum.reject! { |e| e.nil? }
^^^^^^^^^^^^^^^^^^^^^^ Use `compact!` instead of `reject! { |e| e.nil? }`.
RUBY
expect_correction(<<~RUBY)
array.to_enum.compact
array.to_enum.compact!
RUBY
end
it 'registers an offense and corrects when using `lazy.reject` on array to reject nils' do
expect_offense(<<~RUBY)
array.lazy.reject { |e| e.nil? }
^^^^^^^^^^^^^^^^^^^^^ Use `compact` instead of `reject { |e| e.nil? }`.
array.lazy.reject! { |e| e.nil? }
^^^^^^^^^^^^^^^^^^^^^^ Use `compact!` instead of `reject! { |e| e.nil? }`.
RUBY
expect_correction(<<~RUBY)
array.lazy.compact
array.lazy.compact!
RUBY
end
end
context 'Ruby <= 3.0', :ruby30 do
it 'does not register an offense and corrects when using `to_enum.reject` on array to reject nils' do
expect_no_offenses(<<~RUBY)
array.to_enum.reject { |e| e.nil? }
array.to_enum.reject! { |e| e.nil? }
RUBY
end
it 'does not register an offense and corrects when using `lazy.reject` on array to reject nils' do
expect_no_offenses(<<~RUBY)
array.lazy.reject { |e| e.nil? }
array.lazy.reject! { |e| e.nil? }
RUBY
end
end
end