-
-
Notifications
You must be signed in to change notification settings - Fork 79
/
unfreeze_string_spec.rb
133 lines (112 loc) · 3.24 KB
/
unfreeze_string_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
# frozen_string_literal: true
RSpec.describe RuboCop::Cop::Performance::UnfreezeString, :config do
context 'when Ruby >= 3.3', :ruby33 do
it 'does not register an offense and corrects for an empty string with `.dup`' do
expect_no_offenses(<<~RUBY)
"".dup
RUBY
end
end
context 'when Ruby <= 3.2', :ruby32, unsupported_on: :prism do
it 'registers an offense and corrects for an empty string with `.dup`' do
expect_offense(<<~RUBY)
"".dup
^^^^^^ Use unary plus to get an unfrozen string literal.
RUBY
expect_correction(<<~RUBY)
+""
RUBY
end
it 'registers an offense and corrects for a string with `.dup`' do
expect_offense(<<~RUBY)
"foo".dup
^^^^^^^^^ Use unary plus to get an unfrozen string literal.
RUBY
expect_correction(<<~RUBY)
+"foo"
RUBY
end
it 'registers an offense and corrects for a heredoc with `.dup`' do
expect_offense(<<~RUBY)
<<TEXT.dup
^^^^^^^^^^ Use unary plus to get an unfrozen string literal.
foo
bar
TEXT
RUBY
expect_correction(<<~RUBY)
+<<TEXT
foo
bar
TEXT
RUBY
end
it 'registers an offense and corrects for a string that contains a stringinterpolation with `.dup`' do
expect_offense(<<~'RUBY')
"foo#{bar}baz".dup
^^^^^^^^^^^^^^^^^^ Use unary plus to get an unfrozen string literal.
RUBY
expect_correction(<<~'RUBY')
+"foo#{bar}baz"
RUBY
end
end
it 'registers an offense and corrects for `String.new`' do
expect_offense(<<~RUBY)
String.new
^^^^^^^^^^ Use unary plus to get an unfrozen string literal.
RUBY
expect_correction(<<~RUBY)
+''
RUBY
end
it 'registers an offense and corrects for `String.new` with an empty string' do
expect_offense(<<~RUBY)
String.new('')
^^^^^^^^^^^^^^ Use unary plus to get an unfrozen string literal.
RUBY
expect_correction(<<~RUBY)
+''
RUBY
end
it 'registers an offense and corrects for `String.new` with a string' do
expect_offense(<<~RUBY)
String.new('foo')
^^^^^^^^^^^^^^^^^ Use unary plus to get an unfrozen string literal.
RUBY
expect_correction(<<~RUBY)
+'foo'
RUBY
end
it 'registers an offense and corrects when invoking a method after `String.new` with a string' do
expect_offense(<<~RUBY)
String.new('foo').force_encoding(Encoding::ASCII)
^^^^^^^^^^^^^^^^^ Use unary plus to get an unfrozen string literal.
RUBY
expect_correction(<<~RUBY)
(+'foo').force_encoding(Encoding::ASCII)
RUBY
end
it 'accepts an empty string with unary plus operator' do
expect_no_offenses(<<~RUBY)
+""
RUBY
end
it 'accepts a string with unary plus operator' do
expect_no_offenses(<<~RUBY)
+"foobar"
RUBY
end
it 'accepts `String.new` with capacity option' do
expect_no_offenses(<<~RUBY)
String.new(capacity: 100)
RUBY
end
context 'when Ruby <= 2.2', :ruby22, unsupported_on: :prism do
it 'does not register an offense for an empty string with `.dup`' do
expect_no_offenses(<<~RUBY)
"".dup
RUBY
end
end
end