/
collection_each_shared.rb
61 lines (50 loc) · 1.16 KB
/
collection_each_shared.rb
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
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