/
parentheses_as_grouped_expression_spec.rb
78 lines (65 loc) · 1.92 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
# frozen_string_literal: true
RSpec.describe RuboCop::Cop::Lint::ParenthesesAsGroupedExpression do
subject(:cop) { described_class.new }
it 'registers an offense for method call with space before the ' \
'parenthesis' do
expect_offense(<<~RUBY)
a.func (x)
^ `(...)` interpreted as grouped expression.
RUBY
end
it 'registers an offense for predicate method call with space ' \
'before the parenthesis' do
expect_offense(<<~RUBY)
is? (x)
^ `(...)` interpreted as grouped expression.
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
context 'when using safe navigation operator' do
it 'registers an offense for method call with space before the ' \
'parenthesis' do
expect_offense(<<~RUBY)
a&.func (x)
^ `(...)` interpreted as grouped expression.
RUBY
end
end
end