/
literal_in_interpolation_spec.rb
193 lines (165 loc) · 7.1 KB
/
literal_in_interpolation_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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
# frozen_string_literal: true
RSpec.describe RuboCop::Cop::Lint::LiteralInInterpolation do
subject(:cop) { described_class.new }
it 'accepts empty interpolation' do
expect_no_offenses('"this is #{a} silly"')
end
it 'accepts interpolation of xstr' do
expect_no_offenses('"this is #{`a`} silly"')
end
it 'accepts interpolation of irange where endpoints are not literals' do
expect_no_offenses('"this is an irange: #{var1..var2}"')
end
it 'accepts interpolation of erange where endpoints are not literals' do
expect_no_offenses('"this is an erange: #{var1...var2}"')
end
shared_examples 'literal interpolation' do |literal, expected = literal|
it "registers an offense for #{literal} in interpolation" do
inspect_source(%("this is the \#{#{literal}}"))
expect(cop.offenses.size).to eq(1)
end
it "should have #{literal} as the message highlight" do
inspect_source(%("this is the \#{#{literal}}"))
expect(cop.highlights).to eq([literal.to_s])
end
it "removes interpolation around #{literal}" do
corrected = autocorrect_source(%("this is the \#{#{literal}}"))
expect(corrected).to eq(%("this is the #{expected}"))
end
it "removes interpolation around #{literal} when there is more text" do
corrected =
autocorrect_source(%("this is the \#{#{literal}} literally"))
expect(corrected).to eq(%("this is the #{expected} literally"))
end
it "removes interpolation around multiple #{literal}" do
corrected =
autocorrect_source(%("some \#{#{literal}} with \#{#{literal}} too"))
expect(corrected).to eq(%("some #{expected} with #{expected} too"))
end
context 'when there is non-literal and literal interpolation' do
context 'when literal interpolation is before non-literal' do
it 'only remove interpolation around literal' do
corrected =
autocorrect_source(%("this is \#{#{literal}} with \#{a} now"))
expect(corrected).to eq(%("this is #{expected} with \#{a} now"))
end
end
context 'when literal interpolation is after non-literal' do
it 'only remove interpolation around literal' do
corrected =
autocorrect_source(%("this is \#{a} with \#{#{literal}} now"))
expect(corrected).to eq(%("this is \#{a} with #{expected} now"))
end
end
end
it "registers an offense only for final #{literal} in interpolation" do
inspect_source(%("this is the \#{#{literal};#{literal}}"))
expect(cop.offenses.size).to eq(1)
end
end
it_behaves_like('literal interpolation', 1)
it_behaves_like('literal interpolation', -1)
it_behaves_like('literal interpolation', '1_123', '1123')
it_behaves_like('literal interpolation',
'123_456_789_123_456_789', '123456789123456789')
it_behaves_like('literal interpolation', '1.2e-3', '0.0012')
it_behaves_like('literal interpolation', '0xaabb', '43707')
it_behaves_like('literal interpolation', '0o377', '255')
it_behaves_like('literal interpolation', 2.0)
it_behaves_like('literal interpolation', '[]', '[]')
it_behaves_like('literal interpolation', '["a", "b"]', '[\"a\", \"b\"]')
it_behaves_like('literal interpolation', '{"a" => "b"}', '{\"a\" => \"b\"}')
it_behaves_like('literal interpolation', true)
it_behaves_like('literal interpolation', false)
it_behaves_like('literal interpolation', 'nil')
it_behaves_like('literal interpolation', ':symbol', 'symbol')
it_behaves_like('literal interpolation', ':"symbol"', 'symbol')
it_behaves_like('literal interpolation', 1..2)
it_behaves_like('literal interpolation', 1...2)
it_behaves_like('literal interpolation', '%w[]', '[]')
it_behaves_like('literal interpolation', '%w[v1]', '[\"v1\"]')
it_behaves_like('literal interpolation', '%w[v1 v2]', '[\"v1\", \"v2\"]')
it_behaves_like('literal interpolation', '%i[s1 s2]', '[\"s1\", \"s2\"]')
it_behaves_like('literal interpolation', '%I[s1 s2]', '[\"s1\", \"s2\"]')
it_behaves_like('literal interpolation', '%i[s1 s2]', '[\"s1\", \"s2\"]')
it_behaves_like('literal interpolation', '%i[ s1 s2 ]', '[\"s1\", \"s2\"]')
it 'handles nested interpolations when auto-correction' do
corrected = autocorrect_source(%("this is \#{"\#{1}"} silly"))
# next iteration fixes this
expect(corrected).to eq %("this is \#{"1"} silly")
end
shared_examples 'special keywords' do |keyword|
it "accepts strings like #{keyword}" do
expect_no_offenses(<<-RUBY.strip_indent)
%("this is \#{#{keyword}} silly")
RUBY
end
it "does not try to autocorrect strings like #{keyword}" do
corrected = autocorrect_source(%("this is the \#{#{keyword}} silly"))
expect(corrected).to eq(%("this is the \#{#{keyword}} silly"))
end
it "registers an offense for interpolation after #{keyword}" do
inspect_source(%("this is the \#{#{keyword}} \#{1}"))
expect(cop.offenses.size).to eq(1)
end
it "auto-corrects literal interpolation after #{keyword}" do
corrected = autocorrect_source(%("this is the \#{#{keyword}} \#{1}"))
expect(corrected).to eq(%("this is the \#{#{keyword}} 1"))
end
end
it_behaves_like('special keywords', '__FILE__')
it_behaves_like('special keywords', '__LINE__')
it_behaves_like('special keywords', '__RUBY__')
it_behaves_like('special keywords', '__ENCODING__')
shared_examples 'non-special string literal interpolation' do |string|
it "registers an offense for #{string}" do
inspect_source(%("this is the \#{#{string}}"))
expect(cop.offenses.size).to eq(1)
end
it "should have #{string} in the message highlight" do
inspect_source(%("this is the \#{#{string}}"))
expect(cop.highlights).to eq([string])
end
it "should remove the interpolation and quotes around #{string}" do
corrected = autocorrect_source(%("this is the \#{#{string}}"))
expect(corrected).to eq(%("this is the #{string.gsub(/'|"/, '')}"))
end
end
it_behaves_like('non-special string literal interpolation', %('foo'))
it_behaves_like('non-special string literal interpolation', %("foo"))
it 'handles double quotes in single quotes when auto-correction' do
corrected = autocorrect_source(<<-'RUBY'.strip_indent)
"this is #{'"'} silly"
RUBY
expect(corrected).to eq(<<-'RUBY'.strip_indent)
"this is \" silly"
RUBY
end
it 'handles backslach in single quotes when auto-correction' do
corrected = autocorrect_source(<<-'RUBY'.strip_indent)
x = "ABC".gsub(/(A)(B)(C)/, "D#{'\2'}F")
"this is #{'\n'} silly"
"this is #{%q(\n)} silly"
RUBY
expect(corrected).to eq(<<-'RUBY'.strip_indent)
x = "ABC".gsub(/(A)(B)(C)/, "D\\2F")
"this is \\n silly"
"this is \\n silly"
RUBY
end
it 'handles backslach in double quotes when auto-correction' do
corrected = autocorrect_source(<<-'RUBY'.strip_indent)
"this is #{"\n"} silly"
"this is #{%(\n)} silly"
"this is #{%Q(\n)} silly"
RUBY
expect(corrected).to eq(<<-'RUBY'.strip_indent)
"this is
silly"
"this is
silly"
"this is
silly"
RUBY
end
end