File: except_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 (42 lines) | stat: -rw-r--r-- 1,493 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
require 'spec_helper'

describe Immutable::Hash do
  describe '#except' do
    let(:hash) { H['A' => 'aye', 'B' => 'bee', 'C' => 'see', nil => 'NIL'] }

    context 'with only keys that the Hash has' do
      it 'returns a Hash without those values' do
        hash.except('B', nil).should eql(H['A' => 'aye', 'C' => 'see'])
      end

      it "doesn't change the original Hash" do
        hash.except('B', nil)
        hash.should eql(H['A' => 'aye', 'B' => 'bee', 'C' => 'see', nil => 'NIL'])
      end
    end

    context "with keys that the Hash doesn't have" do
      it 'returns a Hash without the values that it had keys for' do
        hash.except('B', 'A', 3).should eql(H['C' => 'see', nil => 'NIL'])
      end

      it "doesn't change the original Hash" do
        hash.except('B', 'A', 3)
        hash.should eql(H['A' => 'aye', 'B' => 'bee', 'C' => 'see', nil => 'NIL'])
      end
    end

    it 'works on a large Hash, with many combinations of input' do
      keys = (1..1000).to_a
      original = H.new(keys.zip(2..1001))
      100.times do
        to_remove = rand(100).times.collect { keys.sample }
        result    = original.except(*to_remove)
        result.size.should == original.size - to_remove.uniq.size
        to_remove.each { |key| result.key?(key).should == false }
        (keys.sample(100) - to_remove).each { |key| result.key?(key).should == true }
      end
      original.should eql(H.new(keys.zip(2..1001))) # shouldn't have changed
    end
  end
end