/
select_map_spec.rb
140 lines (121 loc) · 4.72 KB
/
select_map_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
# frozen_string_literal: true
RSpec.describe RuboCop::Cop::Performance::SelectMap, :config do
context 'when TargetRubyVersion >= 2.7', :ruby27 do
it 'registers an offense when using `select(&:...).map(&:...)`' do
expect_offense(<<~RUBY)
ary.select(&:present?).map(&:to_i)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `filter_map` instead of `select.map`.
RUBY
end
it 'registers an offense when using `select(&:...)&.map(&:...)`' do
expect_offense(<<~RUBY)
ary&.select(&:present?)&.map(&:to_i)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `filter_map` instead of `select.map`.
RUBY
end
it 'registers an offense when using `filter(&:...).map(&:...)`' do
expect_offense(<<~RUBY)
ary.filter(&:present?).map(&:to_i)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `filter_map` instead of `filter.map`.
RUBY
end
it 'registers an offense when using `select(&:...).map(&:...)` with safe navigation operator' do
expect_offense(<<~RUBY)
ary&.select(&:present?).map(&:to_i)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `filter_map` instead of `select.map`.
RUBY
end
it 'registers an offense when using `filter(&:...).map(&:...)` with safe navitaion operator' do
expect_offense(<<~RUBY)
ary&.filter(&:present?).map(&:to_i)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `filter_map` instead of `filter.map`.
RUBY
end
it 'registers an offense when using `select { ... }.map { ... }`' do
expect_offense(<<~RUBY)
ary.select { |o| o.present? }.map { |o| o.to_i }
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `filter_map` instead of `select.map`.
RUBY
end
it 'registers an offense when using `select { ... }&.map { ... }`' do
expect_offense(<<~RUBY)
ary&.select { |o| o.present? }&.map { |o| o.to_i }
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `filter_map` instead of `select.map`.
RUBY
end
it 'registers an offense when using `select { ... }.map(&:...)`' do
expect_offense(<<~RUBY)
ary.select { |o| o.present? }.map(&:to_i)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `filter_map` instead of `select.map`.
RUBY
end
it 'registers an offense when using `select(&:...).map { ... }`' do
expect_offense(<<~RUBY)
ary.select(&:present?).map { |o| o.to_i }
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `filter_map` instead of `select.map`.
RUBY
end
it 'registers an offense when using `select(&:...).map(&:...)` in method chain' do
expect_offense(<<~RUBY)
ary.do_something.select(&:present?).map(&:to_i).max
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `filter_map` instead of `select.map`.
RUBY
end
it 'registers an offense when using `select(&:...).map(&:...)` without receiver' do
expect_offense(<<~RUBY)
select(&:present?).map(&:to_i)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `filter_map` instead of `select.map`.
RUBY
end
it 'does not register an offense when using `filter_map`' do
expect_no_offenses(<<~RUBY)
ary.filter_map { |o| o.to_i if o.present? }
RUBY
end
it 'does not register an offense when using `select`' do
expect_no_offenses(<<~RUBY)
ary.select(&:present?)
RUBY
end
it 'does not register an offense when using `select(key: value).map`' do
expect_no_offenses(<<~RUBY)
ary.do_something.select(key: value).map(&:to_i)
RUBY
end
it 'does not register an offense when using `select` with assignment' do
expect_no_offenses(<<~RUBY)
ret = select
RUBY
end
it 'does not register an offense when using `select(&:...).stranger.map(&:...)`' do
expect_no_offenses(<<~RUBY)
ary.do_something.select(&:present?).stranger.map(&:to_i).max
RUBY
end
it 'does not register an offense when using `select { ... }.stranger.map { ... }`' do
expect_no_offenses(<<~RUBY)
ary.select { |o| o.present? }.stranger.map { |o| o.to_i }
RUBY
end
#
# `compact` is not compatible with `filter_map`.
#
# [true, false, nil].compact #=> [true, false]
# [true, false, nil].filter_map(&:itself) #=> [true]
#
# Use `Performance/MapCompact` cop.
#
it 'does not register an offense when using `map(&:...).compact`' do
expect_no_offenses(<<~RUBY)
ary.map(&:to_i).compact
RUBY
end
end
context 'when TargetRubyVersion <= 2.6', :ruby26, unsupported_on: :prism do
it 'does not register an offense when using `select.map`' do
expect_no_offenses(<<~RUBY)
ary.select(&:present?).map(&:to_i)
RUBY
end
end
end