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 (35 lines) | stat: -rw-r--r-- 1,110 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
require 'spec_helper'

describe Immutable::Hash do
  let(:hash) { H['a' => 1, 'b' => 2, 'c' => 3, 'd' => 4] }
  let(:partition) { hash.partition { |k,v| v % 2 == 0 }}

  describe '#partition' do
    it 'returns a pair of Immutable::Hashes' do
      partition.each { |h| h.class.should be(Immutable::Hash) }
      partition.should be_frozen
    end

    it 'returns key/val pairs for which predicate is true in first Hash' do
      partition[0].should == {'b' => 2, 'd' => 4}
    end

    it 'returns key/val pairs for which predicate is false in second Hash' do
      partition[1].should == {'a' => 1, 'c' => 3}
    end

    it "doesn't modify the original Hash" do
      partition
      hash.should eql(H['a' => 1, 'b' => 2, 'c' => 3, 'd' => 4])
    end

    context 'from a subclass' do
      it 'should return instances of the subclass' do
        subclass  = Class.new(Immutable::Hash)
        instance  = subclass.new('a' => 1, 'b' => 2, 'c' => 3, 'd' => 4)
        partition = instance.partition { |k,v| v % 2 == 0 }
        partition.each { |h| h.class.should be(subclass) }
      end
    end
  end
end