File: drop_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 (55 lines) | stat: -rw-r--r-- 1,495 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
require 'spec_helper'

describe Immutable::SortedSet do
  describe '#drop' do
    [
      [[], 0, []],
      [[], 10, []],
      [['A'], 10, []],
      [%w[A B C], 0, %w[A B C]],
      [%w[A B C], 1, %w[B C]],
      [%w[A B C], 2, ['C']],
      [%w[A B C], 3, []]
    ].each do |values, number, expected|
      context "#{number} from #{values.inspect}" do
        let(:sorted_set) { SS[*values] }

        it 'preserves the original' do
          sorted_set.drop(number)
          sorted_set.should eql(SS[*values])
        end

        it "returns #{expected.inspect}" do
          sorted_set.drop(number).should eql(SS[*expected])
        end
      end
    end

    context 'when argument is zero' do
      let(:sorted_set) { SS[6, 7, 8, 9] }
      it 'returns self' do
        sorted_set.drop(0).should be(sorted_set)
      end
    end

    context 'when the set has a custom order' do
      let(:sorted_set) { SS.new([1, 2, 3], &:-@)}
      it 'maintains the custom order' do
        sorted_set.drop(1).to_a.should == [2, 1]
        sorted_set.drop(2).to_a.should == [1]
      end

      it 'keeps the comparator even when set is cleared' do
        s = sorted_set.drop(3)
        s.add(4).add(5).add(6).to_a.should == [6, 5, 4]
      end
    end

    context 'when called on a subclass' do
      it 'should return an instance of the subclass' do
        subclass = Class.new(Immutable::SortedSet)
        subclass.new([1,2,3]).drop(1).class.should be(subclass)
      end
    end
  end
end