-
Notifications
You must be signed in to change notification settings - Fork 2
/
toggle_test.go
228 lines (199 loc) · 6.87 KB
/
toggle_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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
package toggle_test
import (
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"github.com/xchapter7x/toggle"
)
var _ = Describe("toggle package", func() {
controlNamespace := "hi"
BeforeEach(func() {
toggle.Init(controlNamespace, nil)
})
AfterEach(func() {
toggle.Close()
})
Describe("Flip Function", func() {
flagname := "controlflag"
controlStringA := "controltest"
controlStringB := "controlB"
var testFuncA func(string) string = func(arg1 string) string {
return arg1
}
var testFuncB func(string) string = func(arg1 string) string {
return controlStringB
}
It("Should do something", func() {
var s string
toggle.RegisterFeature(flagname)
response := []interface{}{&s}
err := toggle.FlipP(flagname, response, testFuncA, testFuncB, controlStringA)
Expect(s).To(Equal(controlStringA))
Ω(err).Should(BeNil())
})
})
Describe("RegisterFeature Function", func() {
It("Should inject a new feature and return nil error", func() {
initialFeatureCount := len(toggle.ShowFeatures())
featureName := "sampleFeature"
err := toggle.RegisterFeature(featureName)
currentFeatureCount := len(toggle.ShowFeatures())
Expect(initialFeatureCount).NotTo(Equal(currentFeatureCount))
Ω(err).Should(BeNil())
})
It("Should add feature record for referencing", func() {
initialFeatureList := toggle.ShowFeatures()
featureName := "sampleFeature"
_, controlExists := initialFeatureList[featureName]
toggle.RegisterFeature(featureName)
currentFeatureList := toggle.ShowFeatures()
_, currentExists := currentFeatureList[toggle.GetFullFeatureSignature(featureName)]
Expect(controlExists).NotTo(Equal(currentExists))
})
It("Should ignore duplicate register calls and return non nil error", func() {
featureName := "sampleFeature"
toggle.RegisterFeature(featureName)
initialFeatureCount := len(toggle.ShowFeatures())
err := toggle.RegisterFeature(featureName)
currentFeatureCount := len(toggle.ShowFeatures())
Expect(initialFeatureCount).To(Equal(currentFeatureCount))
Ω(err).ShouldNot(BeNil())
})
})
Describe("IsActive function", func() {
flagName := "bogusFlag"
It("Should return false if given unregistered flag", func() {
response := toggle.IsActive(flagName)
Expect(response).To(Equal(false))
})
It("Should return false if given flag that is FEATURE_OFF status ", func() {
toggle.RegisterFeature(flagName)
toggle.SetFeatureStatus(flagName, toggle.FEATURE_OFF)
response := toggle.IsActive(flagName)
Expect(response).To(Equal(false))
})
It("Should return true if given flag that is FEATURE_ON status ", func() {
toggle.RegisterFeature(flagName)
toggle.SetFeatureStatus(flagName, toggle.FEATURE_ON)
response := toggle.IsActive(flagName)
Expect(response).To(Equal(true))
})
})
Describe("SetFeatureStatus function", func() {
flagName := "bogusFlag"
It("Should return false if setting FEATURE_OFF status from default", func() {
toggle.RegisterFeature(flagName)
toggle.SetFeatureStatus(flagName, toggle.FEATURE_OFF)
response := toggle.IsActive(flagName)
Expect(response).To(Equal(false))
})
It("Should return true if setting FEATURE_ON status from default", func() {
toggle.RegisterFeature(flagName)
toggle.SetFeatureStatus(flagName, toggle.FEATURE_ON)
response := toggle.IsActive(flagName)
Expect(response).To(Equal(true))
})
It("Should return false if setting FEATURE_OFF status updating existing value", func() {
toggle.RegisterFeature(flagName)
toggle.SetFeatureStatus(flagName, toggle.FEATURE_ON)
toggle.SetFeatureStatus(flagName, toggle.FEATURE_OFF)
response := toggle.IsActive(flagName)
Expect(response).To(Equal(false))
})
It("Should return true if setting FEATURE_ON status updating existing value", func() {
toggle.RegisterFeature(flagName)
toggle.SetFeatureStatus(flagName, toggle.FEATURE_OFF)
toggle.SetFeatureStatus(flagName, toggle.FEATURE_ON)
response := toggle.IsActive(flagName)
Expect(response).To(Equal(true))
})
It("Should return a non nil error if flagName not valid", func() {
response := toggle.SetFeatureStatus(flagName, toggle.FEATURE_ON)
Ω(response).ShouldNot(BeNil())
})
It("Should return a nil error if flagName is valid", func() {
toggle.RegisterFeature(flagName)
response := toggle.SetFeatureStatus(flagName, toggle.FEATURE_ON)
Ω(response).Should(BeNil())
})
})
Describe("Flip function", func() {
flagName := "bogusFlag"
controlDefault := "default"
controlNew := "new"
It("Should take arguments and pass them into the selected function", func() {
toggle.RegisterFeature(flagName)
status := ""
toggle.Flip(flagName, func(arg1 string) {
status = arg1
}, func(arg1 string) {
status = arg1
}, controlDefault)
Expect(status).To(Equal(controlDefault))
})
It("Should return arguments from the flip function in the form of an interface array", func() {
toggle.RegisterFeature(flagName)
status := ""
var response []interface{}
argumentCount := 1
controlLen := len(response) + argumentCount
response = toggle.Flip(flagName, func() (r string) {
status = controlDefault
return status
}, func() (r string) {
status = controlNew
return status
})
Expect(len(response)).To(Equal(controlLen))
Expect(response[0]).To(Equal(status))
})
It("Should return arguments from the flip function in the form of an interface array when multiple response values", func() {
toggle.RegisterFeature(flagName)
status := ""
var response []interface{}
argumentCount := 2
controlLen := len(response) + argumentCount
response = toggle.Flip(flagName, func() (r, j string) {
status = controlDefault
return status, status
}, func() (r, j string) {
status = controlNew
return status, status
})
Expect(len(response)).To(Equal(controlLen))
Expect(response[0]).To(Equal(status))
Expect(response[1]).To(Equal(status))
})
It("Should select the default feature function to run when flag is default", func() {
toggle.RegisterFeature(flagName)
status := ""
toggle.Flip(flagName, func() {
status = controlDefault
}, func() {
status = controlNew
})
Expect(status).To(Equal(controlDefault))
})
It("Should select the new feature function to run when flag is set to inactive", func() {
toggle.RegisterFeature(flagName)
toggle.SetFeatureStatus(flagName, toggle.FEATURE_OFF)
status := ""
toggle.Flip(flagName, func() {
status = controlDefault
}, func() {
status = controlNew
})
Expect(status).To(Equal(controlDefault))
})
It("Should select the new feature function to run when flag is set to active", func() {
toggle.RegisterFeature(flagName)
toggle.SetFeatureStatus(flagName, toggle.FEATURE_ON)
status := ""
toggle.Flip(flagName, func() {
status = controlDefault
}, func() {
status = controlNew
})
Expect(status).To(Equal(controlNew))
})
})
})