summaryrefslogtreecommitdiff
path: root/spec/ruby/core/enumerable/chunk_while_spec.rb
blob: 88d6d2983fcbc00df774b9c4d67c271733f237e4 (plain)
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
require_relative '../../spec_helper'
require_relative 'fixtures/classes'

describe "Enumerable#chunk_while" do
  before :each do
    ary = [10, 9, 7, 6, 4, 3, 2, 1]
    @enum = EnumerableSpecs::Numerous.new(*ary)
    @result = @enum.chunk_while { |i, j| i - 1 == j }
    @enum_length = ary.length
  end

  context "when given a block" do
    it "returns an enumerator" do
      @result.should be_an_instance_of(Enumerator)
    end

    it "splits chunks between adjacent elements i and j where the block returns false" do
      @result.to_a.should == [[10, 9], [7, 6], [4, 3, 2, 1]]
    end

    it "calls the block for length of the receiver enumerable minus one times" do
      times_called = 0
      @enum.chunk_while do |i, j|
        times_called += 1
        i - 1 == j
      end.to_a
      times_called.should == (@enum_length - 1)
    end
  end

  context "when not given a block" do
    it "raises an ArgumentError" do
      lambda { @enum.chunk_while }.should raise_error(ArgumentError)
    end
  end

  context "on a single-element array" do
    it "ignores the block and returns an enumerator that yields [element]" do
      [1].chunk_while {|x| x.even?}.to_a.should == [[1]]
    end
  end
end