-
-
Notifications
You must be signed in to change notification settings - Fork 3k
/
space_inside_percent_literal_delimiters_spec.rb
134 lines (110 loc) · 3.63 KB
/
space_inside_percent_literal_delimiters_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
# frozen_string_literal: true
RSpec.describe RuboCop::Cop::Layout::SpaceInsidePercentLiteralDelimiters, :config do
let(:message) { 'Do not use spaces inside percent literal delimiters.' }
%w[i I w W x].each do |type|
[%w[{ }], %w[( )], %w([ ]), %w[! !]].each do |(ldelim, rdelim)|
context "for #{type} type and #{[ldelim, rdelim]} delimiters" do
define_method(:code_example) { |content| ['%', type, ldelim, content, rdelim].join }
it 'registers an offense for unnecessary spaces' do
expect_offense(<<~RUBY)
#{code_example(' 1 2 ')}
^^ #{message}
^ #{message}
RUBY
expect_correction("#{code_example('1 2')}\n")
end
it 'registers an offense for spaces after first delimiter' do
expect_offense(<<~RUBY)
#{code_example(' 1 2')}
^ #{message}
RUBY
expect_correction("#{code_example('1 2')}\n")
end
it 'registers an offense for spaces before final delimiter' do
expect_offense(<<~RUBY)
#{code_example('1 2 ')}
^ #{message}
RUBY
expect_correction("#{code_example('1 2')}\n")
end
it 'registers an offense for literals with escaped and other spaces' do
expect_offense(<<~RUBY)
#{code_example(' \ a b c\ ')}
^ #{message}
^ #{message}
RUBY
expect_correction("#{code_example('\ a b c\ ')}\n")
end
it 'accepts literals without additional spaces' do
expect_no_offenses(code_example('a b c'))
end
it 'accepts literals with escaped spaces' do
expect_no_offenses(code_example('\ a b c\ '))
end
it 'accepts multi-line literals' do
expect_no_offenses(<<~RUBY)
%#{type}(
a
b
c
)
RUBY
end
it 'accepts multi-line literals within a method' do
expect_no_offenses(<<~RUBY)
def foo
%#{type}(
a
b
c
)
end
RUBY
end
it 'accepts newlines and additional following alignment spaces' do
expect_no_offenses(<<~RUBY)
%#{type}(a b
c)
RUBY
end
it 'accepts spaces between entries' do
expect_no_offenses(code_example('a b c'))
end
context 'with space in blank percent literals' do
it 'registers and corrects an offense' do
expect_offense(<<~RUBY)
#{code_example(' ')}
^ #{message}
RUBY
expect_correction("#{code_example('')}\n")
end
end
context 'with spaces in blank percent literals' do
it 'registers and corrects an offense' do
expect_offense(<<~RUBY)
#{code_example(' ')}
^^ #{message}
RUBY
expect_correction("#{code_example('')}\n")
end
end
context 'with newline in blank percent literals' do
it 'registers and corrects an offense' do
expect_offense(code_example("\n").lines.insert(1, " ^{} #{message}\n").join)
expect_correction(code_example('').to_s)
end
end
end
end
end
it 'accepts other percent literals' do
expect_no_offenses(<<-RUBY)
%q( a b c )
%r( a b c )
%s( a b c )
RUBY
end
it 'accepts execute-string literals' do
expect_no_offenses('` curl `')
end
end