/
combinable_loops_spec.rb
101 lines (84 loc) · 3.52 KB
/
combinable_loops_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
# frozen_string_literal: true
RSpec.describe RuboCop::Cop::Style::CombinableLoops do
subject(:cop) { described_class.new }
context 'when looping method' do
it 'registers an offense when looping over the same data as previous loop' do
expect_offense(<<~RUBY)
items.each { |item| do_something(item) }
items.each { |item| do_something_else(item, arg) }
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Combine this loop with the previous loop.
items.each_with_index { |item| do_something(item) }
items.each_with_index { |item| do_something_else(item, arg) }
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Combine this loop with the previous loop.
items.reverse_each { |item| do_something(item) }
items.reverse_each { |item| do_something_else(item, arg) }
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Combine this loop with the previous loop.
RUBY
end
it 'does not register an offense when the same loops are interleaved with some code' do
expect_no_offenses(<<~RUBY)
items.each { |item| do_something(item) }
some_code
items.each { |item| do_something_else(item, arg) }
RUBY
end
it 'does not register an offense when the same loop method is used over different collections' do
expect_no_offenses(<<~RUBY)
items.each { |item| do_something(item) }
bars.each { |bar| do_something(bar) }
RUBY
end
it 'does not register an offense when different loop methods are used over the same collection' do
expect_no_offenses(<<~RUBY)
items.reverse_each { |item| do_something(item) }
items.each { |item| do_something(item) }
RUBY
end
it 'does not register an offense when each branch contains the same single loop over the same collection' do
expect_no_offenses(<<~RUBY)
if condition
items.each { |item| do_something(item) }
else
items.each { |item| do_something_else(item, arg) }
end
RUBY
end
it 'does not register an offense for when the same method with different arguments' do
expect_no_offenses(<<~RUBY)
each_slice(2) { |slice| do_something(slice) }
each_slice(3) { |slice| do_something(slice) }
RUBY
end
end
context 'when for loop' do
it 'registers an offense when looping over the same data as previous loop' do
expect_offense(<<~RUBY)
for item in items do do_something(item) end
for item in items do do_something_else(item, arg) end
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Combine this loop with the previous loop.
RUBY
end
it 'does not register an offense when the same loops are interleaved with some code' do
expect_no_offenses(<<~RUBY)
for item in items do do_something(item) end
some_code
for item in items do do_something_else(item, arg) end
RUBY
end
it 'does not register an offense when the same loop method is used over different collections' do
expect_no_offenses(<<~RUBY)
for item in items do do_something(item) end
for foo in foos do do_something(foo) end
RUBY
end
it 'does not register an offense when each branch contains the same single loop over the same collection' do
expect_no_offenses(<<~RUBY)
if condition
for item in items do do_something(item) end
else
for item in items do do_something_else(item, arg) end
end
RUBY
end
end
end