-
-
Notifications
You must be signed in to change notification settings - Fork 3k
/
space_before_block_braces_spec.rb
159 lines (132 loc) · 3.82 KB
/
space_before_block_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
# frozen_string_literal: true
RSpec.describe RuboCop::Cop::Layout::SpaceBeforeBlockBraces, :config do
let(:cop_config) { { 'EnforcedStyle' => 'space' } }
context 'when EnforcedStyle is space' do
it 'accepts braces surrounded by spaces' do
expect_no_offenses('each { puts }')
end
it 'registers an offense and corrects left brace without outer space' do
expect_offense(<<~RUBY)
each{ puts }
^ Space missing to the left of {.
RUBY
expect_correction(<<~RUBY)
each { puts }
RUBY
end
it 'registers an offense and corrects opposite + correct style' do
expect_offense(<<~RUBY)
each{ puts }
^ Space missing to the left of {.
each { puts }
RUBY
expect_correction(<<~RUBY)
each { puts }
each { puts }
RUBY
end
it 'registers an offense and corrects multiline block where the left ' \
'brace has no outer space' do
expect_offense(<<~RUBY)
foo.map{ |a|
^ Space missing to the left of {.
a.bar.to_s
}
RUBY
expect_correction(<<~RUBY)
foo.map { |a|
a.bar.to_s
}
RUBY
end
end
context 'when EnforcedStyle is no_space' do
let(:cop_config) { { 'EnforcedStyle' => 'no_space' } }
it 'registers an offense and corrects braces surrounded by spaces' do
expect_offense(<<~RUBY)
each { puts }
^ Space detected to the left of {.
RUBY
expect_correction(<<~RUBY)
each{ puts }
RUBY
end
it 'registers an offense and corrects correct + opposite style' do
expect_offense(<<~RUBY)
each{ puts }
each { puts }
^ Space detected to the left of {.
RUBY
expect_correction(<<~RUBY)
each{ puts }
each{ puts }
RUBY
end
it 'accepts left brace without outer space' do
expect_no_offenses('each{ puts }')
end
context 'with `EnforcedStyle` of `Style/BlockDelimiters`' do
let(:config) do
merged_config = RuboCop::ConfigLoader.default_configuration[
'Layout/SpaceBeforeBlockBraces'
].merge(cop_config)
RuboCop::Config.new(
'Layout/SpaceBeforeBlockBraces' => merged_config,
'Style/BlockDelimiters' => { 'EnforcedStyle' => 'line_count_based' }
)
end
it 'accepts left brace without outer space' do
expect_no_offenses(<<~RUBY)
let(:foo){{foo: 1, bar: 2}}
RUBY
end
end
end
context 'with space before empty braces not allowed' do
let(:cop_config) do
{
'EnforcedStyle' => 'space',
'EnforcedStyleForEmptyBraces' => 'no_space'
}
end
it 'accepts empty braces without outer space' do
expect_no_offenses('->{}')
end
it 'registers an offense and corrects empty braces' do
expect_offense(<<~RUBY)
-> {}
^ Space detected to the left of {.
RUBY
expect_correction(<<~RUBY)
->{}
RUBY
end
end
context 'with space before empty braces allowed' do
let(:cop_config) do
{
'EnforcedStyle' => 'no_space',
'EnforcedStyleForEmptyBraces' => 'space'
}
end
it 'accepts empty braces with outer space' do
expect_no_offenses('-> {}')
end
it 'registers an offense and corrects empty braces' do
expect_offense(<<~RUBY)
->{}
^ Space missing to the left of {.
RUBY
expect_correction(<<~RUBY)
-> {}
RUBY
end
end
context 'with invalid value for EnforcedStyleForEmptyBraces' do
let(:cop_config) { { 'EnforcedStyleForEmptyBraces' => 'unknown' } }
it 'fails with an error' do
expect { inspect_source('each {}') }
.to raise_error('Unknown EnforcedStyleForEmptyBraces selected!')
end
end
end