diff options
Diffstat (limited to 'spec/ruby/core/enumerable/all_spec.rb')
| -rw-r--r-- | spec/ruby/core/enumerable/all_spec.rb | 128 |
1 files changed, 97 insertions, 31 deletions
diff --git a/spec/ruby/core/enumerable/all_spec.rb b/spec/ruby/core/enumerable/all_spec.rb index bfde584260..cbdd63f86a 100644 --- a/spec/ruby/core/enumerable/all_spec.rb +++ b/spec/ruby/core/enumerable/all_spec.rb @@ -1,63 +1,67 @@ -require File.expand_path('../../../spec_helper', __FILE__) -require File.expand_path('../fixtures/classes', __FILE__) +require_relative '../../spec_helper' +require_relative 'fixtures/classes' describe "Enumerable#all?" do - before :each do @enum = EnumerableSpecs::Numerous.new @empty = EnumerableSpecs::Empty.new() - @enum1 = [0, 1, 2, -1] - @enum2 = [nil, false, true] + @enum1 = EnumerableSpecs::Numerous.new(0, 1, 2, -1) + @enum2 = EnumerableSpecs::Numerous.new(nil, false, true) end it "always returns true on empty enumeration" do - @empty.all?.should == true + @empty.should.all? @empty.all? { nil }.should == true - [].all?.should == true + [].should.all? [].all? { false }.should == true - {}.all?.should == true + {}.should.all? {}.all? { nil }.should == true end + it "raises an ArgumentError when more than 1 argument is provided" do + -> { @enum.all?(1, 2, 3) }.should.raise(ArgumentError) + -> { [].all?(1, 2, 3) }.should.raise(ArgumentError) + -> { {}.all?(1, 2, 3) }.should.raise(ArgumentError) + end + it "does not hide exceptions out of #each" do - lambda { + -> { EnumerableSpecs::ThrowingEach.new.all? - }.should raise_error(RuntimeError) + }.should.raise(RuntimeError) - lambda { + -> { EnumerableSpecs::ThrowingEach.new.all? { false } - }.should raise_error(RuntimeError) + }.should.raise(RuntimeError) end describe "with no block" do it "returns true if no elements are false or nil" do - @enum.all?.should == true - @enum1.all?.should == true - @enum2.all?.should == false + @enum.should.all? + @enum1.should.all? + @enum2.should_not.all? - EnumerableSpecs::Numerous.new('a','b','c').all?.should == true - EnumerableSpecs::Numerous.new(0, "x", true).all?.should == true + EnumerableSpecs::Numerous.new('a','b','c').should.all? + EnumerableSpecs::Numerous.new(0, "x", true).should.all? end it "returns false if there are false or nil elements" do - EnumerableSpecs::Numerous.new(false).all?.should == false - EnumerableSpecs::Numerous.new(false, false).all?.should == false + EnumerableSpecs::Numerous.new(false).should_not.all? + EnumerableSpecs::Numerous.new(false, false).should_not.all? - EnumerableSpecs::Numerous.new(nil).all?.should == false - EnumerableSpecs::Numerous.new(nil, nil).all?.should == false + EnumerableSpecs::Numerous.new(nil).should_not.all? + EnumerableSpecs::Numerous.new(nil, nil).should_not.all? - EnumerableSpecs::Numerous.new(1, nil, 2).all?.should == false - EnumerableSpecs::Numerous.new(0, "x", false, true).all?.should == false - @enum2.all?.should == false + EnumerableSpecs::Numerous.new(1, nil, 2).should_not.all? + EnumerableSpecs::Numerous.new(0, "x", false, true).should_not.all? + @enum2.should_not.all? end it "gathers whole arrays as elements when each yields multiple" do multi = EnumerableSpecs::YieldsMultiWithFalse.new - multi.all?.should be_true + multi.all?.should == true end - end describe "with block" do @@ -100,22 +104,84 @@ describe "Enumerable#all?" do end it "does not hide exceptions out of the block" do - lambda { + -> { @enum.all? { raise "from block" } - }.should raise_error(RuntimeError) + }.should.raise(RuntimeError) end it "gathers initial args as elements when each yields multiple" do multi = EnumerableSpecs::YieldsMulti.new - multi.all? {|e| !(Array === e) }.should be_true + yielded = [] + multi.all? { |e| yielded << e }.should == true + yielded.should == [1, 3, 6] end it "yields multiple arguments when each yields multiple" do multi = EnumerableSpecs::YieldsMulti.new yielded = [] - multi.all? {|e, i| yielded << [e, i] } - yielded.should == [[1, 2], [3, 4], [6, 7]] + multi.all? { |*args| yielded << args }.should == true + yielded.should == [[1, 2], [3, 4, 5], [6, 7, 8, 9]] + end + end + + describe 'when given a pattern argument' do + it "calls `===` on the pattern the return value" do + pattern = EnumerableSpecs::Pattern.new { |x| x >= 0 } + @enum1.all?(pattern).should == false + pattern.yielded.should == [[0], [1], [2], [-1]] + end + + it "always returns true on empty enumeration" do + @empty.all?(Integer).should == true + [].all?(Integer).should == true + {}.all?(NilClass).should == true + end + + it "does not hide exceptions out of #each" do + -> { + EnumerableSpecs::ThrowingEach.new.all?(Integer) + }.should.raise(RuntimeError) + end + + it "returns true if the pattern never returns false or nil" do + pattern = EnumerableSpecs::Pattern.new { |x| 42 } + @enum.all?(pattern).should == true + + [1, 42, 3].all?(pattern).should == true + + pattern = EnumerableSpecs::Pattern.new { |x| Array === x } + {a: 1, b: 2}.all?(pattern).should == true + end + + it "returns false if the pattern ever returns false or nil" do + pattern = EnumerableSpecs::Pattern.new { |x| x >= 0 } + @enum1.all?(pattern).should == false + pattern.yielded.should == [[0], [1], [2], [-1]] + + [1, 2, 3, -1].all?(pattern).should == false + + pattern = EnumerableSpecs::Pattern.new { |x| x[1] >= 0 } + {a: 1, b: -1}.all?(pattern).should == false end + it "does not hide exceptions out of pattern#===" do + pattern = EnumerableSpecs::Pattern.new { raise "from pattern" } + -> { + @enum.all?(pattern) + }.should.raise(RuntimeError) + end + + it "calls the pattern with gathered array when yielded with multiple arguments" do + multi = EnumerableSpecs::YieldsMulti.new + pattern = EnumerableSpecs::Pattern.new { true } + multi.all?(pattern).should == true + pattern.yielded.should == [[[1, 2]], [[3, 4, 5]], [[6, 7, 8, 9]]] + end + + it "ignores the block if there is an argument" do + -> { + EnumerableSpecs::Numerous.new(1, 2, 3, 4, 5).all?(String) { true }.should == false + }.should complain(/given block not used/) + end end end |
