Skip to content

Commit d62d9eb

Browse files
committed
Add specs for Hash initialization behavior
1 parent aaa7da1 commit d62d9eb

File tree

1 file changed

+84
-0
lines changed

1 file changed

+84
-0
lines changed

spec/concurrent/hash_spec.rb

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,90 @@ module Concurrent
22
RSpec.describe Hash do
33
let!(:hsh) { described_class.new }
44

5+
describe '.[]' do
6+
describe 'when initializing with no arguments' do
7+
it do
8+
expect(described_class[]).to be_empty
9+
end
10+
end
11+
12+
describe 'when initializing with an even number of arguments' do
13+
it 'creates a hash using the odd position arguments as keys and even position arguments as values' do
14+
expect(described_class[:hello, 'hello', :world, 'world']).to eq(hello: 'hello', world: 'world')
15+
end
16+
end
17+
18+
describe 'when initializing with an array of pairs' do
19+
let(:array_of_pairs) { [[:hello, 'hello'], [:world, 'world']] }
20+
21+
it 'creates a hash using each pair as a (key, value) pair' do
22+
expect(described_class[array_of_pairs]).to eq(hello: 'hello', world: 'world')
23+
end
24+
end
25+
26+
describe 'when initializing with another hash as an argument' do
27+
let(:other_hash) { {hello: 'hello', world: 'world'} }
28+
let(:fake_other_hash) { double('Fake hash', to_hash: other_hash) }
29+
30+
it 'creates a new hash' do
31+
expect(described_class[other_hash]).to_not be other_hash
32+
end
33+
34+
it 'creates a hash with the same contents as the other hash' do
35+
expect(described_class[other_hash]).to eq(hello: 'hello', world: 'world')
36+
end
37+
38+
it 'creates a hash with the results of calling #to_hash on the other array' do
39+
expect(described_class[fake_other_hash]).to eq(hello: 'hello', world: 'world')
40+
end
41+
end
42+
end
43+
44+
describe '.new' do
45+
describe 'when initializing with no arguments' do
46+
it do
47+
expect(described_class.new).to be_empty
48+
end
49+
end
50+
51+
describe 'when initialized with a default object' do
52+
let(:default_object) { :ruby }
53+
54+
it 'uses the default object for non-existing keys' do
55+
hash = described_class.new(default_object)
56+
57+
expect(hash[:hello]).to be :ruby
58+
expect(hash[:world]).to be :ruby
59+
end
60+
end
61+
62+
describe 'when initialized with a block' do
63+
it 'calls the block for non-existing keys' do
64+
block_calls = []
65+
66+
hash = described_class.new do |hash_instance, key|
67+
block_calls << [hash_instance, key]
68+
end
69+
70+
hash[:hello]
71+
hash[:world]
72+
73+
expect(block_calls).to eq [[hash, :hello], [hash, :world]]
74+
end
75+
76+
it 'returns the results of calling the block for non-existing key' do
77+
block_results = ['hello', 'world']
78+
79+
hash = described_class.new do
80+
block_results.shift
81+
end
82+
83+
expect(hash[:hello]).to eq 'hello'
84+
expect(hash[:world]).to eq 'world'
85+
end
86+
end
87+
end
88+
589
context 'concurrency' do
690
it do
791
(1..Concurrent::ThreadSafe::Test::THREADS).map do |i|

0 commit comments

Comments
 (0)