-
Notifications
You must be signed in to change notification settings - Fork 0
/
flyweight_test.go
95 lines (71 loc) · 2 KB
/
flyweight_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
package flyweight_test
import (
"testing"
"github.com/klotzandrew/flyweight"
"github.com/stretchr/testify/assert"
)
type RecursiveStruct struct {
Name string
Value []byte
ChildPtr *RecursiveStruct
ChildPtrs []*RecursiveStruct
Leaf Leaf
Leaves []Leaf
LeafPtr *Leaf
LeafPtrs []*Leaf
NonResetter NonResetter
}
func (r *RecursiveStruct) Reset() { flyweight.Reset(r) }
type Leaf struct {
Name string
}
func (r *Leaf) Reset() { flyweight.Reset(r) }
type NonResetter struct {
Name string
}
func TestFlyweightRecursive(t *testing.T) {
item := &RecursiveStruct{
Name: "top level",
Value: []byte("hello world"),
ChildPtr: &RecursiveStruct{Name: "first child", Value: []byte("hello child")},
ChildPtrs: []*RecursiveStruct{
{Name: "first child"},
},
Leaf: Leaf{Name: "first leaf"},
Leaves: []Leaf{
{Name: "first leaf arr"},
},
LeafPtr: &Leaf{Name: "second leaf"},
LeafPtrs: []*Leaf{
{Name: "first leaf ptr arr"},
},
NonResetter: NonResetter{Name: "non-resetter"},
}
item.Reset()
assert.Equal(t, "", item.Name)
assert.Equal(t, []byte{}, item.Value)
assert.Equal(t, "", item.ChildPtr.Name)
assert.Equal(t, []byte{}, item.ChildPtr.Value)
assert.Equal(t, 0, len(item.ChildPtrs))
item.ChildPtrs = item.ChildPtrs[:cap(item.ChildPtrs)]
assert.Equal(t, 1, len(item.ChildPtrs))
underlyingChild := item.ChildPtrs[0]
assert.Equal(t, "", underlyingChild.Name)
assert.Equal(t, "", item.Leaf.Name)
assert.Equal(t, "", item.LeafPtr.Name)
assert.Equal(t, 0, len(item.Leaves))
item.Leaves = item.Leaves[:cap(item.Leaves)]
assert.Equal(t, 1, len(item.Leaves))
underlyingLeaf := item.Leaves[0]
assert.Equal(t, "", underlyingLeaf.Name)
assert.Equal(t, 0, len(item.LeafPtrs))
item.LeafPtrs = item.LeafPtrs[:cap(item.LeafPtrs)]
assert.Equal(t, 1, len(item.LeafPtrs))
underlyingLeafPtr := item.LeafPtrs[0]
assert.Equal(t, "", underlyingLeafPtr.Name)
assert.Equal(t, "", item.NonResetter.Name)
}
func TestEmptyStruct(t *testing.T) {
item := &RecursiveStruct{}
item.Reset()
}