-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
unfreeze_string_spec.rb
116 lines (97 loc) · 2.69 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
# frozen_string_literal: true
RSpec.describe RuboCop::Cop::Performance::UnfreezeString, :config 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 string' \
'interpolation 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
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
end