File: slice_before_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 (64 lines) | stat: -rw-r--r-- 2,198 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
55
56
57
58
59
60
61
62
63
64
require_relative '../../spec_helper'
require_relative 'fixtures/classes'
require_relative 'shared/enumerable_enumeratorized'

describe "Enumerable#slice_before" do
  before :each do
    @enum = EnumerableSpecs::Numerous.new(7,6,5,4,3,2,1)
  end

  describe "when given an argument and no block" do
    it "calls === on the argument to determine when to yield" do
      arg = mock "filter"
      arg.should_receive(:===).and_return(false, true, false, false, false, true, false)
      e = @enum.slice_before(arg)
      e.should be_an_instance_of(Enumerator)
      e.to_a.should == [[7], [6, 5, 4, 3], [2, 1]]
    end

    it "doesn't yield an empty array if the filter matches the first entry or the last entry" do
      arg = mock "filter"
      arg.should_receive(:===).and_return(true).exactly(7)
      e = @enum.slice_before(arg)
      e.to_a.should == [[7], [6], [5], [4], [3], [2], [1]]
    end

    it "uses standard boolean as a test" do
      arg = mock "filter"
      arg.should_receive(:===).and_return(false, :foo, nil, false, false, 42, false)
      e = @enum.slice_before(arg)
      e.to_a.should == [[7], [6, 5, 4, 3], [2, 1]]
    end
  end

  describe "when given a block" do
    describe "and no argument" do
      it "calls the block to determine when to yield" do
        e = @enum.slice_before{|i| i == 6 || i == 2}
        e.should be_an_instance_of(Enumerator)
        e.to_a.should == [[7], [6, 5, 4, 3], [2, 1]]
      end
    end

    it "does not accept arguments" do
      -> {
        @enum.slice_before(1) {}
      }.should raise_error(ArgumentError)
    end
  end

  it "raises an ArgumentError when given an incorrect number of arguments" do
    -> { @enum.slice_before("one", "two") }.should raise_error(ArgumentError)
    -> { @enum.slice_before }.should raise_error(ArgumentError)
  end

  describe "when an iterator method yields more than one value" do
    it "processes all yielded values" do
      enum = EnumerableSpecs::YieldsMulti.new
      result = enum.slice_before { |i| i == [3, 4, 5] }.to_a
      result.should == [[[1, 2]], [[3, 4, 5], [6, 7, 8, 9]]]
    end
  end

  it_behaves_like :enumerable_enumeratorized_with_unknown_size, [:slice_before, 3]
end