File: delete_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 (71 lines) | stat: -rw-r--r-- 1,837 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
71
require 'spec_helper'

describe Immutable::Set do
  let(:set) { S['A', 'B', 'C'] }

  describe '#delete' do
    context 'with an existing value' do
      it 'preserves the original' do
        set.delete('B')
        set.should eql(S['A', 'B', 'C'])
      end

      it 'returns a copy with the remaining values' do
        set.delete('B').should eql(S['A', 'C'])
      end
    end

    context 'with a non-existing value' do
      it 'preserves the original values' do
        set.delete('D')
        set.should eql(S['A', 'B', 'C'])
      end

      it 'returns self' do
        set.delete('D').should equal(set)
      end
    end

    context 'when removing the last value in a set' do
      it 'returns the canonical empty set' do
        set.delete('B').delete('C').delete('A').should be(Immutable::EmptySet)
      end
    end

    it 'works on large sets, with many combinations of input' do
      array = 1000.times.map { %w[a b c d e f g h i j k l m n].sample(5).join }.uniq
      set = S.new(array)
      array.each do |key|
        result = set.delete(key)
        result.size.should == set.size - 1
        result.include?(key).should == false
        other = array.sample
        (result.include?(other).should == true) if other != key
      end
    end
  end

  describe '#delete?' do
    context 'with an existing value' do
      it 'preserves the original' do
        set.delete?('B')
        set.should eql(S['A', 'B', 'C'])
      end

      it 'returns a copy with the remaining values' do
        set.delete?('B').should eql(S['A', 'C'])
      end
    end

    context 'with a non-existing value' do
      it 'preserves the original values' do
        set.delete?('D')
        set.should eql(S['A', 'B', 'C'])
      end

      it 'returns false' do
        set.delete?('D').should be(false)
      end
    end
  end
end