forked from rubocop/rubocop
/
string_concatenation_spec.rb
125 lines (105 loc) · 3.51 KB
/
string_concatenation_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
# frozen_string_literal: true
RSpec.describe RuboCop::Cop::Style::StringConcatenation do
subject(:cop) { described_class.new }
it 'registers an offense and corrects for string concatenation' do
expect_offense(<<~RUBY)
email_with_name = user.name + ' <' + user.email + '>'
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer string interpolation to string concatenation.
RUBY
expect_correction(<<~RUBY)
email_with_name = "\#{user.name} <\#{user.email}>"
RUBY
end
it 'registers an offense and corrects for string concatenation as part of other expression' do
expect_offense(<<~RUBY)
users = (user.name + ' ' + user.email) * 5
^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer string interpolation to string concatenation.
RUBY
expect_correction(<<~RUBY)
users = ("\#{user.name} \#{user.email}") * 5
RUBY
end
it 'correctly handles strings with special characters' do
expect_offense(<<-RUBY)
email_with_name = "\\n" + user.name + ' ' + user.email + '\\n'
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer string interpolation to string concatenation.
RUBY
expect_correction(<<-RUBY)
email_with_name = "\\n\#{user.name} \#{user.email}\\\\n"
RUBY
end
it 'does not register an offense when using `+` with all non string arguments' do
expect_no_offenses(<<~RUBY)
user.name + user.email
RUBY
end
context 'multiline' do
context 'simple expressions' do
it 'registers an offense and corrects' do
expect_offense(<<-RUBY)
email_with_name = user.name +
^^^^^^^^^^^ Prefer string interpolation to string concatenation.
' ' +
user.email +
'\\n'
RUBY
expect_correction(<<-RUBY)
email_with_name = "\#{user.name} \#{user.email}\\\\n"
RUBY
end
end
context 'if condition' do
it 'registers an offense but does not correct' do
expect_offense(<<~RUBY)
"result:" + if condition
^^^^^^^^^^^^^^^^^^^^^^^^ Prefer string interpolation to string concatenation.
"true"
else
"false"
end
RUBY
expect_no_corrections
end
end
context 'multiline block' do
it 'registers an offense but does not correct' do
expect_offense(<<~RUBY)
'(' + values.map do |v|
^^^^^^^^^^^^^^^^^^^^^^^ Prefer string interpolation to string concatenation.
v.titleize
end.join(', ') + ')'
RUBY
expect_no_corrections
end
end
end
context 'nested interpolation' do
it 'registers an offense but does not correct' do
expect_offense(<<~RUBY)
"foo" + "bar: \#{baz}"
^^^^^^^^^^^^^^^^^^^^^ Prefer string interpolation to string concatenation.
RUBY
expect_no_corrections
end
end
context 'inline block' do
it 'registers an offense but does not correct' do
expect_offense(<<~RUBY)
'(' + values.map { |v| v.titleize }.join(', ') + ')'
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer string interpolation to string concatenation.
RUBY
expect_no_corrections
end
end
context 'heredoc' do
it 'registers an offense but does not correct' do
expect_offense(<<~RUBY)
"foo" + <<~STR
^^^^^^^^^^^^^^ Prefer string interpolation to string concatenation.
text
STR
RUBY
expect_no_corrections
end
end
end