/
single_line_block_chain_spec.rb
55 lines (47 loc) · 1.71 KB
/
single_line_block_chain_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
# frozen_string_literal: true
RSpec.describe RuboCop::Cop::Layout::SingleLineBlockChain, :config do
it 'registers an offense for method call chained on the same line as a block' do
expect_offense(<<~RUBY)
example.select { |item| item.cond? }.join('-')
^^^^^ Put method call on a separate line if chained to a single line block.
RUBY
expect_correction(<<~RUBY)
example.select { |item| item.cond? }
.join('-')
RUBY
end
it 'registers an offense for no selector method call chained on the same line as a block' do
expect_offense(<<~RUBY)
example.select { |item| item.cond? }.(42)
^^ Put method call on a separate line if chained to a single line block.
RUBY
expect_correction(<<~RUBY)
example.select { |item| item.cond? }
.(42)
RUBY
end
it 'does not register an offense for method call chained on a new line after a single line block' do
expect_no_offenses(<<~RUBY)
example.select { |item| item.cond? }
.join('-')
RUBY
end
it 'does not register an offense for method call chained on a new line after a single line block with trailing dot' do
expect_no_offenses(<<~RUBY)
example.select { |item| item.cond? }.
join('-')
RUBY
end
it 'does not register an offense for method call chained without a dot' do
expect_no_offenses(<<~RUBY)
example.select { |item| item.cond? } + 2
RUBY
end
it 'does not register an offense for method call chained on the same line as a multiline block' do
expect_no_offenses(<<~RUBY)
example.select do |item|
item.cond?
end.join('-')
RUBY
end
end