-
Notifications
You must be signed in to change notification settings - Fork 1
/
tree_aggregator_test.go
104 lines (100 loc) · 2.18 KB
/
tree_aggregator_test.go
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
package hranoprovod
import (
"reflect"
"testing"
"github.com/stretchr/testify/assert"
)
func TestTreeNode_AddDeep(t *testing.T) {
tests := []struct {
name string
caller func() *TreeNode
want *TreeNode
}{
{
"spreads correctly the values to categories",
func() *TreeNode {
tn := NewTreeNode("root", 0)
tn.AddDeep(NewElement("one/two", 10), DefaultCategorySeparator)
return tn
},
&TreeNode{
Name: "root",
Total: 0,
Children: map[string]*TreeNode{
"one": {
Name: "one",
Total: 10,
Children: map[string]*TreeNode{
"two": {
Name: "two",
Total: 10,
Children: map[string]*TreeNode{},
},
},
},
}},
},
{
"accumulates correctly the values",
func() *TreeNode {
tn := NewTreeNode("root", 0)
tn.AddDeep(NewElement("one", 10), DefaultCategorySeparator)
tn.AddDeep(NewElement("one/two", 20), DefaultCategorySeparator)
return tn
},
&TreeNode{
Name: "root",
Total: 0,
Children: map[string]*TreeNode{
"one": {
Name: "one",
Total: 30,
Children: map[string]*TreeNode{
"two": {
Name: "two",
Total: 20,
Children: map[string]*TreeNode{},
},
},
},
}},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tn := tt.caller()
if !reflect.DeepEqual(tn, tt.want) {
t.Errorf("want %+v got %+v", tt.want, tt)
}
assert.Equal(t, tt.want, tn)
})
}
}
func TestTreeNode_Keys(t *testing.T) {
tests := []struct {
name string
tn *TreeNode
want []string
}{
{
"returns sorted list of keys",
&TreeNode{
Name: "root",
Total: 0,
Children: map[string]*TreeNode{
"999": {Name: "999", Total: 0, Children: map[string]*TreeNode{}},
"zzzz": {Name: "zzzz", Total: 0, Children: map[string]*TreeNode{}},
"a999": {Name: "a999", Total: 0, Children: map[string]*TreeNode{}},
},
},
[]string{"999", "a999", "zzzz"},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := tt.tn.Keys(); !reflect.DeepEqual(got, tt.want) {
t.Errorf("TreeNode.Keys() = %v, want %v", got, tt.want)
}
})
}
}