-
-
Notifications
You must be signed in to change notification settings - Fork 3k
/
space_inside_hash_literal_braces_spec.rb
219 lines (183 loc) · 5.11 KB
/
space_inside_hash_literal_braces_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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
# frozen_string_literal: true
RSpec.describe RuboCop::Cop::Layout::SpaceInsideHashLiteralBraces, :config do
let(:cop_config) { { 'EnforcedStyle' => 'space' } }
context 'with space inside empty braces not allowed' do
let(:cop_config) { { 'EnforcedStyleForEmptyBraces' => 'no_space' } }
it 'accepts empty braces with no space inside' do
expect_no_offenses('h = {}')
end
it 'registers an offense for empty braces with space inside' do
expect_offense(<<~RUBY)
h = { }
^ Space inside empty hash literal braces detected.
RUBY
expect_correction(<<~RUBY)
h = {}
RUBY
end
end
context 'with space inside empty braces allowed' do
let(:cop_config) { { 'EnforcedStyleForEmptyBraces' => 'space' } }
it 'accepts empty braces with space inside' do
expect_no_offenses('h = { }')
end
it 'registers an offense for empty braces with no space inside' do
expect_offense(<<~RUBY)
h = {}
^ Space inside empty hash literal braces missing.
RUBY
expect_correction(<<~RUBY)
h = { }
RUBY
end
end
it 'registers an offense for hashes with no spaces if so configured' do
expect_offense(<<~RUBY)
h = {a: 1, b: 2}
^ Space inside { missing.
^ Space inside } missing.
h = {a => 1}
^ Space inside { missing.
^ Space inside } missing.
RUBY
expect_correction(<<~RUBY)
h = { a: 1, b: 2 }
h = { a => 1 }
RUBY
end
it 'registers an offense for correct + opposite' do
expect_offense(<<~RUBY)
h = { a: 1}
^ Space inside } missing.
RUBY
expect_correction(<<~RUBY)
h = { a: 1 }
RUBY
end
context 'when EnforcedStyle is no_space' do
let(:cop_config) { { 'EnforcedStyle' => 'no_space' } }
it 'registers an offense for hashes with spaces' do
expect_offense(<<~RUBY)
h = { a: 1, b: 2 }
^ Space inside { detected.
^ Space inside } detected.
RUBY
expect_correction(<<~RUBY)
h = {a: 1, b: 2}
RUBY
end
it 'registers an offense for opposite + correct' do
expect_offense(<<~RUBY)
h = {a: 1 }
^ Space inside } detected.
RUBY
expect_correction(<<~RUBY)
h = {a: 1}
RUBY
end
it 'accepts hashes with no spaces' do
expect_no_offenses(<<~RUBY)
h = {a: 1, b: 2}
h = {a => 1}
RUBY
end
it 'accepts multiline hash' do
expect_no_offenses(<<~RUBY)
h = {
a: 1,
b: 2,
}
RUBY
end
it 'accepts multiline hash with comment' do
expect_no_offenses(<<~RUBY)
h = { # Comment
a: 1,
b: 2,
}
RUBY
end
end
context 'when EnforcedStyle is compact' do
let(:cop_config) { { 'EnforcedStyle' => 'compact' } }
it "doesn't register an offense for non-nested hashes with spaces" do
expect_no_offenses(<<~RUBY)
h = { a: 1, b: 2 }
RUBY
end
it 'registers an offense for nested hashes with spaces' do
expect_offense(<<~RUBY)
h = { a: { a: 1, b: 2 } }
^ Space inside } detected.
RUBY
expect_correction(<<~RUBY)
h = { a: { a: 1, b: 2 }}
RUBY
end
it 'registers an offense for opposite + correct' do
expect_offense(<<~RUBY)
h = {a: 1 }
^ Space inside { missing.
RUBY
expect_correction(<<~RUBY)
h = { a: 1 }
RUBY
end
it 'registers offenses for hashes with no spaces' do
expect_offense(<<~RUBY)
h = {a: 1, b: 2}
^ Space inside } missing.
^ Space inside { missing.
h = {a => 1}
^ Space inside } missing.
^ Space inside { missing.
RUBY
expect_correction(<<~RUBY)
h = { a: 1, b: 2 }
h = { a => 1 }
RUBY
end
it 'accepts multiline hash' do
expect_no_offenses(<<~RUBY)
h = {
a: 1,
b: 2,
}
RUBY
end
it 'accepts multiline hash with comment' do
expect_no_offenses(<<~RUBY)
h = { # Comment
a: 1,
b: 2,
}
RUBY
end
end
it 'accepts hashes with spaces by default' do
expect_no_offenses(<<~RUBY)
h = { a: 1, b: 2 }
h = { a => 1 }
RUBY
end
it 'accepts hash literals with no braces' do
expect_no_offenses('x(a: b.c)')
end
it 'can handle interpolation in a braceless hash literal' do
# A tricky special case where the closing brace of the
# interpolation risks getting confused for a hash literal brace.
expect_no_offenses('f(get: "#{x}")')
end
context 'on Hash[{ x: 1 } => [1]]' do
# regression test; see GH issue 2436
it 'does not register an offense' do
expect_no_offenses('Hash[{ x: 1 } => [1]]')
end
end
context 'on { key: "{" }' do
# regression test; see GH issue 3958
it 'does not register an offense' do
expect_no_offenses('{ key: "{" }')
end
end
end