File: collection_each_shared.rb

package info (click to toggle)
ruby-concurrent 1.1.6%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 30,284 kB
  • sloc: ruby: 30,875; java: 6,117; ansic: 288; makefile: 9; sh: 6
file content (61 lines) | stat: -rw-r--r-- 1,190 bytes parent folder | download | duplicates (3)
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
RSpec.shared_examples :collection_each do

  it 'common' do
    @cache.send(method) { |k, v| fail }
    expect(@cache).to eq @cache.send(method) {}
    @cache[:a] = 1

    h = {}
    @cache.send(method) { |k, v| h[k] = v }
    expect({:a => 1}).to eq h

    @cache[:b] = 2
    h = {}
    @cache.send(method) { |k, v| h[k] = v }
    expect({:a => 1, :b => 2}).to eq h
  end

  it 'pair iterator' do
    @cache[:a] = 1
    @cache[:b] = 2
    i = 0
    r = @cache.send(method) do |k, v|
      if i == 0
        i += 1
        next
      elsif i == 1
        break :breaked
      end
    end

    expect(:breaked).to eq r
  end

  it 'allows modification' do
    @cache[:a] = 1
    @cache[:b] = 1
    @cache[:c] = 1

    expect_size_change(1) do
      @cache.send(method) do |k, v|
        @cache[:z] = 1
      end
    end
  end

  context 'when no block is given' do
    it 'returns an enumerator' do
      @cache[:a] = 1
      @cache[:b] = 2

      expect(@cache.send(method)).to be_a Enumerator
    end

    it 'returns an object which is enumerable' do
      @cache[:a] = 1
      @cache[:b] = 2

      expect(@cache.send(method).to_a).to contain_exactly([:a, 1], [:b, 2])
    end
  end
end