File: invert_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 (30 lines) | stat: -rw-r--r-- 819 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
require 'spec_helper'

describe Immutable::Hash do
  describe '#invert' do
    let(:hash) { H[a: 3, b: 2, c: 1] }

    it 'uses the existing keys as values and values as keys' do
      hash.invert.should eql(H[3 => :a, 2 => :b, 1 => :c])
    end

    it 'will select one key/value pair among multiple which have same value' do
      [H[1 => :a],
       H[1 => :b],
       H[1 => :c]].include?(H[a: 1, b: 1, c: 1].invert).should == true
    end

    it "doesn't change the original Hash" do
      hash.invert
      hash.should eql(H[a: 3, b: 2, c: 1])
    end

    context 'from a subclass of Hash' do
      it 'returns an instance of the subclass' do
        subclass = Class.new(Immutable::Hash)
        instance = subclass.new(a: 1, b: 2)
        instance.invert.class.should be(subclass)
      end
    end
  end
end