forked from rubocop/rubocop
/
ambiguous_block_association_spec.rb
103 lines (92 loc) · 4.31 KB
/
ambiguous_block_association_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
# frozen_string_literal: true
RSpec.describe RuboCop::Cop::Lint::AmbiguousBlockAssociation, :config do
shared_examples 'accepts' do |code|
it 'does not register an offense' do
expect_no_offenses(code)
end
end
it_behaves_like 'accepts', 'foo == bar { baz a }'
it_behaves_like 'accepts', 'foo ->(a) { bar a }'
it_behaves_like 'accepts', 'some_method(a) { |el| puts el }'
it_behaves_like 'accepts', 'some_method(a) do;puts a;end'
it_behaves_like 'accepts', 'some_method a do;puts "dev";end'
it_behaves_like 'accepts', 'some_method a do |e|;puts e;end'
it_behaves_like 'accepts', 'Foo.bar(a) { |el| puts el }'
it_behaves_like 'accepts', 'env ENV.fetch("ENV") { "dev" }'
it_behaves_like 'accepts', 'env(ENV.fetch("ENV") { "dev" })'
it_behaves_like 'accepts', '{ f: "b"}.fetch(:a) do |e|;puts e;end'
it_behaves_like 'accepts', 'Hash[some_method(a) { |el| el }]'
it_behaves_like 'accepts', 'foo = lambda do |diagnostic|;end'
it_behaves_like 'accepts', 'Proc.new { puts "proc" }'
it_behaves_like 'accepts', 'expect { order.save }.to(change { orders.size })'
it_behaves_like 'accepts', 'scope :active, -> { where(status: "active") }'
it_behaves_like('accepts', 'assert_equal posts.find { |p| p.title == "Foo" }, results.first')
it_behaves_like('accepts', 'assert_equal(posts.find { |p| p.title == "Foo" }, results.first)')
it_behaves_like('accepts', 'assert_equal(results.first, posts.find { |p| p.title == "Foo" })')
it_behaves_like('accepts', 'allow(cop).to receive(:on_int) { raise RuntimeError }')
it_behaves_like('accepts', 'allow(cop).to(receive(:on_int) { raise RuntimeError })')
context 'without parentheses' do
context 'without receiver' do
it 'registers an offense' do
expect_offense(<<~RUBY)
some_method a { |el| puts el }
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Parenthesize the param `a { |el| puts el }` to make sure that the block will be associated with the `a` method call.
RUBY
end
end
context 'with receiver' do
it 'registers an offense' do
expect_offense(<<~RUBY)
Foo.some_method a { |el| puts el }
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Parenthesize the param `a { |el| puts el }` to make sure that the block will be associated with the `a` method call.
RUBY
end
context 'when using safe navigation operator' do
it 'registers an offense' do
expect_offense(<<~RUBY)
Foo&.some_method a { |el| puts el }
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Parenthesize the param `a { |el| puts el }` to make sure that the block will be associated with the `a` method call.
RUBY
end
end
end
context 'rspec expect {}.to change {}' do
it 'registers an offense' do
expect_offense(<<~RUBY)
expect { order.expire }.to change { order.events }
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Parenthesize the param `change { order.events }` to make sure that the block will be associated with the `change` method call.
RUBY
end
end
context 'as a hash key' do
it 'registers an offense' do
expect_offense(<<~RUBY)
Hash[some_method a { |el| el }]
^^^^^^^^^^^^^^^^^^^^^^^^^ Parenthesize the param `a { |el| el }` to make sure that the block will be associated with the `a` method call.
RUBY
end
end
context 'with assignment' do
it 'registers an offense' do
expect_offense(<<~RUBY)
foo = some_method a { |el| puts el }
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Parenthesize the param `a { |el| puts el }` to make sure that the block will be associated with the `a` method call.
RUBY
end
end
end
context 'IgnoredMethods' do
let(:cop_config) { { 'IgnoredMethods' => %w[change] } }
it 'does not register an offense for an ignored method' do
expect_no_offenses(<<~RUBY)
expect { order.expire }.to change { order.events }
RUBY
end
it 'registers an offense for other methods' do
expect_offense(<<~RUBY)
expect { order.expire }.to update { order.events }
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Parenthesize the param `update { order.events }` to make sure that the block will be associated with the `update` method call.
RUBY
end
end
end