diff options
Diffstat (limited to 'spec/ruby/core/enumerable/chunk_spec.rb')
| -rw-r--r-- | spec/ruby/core/enumerable/chunk_spec.rb | 38 |
1 files changed, 19 insertions, 19 deletions
diff --git a/spec/ruby/core/enumerable/chunk_spec.rb b/spec/ruby/core/enumerable/chunk_spec.rb index 34fa651b33..7c9b31c991 100644 --- a/spec/ruby/core/enumerable/chunk_spec.rb +++ b/spec/ruby/core/enumerable/chunk_spec.rb @@ -6,25 +6,15 @@ describe "Enumerable#chunk" do ScratchPad.record [] end - ruby_version_is ""..."2.4" do - it "raises an ArgumentError if called without a block" do - lambda do - EnumerableSpecs::Numerous.new.chunk - end.should raise_error(ArgumentError) - end - end - - ruby_version_is "2.4" do - it "returns an Enumerator if called without a block" do - chunk = EnumerableSpecs::Numerous.new(1, 2, 3, 1, 2).chunk - chunk.should be_an_instance_of(Enumerator) - result = chunk.with_index {|elt, i| elt - i }.to_a - result.should == [[1, [1, 2, 3]], [-2, [1, 2]]] - end + it "returns an Enumerator if called without a block" do + chunk = EnumerableSpecs::Numerous.new(1, 2, 3, 1, 2).chunk + chunk.should.instance_of?(Enumerator) + result = chunk.with_index {|elt, i| elt - i }.to_a + result.should == [[1, [1, 2, 3]], [-2, [1, 2]]] end it "returns an Enumerator if given a block" do - EnumerableSpecs::Numerous.new.chunk {}.should be_an_instance_of(Enumerator) + EnumerableSpecs::Numerous.new.chunk {}.should.instance_of?(Enumerator) end it "yields the current element and the current chunk to the block" do @@ -39,12 +29,22 @@ describe "Enumerable#chunk" do result.should == [[1, [1, 2]], [0, [3]], [1, [2]], [0, [3]], [1, [2, 1]]] end + it "returns a partitioned Array of values" do + e = EnumerableSpecs::Numerous.new(1,2,3) + e.chunk { |x| x > 2 }.map(&:last).should == [[1, 2], [3]] + end + it "returns elements for which the block returns :_alone in separate Arrays" do e = EnumerableSpecs::Numerous.new(1, 2, 3, 2, 1) result = e.chunk { |x| x < 2 && :_alone }.to_a result.should == [[:_alone, [1]], [false, [2, 3, 2]], [:_alone, [1]]] end + it "yields Arrays as a single argument to a rest argument" do + e = EnumerableSpecs::Numerous.new([1, 2]) + result = e.chunk { |*x| x.should == [[1,2]] }.to_a + end + it "does not return elements for which the block returns :_separator" do e = EnumerableSpecs::Numerous.new(1, 2, 3, 3, 2, 1) result = e.chunk { |x| x == 2 ? :_separator : 1 }.to_a @@ -59,14 +59,14 @@ describe "Enumerable#chunk" do it "raises a RuntimeError if the block returns a Symbol starting with an underscore other than :_alone or :_separator" do e = EnumerableSpecs::Numerous.new(1, 2, 3, 2, 1) - lambda { e.chunk { |x| :_arbitrary }.to_a }.should raise_error(RuntimeError) + -> { e.chunk { |x| :_arbitrary }.to_a }.should.raise(RuntimeError) end it "does not accept arguments" do e = EnumerableSpecs::Numerous.new(1, 2, 3) - lambda { + -> { e.chunk(1) {} - }.should raise_error(ArgumentError) + }.should.raise(ArgumentError) end it 'returned Enumerator size returns nil' do |
