File: slice_when_spec.rb

package info (click to toggle)
ruby3.3 3.3.8-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 153,620 kB
  • sloc: ruby: 1,244,308; ansic: 836,474; yacc: 28,074; pascal: 6,748; sh: 3,913; python: 1,719; cpp: 1,158; makefile: 742; asm: 712; javascript: 394; lisp: 97; perl: 62; awk: 36; sed: 23; xml: 4
file content (54 lines) | stat: -rw-r--r-- 1,652 bytes parent folder | download | duplicates (7)
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
43
44
45
46
47
48
49
50
51
52
53
54
require_relative '../../spec_helper'
require_relative 'fixtures/classes'

describe "Enumerable#slice_when" do
  before :each do
    ary = [10, 9, 7, 6, 4, 3, 2, 1]
    @enum = EnumerableSpecs::Numerous.new(*ary)
    @result = @enum.slice_when { |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 true" 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.slice_when do |i, j|
        times_called += 1
        i - 1 != j
      end.to_a
      times_called.should == (@enum_length - 1)
    end

    it "doesn't yield an empty array if the block matches the first or the last time" do
      @enum.slice_when { true }.to_a.should == [[10], [9], [7], [6], [4], [3], [2], [1]]
    end

    it "doesn't yield an empty array on a small enumerable" do
      EnumerableSpecs::Empty.new.slice_when { raise }.to_a.should == []
      EnumerableSpecs::Numerous.new(42).slice_when { raise }.to_a.should == [[42]]
    end
  end

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

  describe "when an iterator method yields more than one value" do
    it "processes all yielded values" do
      def foo
        yield 1, 2
      end
      to_enum(:foo).slice_when { true }.to_a.should == [[[1, 2]]]
    end
  end
end