From a9770bac6375c9d4ff8ba8bb1842841aec7c59e3 Mon Sep 17 00:00:00 2001 From: marcandre Date: Sun, 10 Dec 2017 22:36:28 +0000 Subject: Add case equality arity to Enumerable#all?, any?, none? and one?, and specialized Array#any? and Hash#any? Based on patch by D.E. Akers [#11286] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@61098 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- spec/ruby/core/enumerable/any_spec.rb | 96 ++++++++++++++++++++++++++++++++--- 1 file changed, 90 insertions(+), 6 deletions(-) (limited to 'spec') diff --git a/spec/ruby/core/enumerable/any_spec.rb b/spec/ruby/core/enumerable/any_spec.rb index 4a7511f649..edf7e36519 100644 --- a/spec/ruby/core/enumerable/any_spec.rb +++ b/spec/ruby/core/enumerable/any_spec.rb @@ -20,12 +20,19 @@ describe "Enumerable#any?" do {}.any? { nil }.should == false end - it "raises an ArgumentError when any arguments provided" do - lambda { @enum.any?(Proc.new {}) }.should raise_error(ArgumentError) - lambda { @enum.any?(nil) }.should raise_error(ArgumentError) - lambda { @empty.any?(1) }.should raise_error(ArgumentError) - lambda { @enum1.any?(1) {} }.should raise_error(ArgumentError) - lambda { @enum2.any?(1, 2, 3) {} }.should raise_error(ArgumentError) + it "raises an ArgumentError when more than 1 argument is provided" do + lambda { @enum.any?(1, 2, 3) }.should raise_error(ArgumentError) + lambda { [].any?(1, 2, 3) }.should raise_error(ArgumentError) + lambda { {}.any?(1, 2, 3) }.should raise_error(ArgumentError) + end + + ruby_version_is ""..."2.5" do + it "raises an ArgumentError when any arguments provided" do + lambda { @enum.any?(Proc.new {}) }.should raise_error(ArgumentError) + lambda { @enum.any?(nil) }.should raise_error(ArgumentError) + lambda { @empty.any?(1) }.should raise_error(ArgumentError) + lambda { @enum1.any?(1) {} }.should raise_error(ArgumentError) + end end it "does not hide exceptions out of #each" do @@ -138,4 +145,81 @@ describe "Enumerable#any?" do end end + + ruby_version_is "2.5" do + describe 'when given a pattern argument' do + class EnumerableSpecs::Pattern + attr_reader :yielded + def initialize(&block) + @block = block + @yielded = [] + end + def ===(*args) + @yielded << args + @block.call(*args) + end + end + + it "calls `===` on the pattern the return value " do + pattern = EnumerableSpecs::Pattern.new { |x| x == 2 } + @enum1.any?(pattern).should == true + pattern.yielded.should == [[0], [1], [2]] + end + + it "ignores block" do + @enum2.any?(NilClass) { raise }.should == true + [1, 2, nil].any?(NilClass) { raise }.should == true + {a: 1}.any?(Array) { raise }.should == true + end + + it "always returns false on empty enumeration" do + @empty.any?(Integer).should == false + [].any?(Integer).should == false + {}.any?(NilClass).should == false + end + + it "does not hide exceptions out of #each" do + lambda { + EnumerableSpecs::ThrowingEach.new.any?(Integer) + }.should raise_error(RuntimeError) + end + + it "returns true if the pattern ever returns a truthy value" do + @enum2.any?(NilClass).should == true + pattern = EnumerableSpecs::Pattern.new { |x| 42 } + @enum.any?(pattern).should == true + + [1, 42, 3].any?(pattern).should == true + + pattern = EnumerableSpecs::Pattern.new { |x| x == [:b, 2] } + {a: 1, b: 2}.any?(pattern).should == true + end + + it "any? should return false if the block never returns other than false or nil" do + pattern = EnumerableSpecs::Pattern.new { |x| nil } + @enum1.any?(pattern).should == false + pattern.yielded.should == [[0], [1], [2], [-1]] + + [1, 2, 3].any?(pattern).should == false + {a: 1}.any?(pattern).should == false + end + + it "does not hide exceptions out of the block" do + pattern = EnumerableSpecs::Pattern.new { raise "from pattern" } + lambda { + @enum.any?(pattern) + }.should raise_error(RuntimeError) + end + + it "calls the pattern with gathered array when yielded with multiple arguments" do + pattern = EnumerableSpecs::Pattern.new { false } + EnumerableSpecs::YieldsMixed2.new.any?(pattern).should == false + pattern.yielded.should == EnumerableSpecs::YieldsMixed2.gathered_yields.map { |x| [x] } + + pattern = EnumerableSpecs::Pattern.new { false } + {a: 1, b: 2}.any?(pattern).should == false + pattern.yielded.should == [[[:a, 1]], [[:b, 2]]] + end + end + end end -- cgit v1.2.3