forked from rubocop/rubocop
/
space_before_first_arg_spec.rb
167 lines (143 loc) · 4.93 KB
/
space_before_first_arg_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
# frozen_string_literal: true
RSpec.describe RuboCop::Cop::Layout::SpaceBeforeFirstArg, :config do
subject(:cop) { described_class.new(config) }
let(:cop_config) { { 'AllowForAlignment' => true } }
context 'for method calls without parentheses' do
it 'registers an offense and corrects method call with two spaces ' \
'before the first arg' do
expect_offense(<<~RUBY)
something x
^^ Put one space between the method name and the first argument.
a.something y, z
^^ Put one space between the method name and the first argument.
RUBY
expect_correction(<<~RUBY)
something x
a.something y, z
RUBY
end
context 'when using safe navigation operator' do
it 'registers an offense and corrects method call with two spaces ' \
'before the first arg' do
expect_offense(<<~RUBY)
a&.something y, z
^^ Put one space between the method name and the first argument.
RUBY
expect_correction(<<~RUBY)
a&.something y, z
RUBY
end
end
it 'registers an offense for method call with no spaces before the '\
'first arg' do
inspect_source(<<~RUBY)
something'hello'
a.something'hello world'
RUBY
expect(cop.messages)
.to eq(['Put one space between the method name and the first ' \
'argument.'] * 2)
end
it 'auto-corrects missing space' do
new_source = autocorrect_source(<<~RUBY)
something'hello'
a.something'hello world'
RUBY
expect(new_source).to eq(<<~RUBY)
something 'hello'
a.something 'hello world'
RUBY
end
context 'when a vertical argument positions are aligned' do
it 'registers an offense' do
inspect_source(<<~RUBY)
obj = a_method(arg, arg2)
obj.no_parenthesized'asdf'
RUBY
expect(cop.messages).to eq(
['Put one space between the method name and the first argument.']
)
end
it 'auto-corrects missing space' do
new_source = autocorrect_source(<<~RUBY)
obj = a_method(arg, arg2)
obj.no_parenthesized'asdf'
RUBY
expect(new_source).to eq(<<~RUBY)
obj = a_method(arg, arg2)
obj.no_parenthesized 'asdf'
RUBY
end
end
it 'accepts a method call with one space before the first arg' do
expect_no_offenses(<<~RUBY)
something x
a.something y, z
RUBY
end
it 'accepts + operator' do
expect_no_offenses(<<~RUBY)
something +
x
RUBY
end
it 'accepts setter call' do
expect_no_offenses(<<~RUBY)
something.x =
y
RUBY
end
it 'accepts multiple space containing line break' do
expect_no_offenses(<<~RUBY)
something \\
x
RUBY
end
context 'when AllowForAlignment is true' do
it 'accepts method calls with aligned first arguments' do
expect_no_offenses(<<~RUBY)
form.inline_input :full_name, as: :string
form.disabled_input :password, as: :passwd
form.masked_input :zip_code, as: :string
form.masked_input :email_address, as: :email
form.masked_input :phone_number, as: :tel
RUBY
end
end
context 'when AllowForAlignment is false' do
let(:cop_config) { { 'AllowForAlignment' => false } }
it 'registers an offense and corrects method calls ' \
'with aligned first arguments' do
expect_offense(<<~RUBY)
form.inline_input :full_name, as: :string
^^^ Put one space between the method name and the first argument.
form.disabled_input :password, as: :passwd
form.masked_input :zip_code, as: :string
^^^ Put one space between the method name and the first argument.
form.masked_input :email_address, as: :email
^^^ Put one space between the method name and the first argument.
form.masked_input :phone_number, as: :tel
^^^ Put one space between the method name and the first argument.
RUBY
expect_correction(<<~RUBY)
form.inline_input :full_name, as: :string
form.disabled_input :password, as: :passwd
form.masked_input :zip_code, as: :string
form.masked_input :email_address, as: :email
form.masked_input :phone_number, as: :tel
RUBY
end
end
end
context 'for method calls with parentheses' do
it 'accepts a method call without space' do
expect_no_offenses(<<~RUBY)
something(x)
a.something(y, z)
RUBY
end
it 'accepts a method call with space after the left parenthesis' do
expect_no_offenses('something( x )')
end
end
end