-
Notifications
You must be signed in to change notification settings - Fork 3
/
nest_spec.rb
145 lines (102 loc) · 2.98 KB
/
nest_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
require 'nuggets/hash/nest'
describe_extended Hash, Nuggets::Hash::NestMixin, true do
example do
hash = Hash.nest
hash.should be_an_instance_of(Hash)
hash[:a].should be_nil
hash.should have_key(:a)
end
example do
hash = Hash.nest(0, 1)
hash.should be_an_instance_of(Hash)
hash[:a].should == 1
hash.should have_key(:a)
end
example do
hash = Hash.nest(1)
hash.should be_an_instance_of(Hash)
hash[:a].should be_an_instance_of(Hash)
hash.should have_key(:a)
hash[:a][:b].should be_nil
hash[:a].should have_key(:b)
end
example do
hash = Hash.nest(1, 1)
hash.should be_an_instance_of(Hash)
hash[:a].should be_an_instance_of(Hash)
hash.should have_key(:a)
hash[:a][:b].should == 1
hash[:a].should have_key(:b)
end
example do
hash = Hash.nest(1, [])
hash.should be_an_instance_of(Hash)
hash[:a].should be_an_instance_of(Hash)
hash.should have_key(:a)
hash[:a][:b].should == []
hash[:a].should have_key(:b)
hash[:a][:b] << 1
hash[:a][:b].should == [1]
hash[:a][:c] << 2
hash[:a][:c].should == [1, 2]
hash[:a][:b].should == [1, 2]
end
example do
hash = Hash.nest(1) { [] }
hash.should be_an_instance_of(Hash)
hash[:a].should be_an_instance_of(Hash)
hash.should have_key(:a)
hash[:a][:b].should == []
hash[:a].should have_key(:b)
hash[:a][:b] << 1
hash[:a][:b].should == [1]
hash[:a][:c] << 2
hash[:a][:c].should == [2]
hash[:a][:b].should == [1]
end
example do
hash = Hash.nest(3)
hash.should be_an_instance_of(Hash)
hash[:a].should be_an_instance_of(Hash)
hash.should have_key(:a)
hash[:a][:b].should be_an_instance_of(Hash)
hash[:a].should have_key(:b)
hash[:a][:b][:c].should be_an_instance_of(Hash)
hash[:a][:b].should have_key(:c)
hash[:a][:b][:c][:d].should be_nil
hash[:a][:b][:c].should have_key(:d)
hash[:a][:b][:c][:e] = 1
hash[:a][:b][:c][:e].should == 1
hash[:a][:b][:c].should have_key(:e)
end
example do
hash = Hash.identity
hash.should be_an_instance_of(Hash)
hash[:a].should == :a
hash[:b].should == :b
hash.should == { a: :a, b: :b }
end
example do
hash = Hash.identity(2)
hash.should be_an_instance_of(Hash)
hash[:foo][:bar][:a].should == :a
hash[:foo][:bar][:b].should == :b
hash.should == { foo: { bar: { a: :a, b: :b } } }
end
example do
hash = Hash.array
hash.should be_an_instance_of(Hash)
hash[:a].should be_an_instance_of(Array)
hash[:a].push(1).push(2).should == [1, 2]
hash[:b].should be_an_instance_of(Array)
hash[:b].push(3).push(4).should == [3, 4]
hash.should == { a: [1, 2], b: [3, 4] }
end
example do
hash = Hash.array(2)
hash.should be_an_instance_of(Hash)
hash[:foo][:bar][:a].push(1).push(2).should == [1, 2]
hash[:foo][:bar][:b].push(3).push(4).should == [3, 4]
hash.should == { foo: { bar: { a: [1, 2], b: [3, 4] } } }
end
end