/
parentheses_as_grouped_expression_spec.rb
101 lines (84 loc) · 2.36 KB
/
parentheses_as_grouped_expression_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
# frozen_string_literal: true
RSpec.describe RuboCop::Cop::Lint::ParenthesesAsGroupedExpression do
subject(:cop) { described_class.new }
it 'registers an offense and corrects for method call with space before ' \
'the parenthesis' do
expect_offense(<<~RUBY)
a.func (x)
^ `(...)` interpreted as grouped expression.
RUBY
expect_correction(<<~RUBY)
a.func(x)
RUBY
end
it 'registers an offense and corrects for predicate method call with space ' \
'before the parenthesis' do
expect_offense(<<~RUBY)
is? (x)
^ `(...)` interpreted as grouped expression.
RUBY
expect_correction(<<~RUBY)
is?(x)
RUBY
end
it 'does not register an offense for math expression' do
expect_no_offenses(<<~RUBY)
puts (2 + 3) * 4
RUBY
end
it 'does not register an offense for math expression with `to_i`' do
expect_no_offenses(<<~RUBY)
do_something.eq (foo * bar).to_i
RUBY
end
it 'accepts a method call without arguments' do
expect_no_offenses('func')
end
it 'accepts a method call with arguments but no parentheses' do
expect_no_offenses('puts x')
end
it 'accepts a chain of method calls' do
expect_no_offenses(<<~RUBY)
a.b
a.b 1
a.b(1)
RUBY
end
it 'accepts method with parens as arg to method without' do
expect_no_offenses('a b(c)')
end
it 'accepts an operator call with argument in parentheses' do
expect_no_offenses(<<~RUBY)
a % (b + c)
a.b = (c == d)
RUBY
end
it 'accepts a space inside opening paren followed by left paren' do
expect_no_offenses('a( (b) )')
end
it 'does not register an offense for a call with multiple arguments' do
expect_no_offenses('assert_equal (0..1.9), acceleration.domain')
end
it 'does not register an offesne when heredoc has a space between the same string as the method name and `(`' do
expect_no_offenses(<<~RUBY)
foo(
<<~EOS
foo (
)
EOS
)
RUBY
end
context 'when using safe navigation operator' do
it 'registers an offense and corrects for method call with space before ' \
'the parenthesis' do
expect_offense(<<~RUBY)
a&.func (x)
^ `(...)` interpreted as grouped expression.
RUBY
expect_correction(<<~RUBY)
a&.func(x)
RUBY
end
end
end