-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmanager_test.go
121 lines (107 loc) · 2.06 KB
/
manager_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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package manager
import (
"fmt"
"os"
"sort"
"strings"
"testing"
)
type EntryTester struct {
Name string
Email string
Alias string
}
func (e *EntryTester) AddAfter() {
_, _ = fmt.Fprintln(os.Stdout, "AddAfter:", e)
}
func (e *EntryTester) DeleteAfter() {
_, _ = fmt.Fprintln(os.Stdout, "DeleteAfter:", e)
}
func (e *EntryTester) UpdateAfter() {
_, _ = fmt.Fprintln(os.Stdout, "UpdateAfter:", e)
}
func (e *EntryTester) Key() interface{} {
return e.Email
}
func (e *EntryTester) Copy(n Entry) {
if ne, ok := n.(*EntryTester); ok {
e.Name = ne.Name
e.Email = ne.Email
e.Alias = ne.Alias
}
}
func Sort(entries []Entry) []Entry {
sort.SliceStable(entries, func(i, j int) bool {
if strings.Compare(entries[i].Key().(string), entries[j].Key().(string)) <= 0 {
return true
} else {
return false
}
})
return entries
}
var (
tests = []*EntryTester{
{Name: "Wang Qiang", Email: "[email protected]",},
{Name: "Li Hong", Email: "[email protected]",},
{Name: "Zhang Lei", Email: "[email protected]",},
}
)
func TestMain(m *testing.M) {
NotifyRegisterHandler(func(ch <-chan Notify) {
for n := range ch {
_, _ = fmt.Fprintln(os.Stdout, "receive:", n.Operate, n.Entry)
}
})
SortRegisterHandler(Sort)
m.Run()
}
func TestAdd(t *testing.T) {
for _, e := range tests {
Add(e)
t.Log("Get:", Get(e.Key()))
}
for _, e := range GetAll() {
t.Log(e)
}
}
func TestUpdate(t *testing.T) {
for _, e := range tests {
Add(e)
t.Log("Get:", Get(e.Key()))
}
for _, e := range tests {
o, n := *e, *e
n.Alias = "This is alias test"
Update(&n)
t.Log("old", o, "new:", Get(e.Key()))
}
}
func TestDelete(t *testing.T) {
for _, e := range tests {
Add(e)
t.Log("Get:", Get(e.Key()))
}
for _, e := range tests {
o := Delete(e.Key())
t.Log("Delete:", o)
}
}
func TestTraverse(t *testing.T) {
for _, e := range tests {
Add(e)
t.Log("Get:", Get(e.Key()))
}
var entries []*EntryTester
Traverse(func(v interface{}) {
if v == nil {
return
}
if e, ok := v.(*EntryTester); ok {
entries = append(entries, e)
}
})
for _, e := range entries {
t.Log(e)
}
}