forked from rubocop/rubocop
/
redundant_fetch_block_spec.rb
149 lines (124 loc) · 4.43 KB
/
redundant_fetch_block_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
# frozen_string_literal: true
RSpec.describe RuboCop::Cop::Style::RedundantFetchBlock do
subject(:cop) { described_class.new(config) }
context 'with SafeForConstants: true' do
let(:config) do
RuboCop::Config.new(
'Style/RedundantFetchBlock' => {
'SafeForConstants' => true
}
)
end
it 'registers an offense and corrects when using `#fetch` with Integer in the block' do
expect_offense(<<~RUBY)
hash.fetch(:key) { 5 }
^^^^^^^^^^^^^^^^^ Use `fetch(:key, 5)` instead of `fetch(:key) { 5 }`.
RUBY
expect_correction(<<~RUBY)
hash.fetch(:key, 5)
RUBY
end
it 'registers an offense and corrects when using `#fetch` with Float in the block' do
expect_offense(<<~RUBY)
hash.fetch(:key) { 2.5 }
^^^^^^^^^^^^^^^^^^^ Use `fetch(:key, 2.5)` instead of `fetch(:key) { 2.5 }`.
RUBY
expect_correction(<<~RUBY)
hash.fetch(:key, 2.5)
RUBY
end
it 'registers an offense and corrects when using `#fetch` with Symbol in the block' do
expect_offense(<<~RUBY)
hash.fetch(:key) { :value }
^^^^^^^^^^^^^^^^^^^^^^ Use `fetch(:key, :value)` instead of `fetch(:key) { :value }`.
RUBY
expect_correction(<<~RUBY)
hash.fetch(:key, :value)
RUBY
end
it 'registers an offense and corrects when using `#fetch` with Rational in the block' do
expect_offense(<<~RUBY)
hash.fetch(:key) { 2.0r }
^^^^^^^^^^^^^^^^^^^^ Use `fetch(:key, 2.0r)` instead of `fetch(:key) { 2.0r }`.
RUBY
expect_correction(<<~RUBY)
hash.fetch(:key, 2.0r)
RUBY
end
it 'registers an offense and corrects when using `#fetch` with Complex in the block' do
expect_offense(<<~RUBY)
hash.fetch(:key) { 1i }
^^^^^^^^^^^^^^^^^^ Use `fetch(:key, 1i)` instead of `fetch(:key) { 1i }`.
RUBY
expect_correction(<<~RUBY)
hash.fetch(:key, 1i)
RUBY
end
it 'registers an offense and corrects when using `#fetch` with empty block' do
expect_offense(<<~RUBY)
hash.fetch(:key) {}
^^^^^^^^^^^^^^ Use `fetch(:key, nil)` instead of `fetch(:key) {}`.
RUBY
expect_correction(<<~RUBY)
hash.fetch(:key, nil)
RUBY
end
it 'registers an offense and corrects when using `#fetch` with constant in the block' do
expect_offense(<<~RUBY)
hash.fetch(:key) { CONSTANT }
^^^^^^^^^^^^^^^^^^^^^^^^ Use `fetch(:key, CONSTANT)` instead of `fetch(:key) { CONSTANT }`.
RUBY
expect_correction(<<~RUBY)
hash.fetch(:key, CONSTANT)
RUBY
end
it 'registers an offense and corrects when using `#fetch` with String in the block and strings are frozen' do
expect_offense(<<~RUBY)
# frozen_string_literal: true
hash.fetch(:key) { 'value' }
^^^^^^^^^^^^^^^^^^^^^^^ Use `fetch(:key, 'value')` instead of `fetch(:key) { 'value' }`.
RUBY
expect_correction(<<~RUBY)
# frozen_string_literal: true
hash.fetch(:key, 'value')
RUBY
end
it 'does not register an offense when using `#fetch` with String in the block and strings are not frozen' do
expect_no_offenses(<<~RUBY)
hash.fetch(:key) { 'value' }
RUBY
end
it 'does not register an offense when using `#fetch` with argument fallback' do
expect_no_offenses(<<~RUBY)
hash.fetch(:key, :value)
RUBY
end
it 'does not register an offense when using `#fetch` with interpolated Symbol in the block' do
inspect_source('hash.fetch(:key) { :"value_#{value}" }')
expect(cop.offenses.size).to eq(0)
end
it 'does not register an offense when using `#fetch` with an argument in the block' do
inspect_source('hash.fetch(:key) { |k| "missing-#{k}" }')
expect(cop.offenses.size).to eq(0)
end
it 'does not register an offense when using `#fetch` with `Rails.cache`' do
expect_no_offenses(<<~RUBY)
Rails.cache.fetch(:key) { :value }
RUBY
end
end
context 'with SafeForConstants: false' do
let(:config) do
RuboCop::Config.new(
'Style/RedundantFetchBlock' => {
'SafeForConstants' => false
}
)
end
it 'does not register an offense when using `#fetch` with constant in the block' do
expect_no_offenses(<<~RUBY)
hash.fetch(:key) { CONSTANT }
RUBY
end
end
end