forked from rubocop/rubocop
/
multiline_when_then_spec.rb
134 lines (120 loc) · 2.81 KB
/
multiline_when_then_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
# frozen_string_literal: true
RSpec.describe RuboCop::Cop::Style::MultilineWhenThen do
subject(:cop) { described_class.new }
it 'registers an offense for empty when statement with then' do
expect_offense(<<~RUBY)
case foo
when bar then
^^^^ Do not use `then` for multiline `when` statement.
end
RUBY
end
it 'registers an offense for multiline when statement with then' do
expect_offense(<<~RUBY)
case foo
when bar then
^^^^ Do not use `then` for multiline `when` statement.
do_something
end
RUBY
end
it "doesn't register an offense for singleline when statement with then" do
expect_no_offenses(<<~RUBY)
case foo
when bar then do_something
end
RUBY
end
it "doesn't register an offense when `then` required for a body of `when` " \
'is used' do
expect_no_offenses(<<~RUBY)
case cond
when foo then do_something(arg1,
arg2)
end
RUBY
end
it "doesn't register an offense for multiline when statement
with then followed by other lines" do
expect_no_offenses(<<~RUBY)
case foo
when bar then do_something
do_another_thing
end
RUBY
end
it "doesn't register an offense for empty when statement without then" do
expect_no_offenses(<<~RUBY)
case foo
when bar
end
RUBY
end
it "doesn't register an offense for multiline when statement without then" do
expect_no_offenses(<<~RUBY)
case foo
when bar
do_something
end
RUBY
end
it 'does not register an offense for hash when statement with then' do
expect_no_offenses(<<~RUBY)
case condition
when foo then {
key: 'value'
}
end
RUBY
end
it 'does not register an offense for array when statement with then' do
expect_no_offenses(<<~RUBY)
case condition
when foo then [
'element'
]
end
RUBY
end
it 'autocorrects then in empty when' do
new_source = autocorrect_source(<<~RUBY)
case foo
when bar then
end
RUBY
expect(new_source).to eq(<<~RUBY)
case foo
when bar
end
RUBY
end
it 'autocorrects then in multiline when' do
new_source = autocorrect_source(<<~RUBY)
case foo
when bar then
do_something
end
RUBY
expect(new_source).to eq(<<~RUBY)
case foo
when bar
do_something
end
RUBY
end
it 'autocorrects when the body of `when` branch starts ' \
'with `then`' do
new_source = autocorrect_source(<<~RUBY)
case foo
when bar
then do_something
end
RUBY
expect(new_source).to eq(<<~RUBY)
case foo
when bar
do_something
end
RUBY
end
end