forked from rubocop/rubocop
/
redundant_freeze_spec.rb
99 lines (79 loc) · 3.24 KB
/
redundant_freeze_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
# frozen_string_literal: true
RSpec.describe RuboCop::Cop::Style::RedundantFreeze do
subject(:cop) { described_class.new }
let(:prefix) { nil }
shared_examples 'immutable objects' do |o|
it "registers an offense for frozen #{o}" do
source = [prefix, "CONST = #{o}.freeze"].compact.join("\n")
inspect_source(source)
expect(cop.offenses.size).to eq(1)
end
it 'auto-corrects by removing .freeze' do
source = [prefix, "CONST = #{o}.freeze"].compact.join("\n")
new_source = autocorrect_source(source)
expect(new_source).to eq(source.chomp('.freeze'))
end
end
it_behaves_like 'immutable objects', '1'
it_behaves_like 'immutable objects', '1.5'
it_behaves_like 'immutable objects', ':sym'
it_behaves_like 'immutable objects', ':""'
it_behaves_like 'immutable objects', "ENV['foo']"
it_behaves_like 'immutable objects', "'foo'.count"
it_behaves_like 'immutable objects', '(1 + 2)'
it_behaves_like 'immutable objects', '(2 > 1)'
it_behaves_like 'immutable objects', "('a' > 'b')"
it_behaves_like 'immutable objects', '(a > b)'
it_behaves_like 'immutable objects', '[1, 2, 3].size'
shared_examples 'mutable objects' do |o|
it "allows #{o} with freeze" do
source = [prefix, "CONST = #{o}.freeze"].compact.join("\n")
expect_no_offenses(source)
end
end
it_behaves_like 'mutable objects', '[1, 2, 3]'
it_behaves_like 'mutable objects', '{ a: 1, b: 2 }'
it_behaves_like 'mutable objects', "'str'"
it_behaves_like 'mutable objects', '"top#{1 + 2}"'
it_behaves_like 'mutable objects', '/./'
it_behaves_like 'mutable objects', '(1..5)'
it_behaves_like 'mutable objects', '(1...5)'
it_behaves_like 'mutable objects', "('a' + 'b')"
it_behaves_like 'mutable objects', "('a' * 20)"
it_behaves_like 'mutable objects', '(a + b)'
it 'allows .freeze on method call' do
expect_no_offenses('TOP_TEST = Something.new.freeze')
end
context 'when the receiver is a frozen string literal' do
if RuboCop::Config::KNOWN_RUBIES.include?(3.0)
context 'when the target ruby version >= 3.0' do
let(:ruby_version) { 3.0 }
context 'when the frozen string literal comment is missing' do
it_behaves_like 'immutable objects', '"#{a}"'
end
context 'when the frozen string literal comment is true' do
let(:prefix) { '# frozen_string_literal: true' }
it_behaves_like 'immutable objects', '"#{a}"'
end
context 'when the frozen string literal comment is false' do
let(:prefix) { '# frozen_string_literal: false' }
it_behaves_like 'immutable objects', '"#{a}"'
end
end
end
context 'when the target ruby version >= 2.3' do
let(:ruby_version) { 2.3 }
context 'when the frozen string literal comment is missing' do
it_behaves_like 'mutable objects', '"#{a}"'
end
context 'when the frozen string literal comment is true' do
let(:prefix) { '# frozen_string_literal: true' }
it_behaves_like 'immutable objects', '"#{a}"'
end
context 'when the frozen string literal comment is false' do
let(:prefix) { '# frozen_string_literal: false' }
it_behaves_like 'mutable objects', '"#{a}"'
end
end
end
end