File: each_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 (45 lines) | stat: -rw-r--r-- 1,047 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
require 'spec_helper'
require 'set'

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

  describe '#each' do
    let(:each) { set.each(&block) }

    context 'without a block' do
      let(:block) { nil }

      it 'returns an Enumerator' do
        expect(each.class).to be(Enumerator)
        expect(each.to_a).to eq(set.to_a)
      end
    end

    context 'with an empty block' do
      let(:block) { ->(item) {} }

      it 'returns self' do
        expect(each).to be(set)
      end
    end

    context 'with a block' do
      let(:items)  { ::Set.new }
      let(:values) { ::Set.new(%w[A B C]) }
      let(:block)  { ->(item) { items << item } }
      before(:each) { each }

      it 'yields all values' do
        expect(items).to eq(values)
      end
    end

    it 'yields both of a pair of colliding keys' do
      set = S[DeterministicHash.new('a', 1010), DeterministicHash.new('b', 1010)]
      yielded = []
      set.each { |obj| yielded << obj }
      yielded.map(&:value).sort.should == ['a', 'b']
    end
  end
end