-
Notifications
You must be signed in to change notification settings - Fork 0
/
griddable_hash_spec.rb
123 lines (97 loc) · 2.9 KB
/
griddable_hash_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
# griddable_spec.rb
require_relative 'griddable_hash'
require 'ruby-debug'
describe GriddableHash, '#max_depth' do
it 'returns 1 for unnnested hash' do
unnest = GriddableHash[{:a => 1, :b => 2, :c => 3}];
unnest.max_depth.should eq(1)
end
it 'returns 2 for single nest' do
nest2 = GriddableHash[{:a => 1, :contents => [{:a1 => 1, :a2 => 2}], :b => 4}];
nest2.max_depth.should eq(2)
end
it 'returns 4 for 4 deep' do
nest3 = GriddableHash[{:a => 1, :contents => [ {:a1 => 11,
:contents => [ {:a1a => 111, :contents => [{:a1a1 => 1111, :a2a2 => 1112}]} ] } ] }];
nest3.max_depth.should eq(4)
end
end
# describe GriddableHash, '#max_width' do
# it 'returns count for unnnested array' do
# unnest = GriddableHash.new([1,2,3]);
# unnest.max_width.should eq(3)
# end
# it 'returns width of single nest' do
# #
# # 1 2
# # 4 3
# nest2 = GriddableHash.new([1,[2,3],4]);
# nest2.max_width.should eq(2)
# end
# it 'returns 2 for 4 deep' do
# nest4 = GriddableHash.new([1,[[[2,3],4],5],6]);
# #
# # 1 2
# # 6 5 4 3
# nest4.max_width.should eq(2)
# end
# it 'returns correct for example' do
# nest_t = GriddableHash.new(['a', ['a1', ['a1a', ['a1a1', 'a1a2']]],
# ['a2', ['a2a', 'a2b']] ]);
# # a a1 a1a a1a1
# # a1a2
# # a2 a2a
# # a2b
# nest_t.max_width.should eq(4)
# end
# end
describe GriddableHash, '#to_grid' do
it 'returns single cell for unnnested array' do
unnest = GriddableHash[{:a => 1, :b => 2, :c => 3}];
unnest.to_grid.should eq([[{:a => 1, :b => 2, :c => 3}]])
end
it 'returns ok for 2 level' do
nest2 = GriddableHash[{ :a=>1, :contents=> [
{:a1 => 11},
{:a2 => 12}
]}]
# a=1 a1=11
# a2=12
nest2.to_grid.should eq([
[{:a => 1}, {:a1 => 11}],
[nil, {:a2 => 12}],
])
end
it 'returns ok for 3 level' do
nest3 = GriddableHash[{ :a=>1, :contents=> [
{:a1 => 11, :contents => [{:a1a => 111}, {:a1b => 112}]},
{:a2 => 12, :contents => [{:a2a => 121}]}
]}]
# a=1 a1=11 a1a=111
# a1b=112
# a2=12 a2a=121
nest3.to_grid.should eq([
[{:a => 1}, {:a1 => 11}, {:a1a => 111}],
[nil, nil, {:a1b => 112}],
[nil, {:a2 => 12}, {:a2a => 121}],
])
end
it 'returns correct for nest 4' do
nest4 = GriddableHash[{:a => 1, :contents => [
{:a1 => 11,
:contents => [ {:a1a => 111, :contents => [{:a1a1 => 1111}, {:a1a2 => 1112}] } ] },
{ :a2 => 12,
:contents => [ {:a2a => 121}, {:a2b => 122} ]}
] }];
# a=1 a1=11 a1a=111 a1a1=1111
# a1a2=1112
# a2=12 a2a=121
# a2b=122
nest4.to_grid.should eq([
[ {:a => 1}, {:a1 => 11}, {:a1a => 111}, {:a1a1 => 1111}],
[ nil, nil, nil, {:a1a2 => 1112}],
[ nil, {:a2 => 12}, {:a2a => 121}],
[ nil, nil, {:a2b => 122}],
]);
end
end