forked from Yelp/yelp-ruby
/
burst_struct_spec.rb
147 lines (119 loc) · 3.68 KB
/
burst_struct_spec.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
require 'yelp/burst_struct'
describe BurstStruct::Burst do
describe '#foo' do
subject(:struct) { BurstStruct::Burst.new(foo: 'bar') }
context 'when a key exists' do
it 'should return' do
expect(struct.foo).to eql 'bar'
end
it { should have_key(:foo) }
it { should have_key('foo') }
it { should respond_to(:foo) }
end
context 'when a key does not exist' do
it 'should not respond to it' do
expect(struct.respond_to? :super_foo).to eql false
end
it { should_not have_key(:super_foo) }
it { should_not have_key('super_foo') }
end
end
context 'n deep nested hash' do
let(:hash) { Hash[ futurama: { characters: { robots: { best: 'bender' } } } ] }
subject(:struct) { BurstStruct::Burst.new(hash) }
describe '#foo#bar#baz#biz' do
it 'should return' do
expect(struct.futurama.characters.robots.best).to eql 'bender'
end
end
end
context 'arrays' do
let(:hash) { Hash[ businesses: [ { name: 'Yelp', location: 'San Francisco' },
{ name: 'Ice Cream', flavor: 'Chocolate' },
[ { name: 'Moe', occupation: 'Bartender' } ] ] ] }
subject(:struct) { BurstStruct::Burst.new(hash) }
describe '#businesses[0].name' do
it 'should return' do
expect(struct.businesses[0].name).to eql 'Yelp'
expect(struct.businesses[0].location).to eql 'San Francisco'
end
end
describe '#business[1].name' do
it 'should return' do
expect(struct.businesses[1].name).to eql 'Ice Cream'
expect(struct.businesses[1].flavor).to eql 'Chocolate'
end
end
describe 'nested arrays' do
it 'should parse arrays all the way down' do
expect(struct.businesses[2][0].name).to eql 'Moe'
end
end
end
context 'large hash' do
let(:hash) do
{ foo: 1,
bar: {
baz: 2,
bender: {
bending: {
rodriguez: true
}
}
},
fry: [
{past: true},
{present: true},
{future: true}
],
turunga: 'leela',
'hubert' => 'farnsworth',
zoidberg: [
'doctor',
'homeowner',
'moviestar'
]
}
end
subject(:struct) { BurstStruct::Burst.new(hash) }
it 'turns top level into a struct' do
expect(struct.foo).to eql 1
end
it 'recursively creates structs all the way down the hash' do
expect(struct.bar.baz).to equal 2
expect(struct.bar.bender.bending.rodriguez).to eql true
end
it 'creates arrays with hashes into new structs' do
expect(struct.fry[0].past).to eql true
expect(struct.fry[1].present).to eql true
expect(struct.fry[2].future).to eql true
end
it 'turns string keys into structs' do
expect(struct.hubert).to eql 'farnsworth'
end
it 'should maintain arrays with non hashes' do
expect(struct.zoidberg.size).to eql 3
expect(struct.zoidberg[0]).to eql 'doctor'
end
end
context 'deserialize' do
let(:hash) do
{ name: 'Bender', company: 'Yelp', title: 'Bending Engineer' }
end
subject(:struct) { BurstStruct::Burst.new(hash) }
it 'should deserialize to the same json' do
expect(struct.to_json).to eql hash.to_json
end
end
context 'struct with string keys' do
subject(:struct) { BurstStruct::Burst.new('foo' => 'bar') }
context 'when a key exists' do
it 'should return' do
expect(struct.foo).to eql 'bar'
end
it { should have_key(:foo) }
it { should have_key('foo') }
it { should respond_to(:foo) }
end
end
end