File: partition_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 (52 lines) | stat: -rw-r--r-- 1,474 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
require 'spec_helper'

describe Immutable::Vector do
  describe '#partition' do
    [
      [[], [], []],
      [[1], [1], []],
      [[1, 2], [1], [2]],
      [[1, 2, 3], [1, 3], [2]],
      [[1, 2, 3, 4], [1, 3], [2, 4]],
      [[2, 3, 4], [3], [2, 4]],
      [[3, 4], [3], [4]],
      [[4], [], [4]],
    ].each do |values, expected_matches, expected_remainder|
      describe "on #{values.inspect}" do
        let(:vector) { V[*values] }

        describe 'with a block' do
          let(:result)    { vector.partition(&:odd?) }
          let(:matches)   { result.first }
          let(:remainder) { result.last }

          it 'preserves the original' do
            result
            vector.should eql(V[*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 matches' do
            matches.should eql(V[*expected_matches])
          end

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

        describe 'without a block' do
          it 'returns an Enumerator' do
            vector.partition.class.should be(Enumerator)
            vector.partition.each(&:odd?).should eql([V.new(expected_matches), V.new(expected_remainder)])
          end
        end
      end
    end
  end
end