File: add_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 (62 lines) | stat: -rw-r--r-- 1,712 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
require 'spec_helper'

describe Immutable::SortedSet do
  let(:sorted_set) { SS['B', 'C', 'D'] }

  [:add, :<<].each do |method|
    describe "##{method}" do
      context 'with a unique value' do
        it 'preserves the original' do
          sorted_set.send(method, 'A')
          sorted_set.should eql(SS['B', 'C', 'D'])
        end

        it 'returns a copy with the superset of values (in order)' do
          sorted_set.send(method, 'A').should eql(SS['A', 'B', 'C', 'D'])
        end
      end

      context 'with a duplicate value' do
        it 'preserves the original values' do
          sorted_set.send(method, 'C')
          sorted_set.should eql(SS['B', 'C', 'D'])
        end

        it 'returns self' do
          sorted_set.send(method, 'C').should equal(sorted_set)
        end
      end

      context 'on a set ordered by a comparator' do
        it 'inserts the new item in the correct place' do
          s = SS.new(['tick', 'pig', 'hippopotamus'], &:length)
          s.add('giraffe').to_a.should == ['pig', 'tick', 'giraffe', 'hippopotamus']
        end
      end
    end
  end

  describe '#add?' do
    context 'with a unique value' do
      it 'preserves the original' do
        sorted_set.add?('A')
        sorted_set.should eql(SS['B', 'C', 'D'])
      end

      it 'returns a copy with the superset of values' do
        sorted_set.add?('A').should eql(SS['A', 'B', 'C', 'D'])
      end
    end

    context 'with a duplicate value' do
      it 'preserves the original values' do
        sorted_set.add?('C')
        sorted_set.should eql(SS['B', 'C', 'D'])
      end

      it 'returns false' do
        sorted_set.add?('C').should equal(false)
      end
    end
  end
end