-
Notifications
You must be signed in to change notification settings - Fork 7
/
h_test.go
88 lines (75 loc) · 1.89 KB
/
h_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
package gone_test
import (
"github.com/gone-io/gone"
"github.com/stretchr/testify/assert"
"testing"
)
type Triangle struct {
gone.Flag
a gone.XPoint `gone:"point-a"`
b gone.Point `gone:"point-b"`
c *gone.Point `gone:"point-c"`
}
func TestRun(t *testing.T) {
gone.Run(func(cemetery gone.Cemetery) error {
cemetery.
Bury(&Triangle{}).
Bury(&gone.Point{Index: 1}, gone.GonerId("point-a")).
Bury(&gone.Point{Index: 2}, gone.GonerId("point-b")).
Bury(&gone.Point{Index: 3}, gone.GonerId("point-c"))
return nil
})
}
func TestNew(t *testing.T) {
var sort []int
const gonerId gone.GonerId = "Triangle"
heaven := gone.
New(func(cemetery gone.Cemetery) error {
cemetery.
Bury(&Triangle{}, gonerId).
Bury(&gone.Point{Index: 1}, gone.GonerId("point-a")).
Bury(&gone.Point{Index: 2}, gone.GonerId("point-b")).
Bury(&gone.Point{Index: 3}, gone.GonerId("point-c"))
return nil
})
heaven.
BeforeStart(func(cemetery gone.Cemetery) error {
tomb := cemetery.GetTomById(gonerId)
triangle, ok := tomb.GetGoner().(*Triangle)
assert.True(t, ok)
assert.Equal(t, triangle.a.GetIndex(), 1)
assert.Equal(t, triangle.b.GetIndex(), 2)
assert.Equal(t, triangle.c.Index, 3)
sort = append(sort, 0)
return nil
}).
AfterStart(func(cemetery gone.Cemetery) error {
sort = append(sort, 1)
return nil
}).
BeforeStop(func(cemetery gone.Cemetery) error {
sort = append(sort, 2)
return nil
}).
AfterStop(func(cemetery gone.Cemetery) error {
sort = append(sort, 3)
return nil
})
heaven.Install()
heaven.Start()
heaven.Stop()
for i := range sort {
assert.Equal(t, i, sort[i])
}
}
func TestServe(t *testing.T) {
gone.AfterStopSignalWaitSecond = 1
gone.Serve(func(cemetery gone.Cemetery) error {
go func() {
tom := cemetery.GetTomById(gone.IdGoneHeaven)
heaven := tom.GetGoner().(gone.Heaven)
heaven.End()
}()
return nil
})
}