Skip to content

Commit bcea091

Browse files
committed
Fix Array and Enumerable methods to warn when passed block argument not used
1 parent 87eea20 commit bcea091

File tree

15 files changed

+95
-11
lines changed

15 files changed

+95
-11
lines changed

spec/ruby/core/array/all_spec.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,10 @@
44
describe "Array#all?" do
55
@value_to_return = -> (_) { true }
66
it_behaves_like :array_iterable_and_tolerating_size_increasing, :all?
7+
8+
it "ignores the block if there is an argument" do
9+
-> {
10+
['bar', 'foobar'].all?(/bar/) { false }.should == true
11+
}.should complain(/given block not used/)
12+
end
713
end

spec/ruby/core/array/any_spec.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,4 +38,12 @@
3838
array_with_members.any? {|v| v == 42 }.should == false
3939
end
4040
end
41+
42+
describe 'when given a pattern argument' do
43+
it "ignores the block if there is an argument" do
44+
-> {
45+
['bar', 'foobar'].any?(/bar/) { false }.should == true
46+
}.should complain(/given block not used/)
47+
end
48+
end
4149
end

spec/ruby/core/array/count_spec.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,12 @@
1414
[:a, :b, :c].count { |s| s != :b }.should == 2
1515
end
1616

17+
it "ignores the block if there is an argument" do
18+
-> {
19+
[:a, :b, :b, :c].count(:b) { |e| e.size > 10 }.should == 2
20+
}.should complain(/given block not used/)
21+
end
22+
1723
context "when a block argument given" do
1824
it_behaves_like :array_iterable_and_tolerating_size_increasing, :count
1925
end

spec/ruby/core/array/initialize_spec.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,9 @@
5353
end
5454

5555
it "does not use the given block" do
56-
->{ [1, 2, 3].send(:initialize) { raise } }.should_not raise_error
56+
-> {
57+
-> { [1, 2, 3].send(:initialize) { raise } }.should_not raise_error
58+
}.should complain(/#{__FILE__}:#{__LINE__-1}: warning: given block not used/, verbose: true)
5759
end
5860
end
5961

spec/ruby/core/array/new_spec.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,9 @@
2626
end
2727

2828
it "does not use the given block" do
29-
->{ Array.new { raise } }.should_not raise_error
29+
-> {
30+
-> { Array.new { raise } }.should_not raise_error
31+
}.should complain(/warning: given block not used/, verbose: true)
3032
end
3133
end
3234

spec/ruby/core/array/none_spec.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,10 @@
44
describe "Array#none?" do
55
@value_to_return = -> (_) { false }
66
it_behaves_like :array_iterable_and_tolerating_size_increasing, :none?
7+
8+
it "ignores the block if there is an argument" do
9+
-> {
10+
['bar', 'foobar'].none?(/baz/) { true }.should == true
11+
}.should complain(/given block not used/)
12+
end
713
end

spec/ruby/core/array/one_spec.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,10 @@
44
describe "Array#one?" do
55
@value_to_return = -> (_) { false }
66
it_behaves_like :array_iterable_and_tolerating_size_increasing, :one?
7+
8+
it "ignores the block if there is an argument" do
9+
-> {
10+
['bar', 'foobar'].one?(/foo/) { false }.should == true
11+
}.should complain(/given block not used/)
12+
end
713
end

spec/ruby/core/enumerable/all_spec.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,5 +177,11 @@
177177
multi.all?(pattern).should == true
178178
pattern.yielded.should == [[[1, 2]], [[3, 4, 5]], [[6, 7, 8, 9]]]
179179
end
180+
181+
it "ignores the block if there is an argument" do
182+
-> {
183+
EnumerableSpecs::Numerous.new(1, 2, 3, 4, 5).all?(String) { true }.should == false
184+
}.should complain(/given block not used/)
185+
end
180186
end
181187
end

spec/ruby/core/enumerable/any_spec.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,5 +190,11 @@
190190
multi.any?(pattern).should == false
191191
pattern.yielded.should == [[[1, 2]], [[3, 4, 5]], [[6, 7, 8, 9]]]
192192
end
193+
194+
it "ignores the block if there is an argument" do
195+
-> {
196+
EnumerableSpecs::Numerous.new(1, 2, 3, 4, 5).any?(String) { true }.should == false
197+
}.should complain(/given block not used/)
198+
end
193199
end
194200
end

spec/ruby/core/enumerable/none_spec.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,5 +143,11 @@
143143
multi.none?(pattern).should == true
144144
pattern.yielded.should == [[[1, 2]], [[3, 4, 5]], [[6, 7, 8, 9]]]
145145
end
146+
147+
it "ignores the block if there is an argument" do
148+
-> {
149+
EnumerableSpecs::Numerous.new(1, 2, 3, 4, 5).none?(String) { true }.should == true
150+
}.should complain(/given block not used/)
151+
end
146152
end
147153
end

0 commit comments

Comments
 (0)