forked from rubocop/rubocop
/
multiline_ternary_operator_spec.rb
59 lines (51 loc) · 1.31 KB
/
multiline_ternary_operator_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
# frozen_string_literal: true
RSpec.describe RuboCop::Cop::Style::MultilineTernaryOperator do
subject(:cop) { described_class.new }
it 'registers offense and corrects when the if branch and the else branch are ' \
'on a separate line from the condition' do
expect_offense(<<~RUBY)
a = cond ?
^^^^^^ Avoid multi-line ternary operators, use `if` or `unless` instead.
b : c
RUBY
expect_correction(<<~RUBY)
a = if cond
b
else
c
end
RUBY
end
it 'registers an offense and corrects when the false branch is on a separate line' do
expect_offense(<<~RUBY)
a = cond ? b :
^^^^^^^^^^ Avoid multi-line ternary operators, use `if` or `unless` instead.
c
RUBY
expect_correction(<<~RUBY)
a = if cond
b
else
c
end
RUBY
end
it 'registers an offense and corrects when everything is on a separate line' do
expect_offense(<<~RUBY)
a = cond ?
^^^^^^ Avoid multi-line ternary operators, use `if` or `unless` instead.
b :
c
RUBY
expect_correction(<<~RUBY)
a = if cond
b
else
c
end
RUBY
end
it 'accepts a single line ternary operator expression' do
expect_no_offenses('a = cond ? b : c')
end
end