File: new_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 (70 lines) | stat: -rw-r--r-- 1,967 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
70
require 'spec_helper'

describe Immutable::Hash do
  describe '.new' do
    it 'is amenable to overriding of #initialize' do
      class SnazzyHash < Immutable::Hash
        def initialize
          super({'snazzy?' => 'oh yeah'})
        end
      end

      SnazzyHash.new['snazzy?'].should == 'oh yeah'
    end

    context 'from a subclass' do
      it 'returns a frozen instance of the subclass' do
        subclass = Class.new(Immutable::Hash)
        instance = subclass.new('some' => 'values')
        instance.class.should be(subclass)
        instance.frozen?.should be true
      end
    end

    it 'accepts an array as initializer' do
      H.new([['a', 'b'], ['c', 'd']]).should eql(H['a' => 'b', 'c' => 'd'])
    end

    it "returns a Hash which doesn't change even if initializer is mutated" do
      rbhash = {a: 1, b: 2}
      hash = H.new(rbhash)
      rbhash[:a] = 'BAD'
      hash.should eql(H[a: 1, b: 2])
    end
  end

  describe '.[]' do
    it 'accepts a Ruby Hash as initializer' do
      hash = H[a: 1, b: 2]
      hash.class.should be(Immutable::Hash)
      hash.size.should == 2
      hash.key?(:a).should == true
      hash.key?(:b).should == true
    end

    it 'accepts a Immutable::Hash as initializer' do
      hash = H[H.new(a: 1, b: 2)]
      hash.class.should be(Immutable::Hash)
      hash.size.should == 2
      hash.key?(:a).should == true
      hash.key?(:b).should == true
    end

    it 'accepts an array as initializer' do
      hash = H[[[:a, 1], [:b, 2]]]
      hash.class.should be(Immutable::Hash)
      hash.size.should == 2
      hash.key?(:a).should == true
      hash.key?(:b).should == true
    end

    it 'can be used with a subclass of Immutable::Hash' do
      subclass = Class.new(Immutable::Hash)
      instance = subclass[a: 1, b: 2]
      instance.class.should be(subclass)
      instance.size.should == 2
      instance.key?(:a).should == true
      instance.key?(:b).should == true
    end
  end
end