/
redundant_freeze_spec.rb
132 lines (107 loc) · 4.46 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
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
# frozen_string_literal: true
RSpec.describe RuboCop::Cop::Style::RedundantFreeze, :config do
let(:prefix) { nil }
shared_examples 'immutable objects' do |o|
it "registers an offense for frozen #{o}" do
expect_offense([prefix, <<~RUBY].compact.join("\n"), o: o)
CONST = %{o}.freeze
^{o}^^^^^^^ Do not freeze immutable objects, as freezing them has no effect.
RUBY
expect_correction([prefix, <<~RUBY].compact.join("\n"))
CONST = #{o}
RUBY
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', "'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', "('a' + 'b')"
it_behaves_like 'mutable objects', "('a' * 20)"
it_behaves_like 'mutable objects', '(a + b)'
it_behaves_like 'mutable objects', '([42] * 42)'
it_behaves_like 'mutable objects', "ENV['foo']"
it_behaves_like 'mutable objects', "::ENV['foo']"
it 'allows .freeze on method call' do
expect_no_offenses('TOP_TEST = Something.new.freeze')
end
context 'when the receiver is a string literal' do
# TODO : It is not yet decided when frozen string will be the default.
# It has been abandoned in the Ruby 3.0 period, but may default in
# the long run. So these tests are left with a provisional value of 4.0.
if RuboCop::TargetRuby.supported_versions.include?(4.0)
context 'when the target ruby version >= 4.0' do
let(:ruby_version) { 4.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 'Ruby 3.0 or higher', :ruby30 do
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 'mutable 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
context 'Ruby 2.7 or lower', :ruby27 do
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
describe 'Regexp and Range literals' do
# Ruby 3.0's Regexp and Range literals are frozen.
#
# https://bugs.ruby-lang.org/issues/15504
# https://bugs.ruby-lang.org/issues/16377
context 'Ruby 3.0 or higher', :ruby30 do
it_behaves_like 'immutable objects', '/./'
it_behaves_like 'immutable objects', '(1..5)'
it_behaves_like 'immutable objects', '(1...5)'
end
context 'Ruby 2.7 or lower', :ruby27 do
it_behaves_like 'mutable objects', '/./'
it_behaves_like 'mutable objects', '(1..5)'
it_behaves_like 'mutable objects', '(1...5)'
end
end
end
end