-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlyfecycle_test.go
More file actions
100 lines (89 loc) · 3.12 KB
/
lyfecycle_test.go
File metadata and controls
100 lines (89 loc) · 3.12 KB
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
/*
Package lyfecycle_test is a test package based on an external representation of normal usage.
It does not have access to Lyfecycle internals, nor does it need them, and nor _should_ it need them.
If this test package passes, then users should expect to be satisfied with the Lyfecycle implementation.
*/
package lyfecycle_test
import (
"github.com/eshork/lyfecycle" // gotta have the package imported to use it
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
// From here, for a bit, we're defining an app as we normally would
// BEGIN NORMAL'ISH APP BEHAVIOUR
// Declare your lifecycle stages with unique integer values
const (
LyfeCycleStage1 = iota
LyfeCycleStage2
LyfeCycleStage3
LyfeCycleStage4
LyfeCycleStage5
)
var calledLyfeCycleStage1 bool
var calledLyfeCycleStage2 bool
var calledLyfeCycleStage3 bool
var calledLyfeCycleStage4 bool
var calledLyfeCycleStage5 bool
var test_stageList lyfecycle.StageIDsList // in a normal app, you probably wouldn't keep a global reference to this var - this one is only here to support testing
// during the soonest possible execution moment, register the full list of lifecycle stages
// module init() is about as early as you can get
func init() {
// sequential stage list from the declared stage IDs
test_stageList = lyfecycle.StageIDsList{ // shoving this into a global variable so we can validate it later...
LyfeCycleStage1,
LyfeCycleStage2,
LyfeCycleStage3,
LyfeCycleStage4,
LyfeCycleStage5,
}
// declare this stage progression with the lyfecyle system
lyfecycle.DefineStages(test_stageList)
// register your callbacks as desired
lyfecycle.RegisterEvent(LyfeCycleStage1, func() {
calledLyfeCycleStage1 = true
})
lyfecycle.RegisterEvent(LyfeCycleStage2, func() {
calledLyfeCycleStage2 = true
})
lyfecycle.RegisterEvent(LyfeCycleStage3, func() {
calledLyfeCycleStage3 = true
})
lyfecycle.RegisterEvent(LyfeCycleStage4, func() {
calledLyfeCycleStage4 = true
})
lyfecycle.RegisterEvent(LyfeCycleStage5, func() {
calledLyfeCycleStage5 = true
})
lyfecycle.PerformAllStages()
}
// END NORMAL'ISH APP BEHAVIOUR
// This is where we start testing
var _ = Describe("Lyfecycle", func() {
BeforeEach(func() {
calledLyfeCycleStage1 = false
calledLyfeCycleStage2 = false
calledLyfeCycleStage3 = false
calledLyfeCycleStage4 = false
calledLyfeCycleStage5 = false
})
Context("GetDefinedStages()", func() {
It("reflects the stages we created in our app init", func() {
Expect(lyfecycle.GetDefinedStages()).Should(BeEquivalentTo(test_stageList))
})
})
Context("PerformAllStages()", func() {
It("runs each stage", func() {
Expect(calledLyfeCycleStage1).Should(BeFalse())
Expect(calledLyfeCycleStage2).Should(BeFalse())
Expect(calledLyfeCycleStage3).Should(BeFalse())
Expect(calledLyfeCycleStage4).Should(BeFalse())
Expect(calledLyfeCycleStage5).Should(BeFalse())
lyfecycle.PerformAllStages()
Expect(calledLyfeCycleStage1).Should(BeTrue())
Expect(calledLyfeCycleStage2).Should(BeTrue())
Expect(calledLyfeCycleStage3).Should(BeTrue())
Expect(calledLyfeCycleStage4).Should(BeTrue())
Expect(calledLyfeCycleStage5).Should(BeTrue())
})
})
})