File: break_spec.rb

package info (click to toggle)
ruby-immutable-ruby 0.1.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,852 kB
  • sloc: ruby: 16,556; makefile: 4
file content (69 lines) | stat: -rw-r--r-- 1,858 bytes parent folder | download
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
65
66
67
68
69
require 'spec_helper'

describe Immutable::List do
  describe '#break' do
    it 'is lazy' do
      -> { Immutable.stream { fail }.break { |item| false } }.should_not raise_error
    end

    [
      [[], [], []],
      [[1], [1], []],
      [[1, 2], [1, 2], []],
      [[1, 2, 3], [1, 2], [3]],
      [[1, 2, 3, 4], [1, 2], [3, 4]],
      [[2, 3, 4], [2], [3, 4]],
      [[3, 4], [], [3, 4]],
      [[4], [], [4]],
    ].each do |values, expected_prefix, expected_remainder|
      context "on #{values.inspect}" do
        let(:list) { L[*values] }

        context 'with a block' do
          let(:result) { list.break { |item| item > 2 }}
          let(:prefix) { result.first }
          let(:remainder) { result.last }

          it 'preserves the original' do
            result
            list.should eql(L[*values])
          end

          it 'returns a frozen array with two items' do
            result.class.should be(Array)
            result.should be_frozen
            result.size.should be(2)
          end

          it 'correctly identifies the prefix' do
            prefix.should eql(L[*expected_prefix])
          end

          it 'correctly identifies the remainder' do
            remainder.should eql(L[*expected_remainder])
          end
        end

        context 'without a block' do
          let(:result) { list.break }
          let(:prefix) { result.first }
          let(:remainder) { result.last }

          it 'returns a frozen array with two items' do
            result.class.should be(Array)
            result.should be_frozen
            result.size.should be(2)
          end

          it 'returns self as the prefix' do
            prefix.should equal(list)
          end

          it 'leaves the remainder empty' do
            remainder.should be_empty
          end
        end
      end
    end
  end
end