forked from eduncan911/mspec
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbdd_test.go
273 lines (228 loc) · 7.81 KB
/
bdd_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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
package bdd
import (
"testing"
)
// Some concepts defined:
// - Simple Call: sentence(event, func(it))
// - Multiple Call: sentence(event, func(it, args), like)
// Feature Simple or Multiple Given
// - As a developer,
// - I want to be able to test given with a simple call, or with a multiple call,
// - So that I have more testing power.
func Test_Simple_and_Multiple_Given(t *testing.T) {
given, like, s := Sentences().All()
given(t, "a TestSumOp ts with no handicap", func(when When) {
ts := NewTestSumOp(0)
when("ts.LastResultAsString is called", func(it It) {
val := ts.LastResultAsString
it("should return a empty string", func(assert Assert) {
assert.Empty(val)
})
})
when("ts.Sum(1, 2) is called", func(it It) {
val := ts.Sum(1, 2)
it("should return 3", func(assert Assert) {
assert.Equal(val, 3)
})
it("should have ts.LastResultAsString return '3'", func(assert Assert) {
assert.Equal(ts.LastResultAsString, "3")
})
})
})
given(t, "a TestSumOp ts with handicap %[1]v", func(when When, args ...interface{}) {
ts := NewTestSumOp(args[0].(int))
when("ts.LastResultAsString is called", func(it It) {
val := ts.LastResultAsString
it("should return a empty string", func(assert Assert) {
assert.Empty(val)
})
})
when("ts.Sum(%[2]v, %[3]v) is called", func(it It) {
val := ts.Sum(args[1].(int), args[2].(int))
it("should return %[4]v", func(assert Assert) {
assert.Equal(val, args[3].(int))
})
it("should have ts.LastResultAsString return '%[5]v'", func(assert Assert) {
assert.Equal(ts.LastResultAsString, args[4].(string))
})
})
}, like(
s(0, 1, 2, 3, "3"), s(1, 1, 2, 4, "4"), s(0, 2, 3, 5, "5"),
s(-1, 2, 3, 4, "4"), s(0, -3, 2, -1, "-1"), s(2, -3, 2, 1, "1"),
s(0, 2, 0, 2, "2"), s(-2, 2, 0, 0, "0"),
))
}
// Feature Simple or Multiple When
// - As a developer,
// - I want to be able to test when with a simple call, or with a multiple call,
// - So that I have more testing power.
func Test_Simple_and_Multiple_When(t *testing.T) {
given, like, s := Sentences().All()
given(t, "a empty TestSumOp ts", func(when When) {
var ts TestSumOp
when("ts.LastResultAsString is called", func(it It) {
val := ts.LastResultAsString
it("should return a empty string", func(assert Assert) {
assert.Empty(val)
})
})
when("ts.Sum(%[1]v, %[2]v) is called", func(it It, args ...interface{}) {
val := ts.Sum(args[0].(int), args[1].(int))
it("should return %[3]v", func(assert Assert) {
assert.Equal(val, args[2])
})
it("should have ts.LastResultAsString return '%[4]v'", func(assert Assert) {
assert.Equal(ts.LastResultAsString, args[3])
})
}, like(
s(1, 2, 3, "3"), s(2, 3, 5, "5"), s(-3, 2, -1, "-1"),
s(2, 0, 2, "2"), s(12, 21, 33, "33"), s(-18, 5, -13, "-13"),
))
})
}
// Feature Simple and Multiple it
// - As a developer,
// - I want to be able to test it with a simple call, or with a multiple call,
// - So that I have more testing power.
func Test_Simple_and_Multiple_It(t *testing.T) {
given, like, s := Sentences().All()
given(t, "a empty TestSumOp ts", func(when When) {
var ts TestSumOp
when("val := ts.Sum(1, 2) is called", func(it It) {
val := ts.Sum(1, 2)
it("should have val equal to 3", func(assert Assert) {
assert.Equal(val, 3)
})
it("should have ts.LastResultAsString return '3'", func(assert Assert) {
assert.Equal(ts.LastResultAsString, "3")
})
it("should have TestInt(val).Sum(%[1]v) return %[2]v", func(assert Assert, args ...interface{}) {
assert.Equal(TestInt(val).Sum(args[0].(int)), args[1].(int))
}, like(
s(0, 3), s(1, 4), s(2, 5), s(10, 13), s(3000, 3003),
s(-1, 2), s(-3, 0), s(-10, -7), s(-3000, -2997),
))
})
})
}
// Feature Mixed Multiple sentences
// - As a developer,
// - I want to be able to test with mixed multiple sentences, like given, when and it,
// - So that I have more testing power.
func Test_Mixed_Multiple_sentences(t *testing.T) {
given, like, s := Sentences().All()
given(t, "a empty TestSumOp ts with handicap %[1]v", func(when When, args ...interface{}) {
h := args[0].(int)
ts := NewTestSumOp(h)
when("val := ts.Sum(%[1]v, %[2]v) is called", func(it It, args ...interface{}) {
a := args[0].(int)
b := args[1].(int)
val := ts.Sum(a, b)
it("should have TestInt(val).Sum(%[1]v) return %[2]v", func(assert Assert, args ...interface{}) {
assert.Equal(TestInt(val).Sum(args[0].(int)), args[1].(int))
}, like(
s(1, 1+a+b+h), s(2, 2+a+b+h),
s(3, 3+a+b+h), s(12, 12+a+b+h),
))
}, like(
s(1, 2), s(2, 3), s(2, 0),
s(1, -2), s(3, 0),
))
}, like(
s(1), s(2), s(3), s(10),
s(21), s(637), s(-1),
))
}
// Feature Simple and Multiple NonImplemented sentences
// - As a developer,
// - I want to be able to test with simple and multiple NonImplemented sentences,
// - So I have more warnings to sentences not implemented.
func Test_Simple_and_Multiple_NonImplemented_sentences(t *testing.T) {
given, like, s := Sentences().All()
given(t, "a empty TestSumOp ts", func(when When) {
when("ts.LastResultAsString is called", func(it It) {
it("should return a empty string")
})
when("ts.Sum(%[1]v, %[2]v) is called", like(
s(1, 2), s(-1, 2), s(-1, 0),
s(3, 0), s(13, 2),
))
})
given(t, "a empty TestSumOp ts with handicap 3", func(when When) {
when("val := ts.Sum(1, 2) is called", func(it It) {
it("should have val equal to 3")
it("should have ts.LastResultAsString return '3'")
it("should have TestInt(val).Sum(%[1]v) return %[2]v", like(s(0, 3), s(1, 4), s(2, 5), s(10, 13), s(3000, 3003), s(-1, 2), s(-3, 0), s(-10, -7), s(-3000, -2997)))
})
})
}
// Feature Golden JSON sentences
// - As a developer,
// - I want to be able to test using golden JSON files with test cases,
// - So I can update easily those test cases, and test many times.
func Test_Golden_JSON_sentences(t *testing.T) {
given := Sentences().Golden()
input, gold := &struct {
A int `json:"a"`
B int `json:"b"`
}{}, &struct {
Sum int `json:"sum"`
Ts *TestSumOp `json:"ts"`
}{}
given(t, "a empty TestSumOp ts", func(when When, golden Golden) {
golden.Load(input, gold)
ts := &TestSumOp{}
when("val := ts.Sum(%[input.a]v, %[input.b]v)", func(it It) {
val := ts.Sum(input.A, input.B)
golden.Update(func() interface{} {
gold.Sum = val
gold.Ts = ts
return gold
})
it("should result to %[golden.sum]v", func(assert Assert) {
assert.Equal(gold.Sum, val)
})
it("ts.Handicap should equal %[golden.ts.handicap]v", func(assert Assert) {
assert.Equal(gold.Ts.Handicap, ts.Handicap)
})
it("ts.LastResultAsString should equal %[golden.ts.last_result]v", func(assert Assert) {
assert.Equal(gold.Ts.LastResultAsString, ts.LastResultAsString)
})
})
})
}
// Feature Golden YAML sentences
// - As a developer,
// - I want to be able to test using golden YAML files with test cases,
// - So I can update easily those test cases, and test many times.
func Test_Golden_YAML_sentences(t *testing.T) {
given := Sentences().Golden()
input, gold := &struct {
A int `yaml:"a"`
B int `yaml:"b"`
}{}, &struct {
Sum int `yaml:"sum"`
Ts *TestSumOp `yaml:"ts"`
}{}
given(t, "a empty TestSumOp ts", func(when When, golden Golden) {
golden.Load(input, gold)
ts := &TestSumOp{}
when("val := ts.Sum(%[input.a]v, %[input.b]v)", func(it It) {
val := ts.Sum(input.A, input.B)
golden.Update(func() interface{} {
gold.Sum = val
gold.Ts = ts
return gold
})
it("should result to %[golden.sum]v", func(assert Assert) {
assert.Equal(gold.Sum, val)
})
it("ts.Handicap should equal %[golden.ts.handicap]v", func(assert Assert) {
assert.Equal(gold.Ts.Handicap, ts.Handicap)
})
it("ts.LastResultAsString should equal %[golden.ts.last_result]v", func(assert Assert) {
assert.Equal(gold.Ts.LastResultAsString, ts.LastResultAsString)
})
})
})
}