forked from eduncan911/mspec
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbdd.go
129 lines (106 loc) · 3.22 KB
/
bdd.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
package bdd
import (
"fmt"
"testing"
"github.com/ddsgok/bdd/internal/golden"
"github.com/ddsgok/bdd/spec"
)
// Given defines one Feature's specific context to be tested.
func Given(t *testing.T, given string, args ...interface{}) {
gTestBodies, gTestCases := split(S(), args)
whenFunc := gTestBodies.asWhenFunc()
for _, gArgs := range gTestCases {
// setup the testspec that we will be using
testspec := spec.New(t, feature(), printf(given, gArgs))
testspec.PrintFeature()
testspec.PrintContext()
if whenFunc != nil {
whenFunc(func(when string, args ...interface{}) {
wTestBodies, wTestCases := split(gArgs, args)
itFunc := wTestBodies.asItFuncs()
for _, wArgs := range wTestCases {
testspec.When = printf(when, wArgs)
testspec.PrintWhen()
if itFunc != nil {
itFunc(func(it string, args ...interface{}) {
iTestBodies, iTestCases := split(wArgs, args)
assertFunc := iTestBodies.asAssertFunc()
for _, iArgs := range iTestCases {
testspec.It = printf(it, iArgs)
// It output is handled in the testspec.Run() below
if assertFunc != nil {
// Having at least 1 assert means we are implemented
testspec.AssertFn = func(a Assert) {
assertFunc(a, iArgs...)
}
testspec.NotImplemented = false
} else {
testspec.AssertFn = notImplemented()
testspec.NotImplemented = true
}
// Run() handles contextual printing and some delegation
// to the Assert's implementation for error handling
testspec.Run()
}
}, wArgs...)
}
}
}, gArgs...)
}
// reset to default
spec.Config().ResetLasts()
if spec.Config().Output != spec.OutputNone {
fmt.Println()
}
}
}
// GivenWithGolden defines one Feature's specific context to be tested.
func GivenWithGolden(t *testing.T, given string, args ...interface{}) {
goldenFunc := newTestFunc(args...).asGoldenFunc()
feature := feature()
gm := golden.NewManager(feature, given)
if goldenFunc != nil {
for i := 0; i < gm.NumGoldies(); i++ {
testspec := spec.New(t, feature, gprintf(given, gm.Get(i)))
testspec.PrintFeature()
testspec.PrintContext()
goldenFunc(func(when string, wTestBodies ...interface{}) {
itFunc := newTestFunc(wTestBodies...).asItFuncs()
testspec.When = gprintf(when, gm.Get(i))
testspec.PrintWhen()
if itFunc != nil {
itFunc(func(it string, iTestBodies ...interface{}) {
assertFunc := newTestFunc(iTestBodies...).asAssertFunc()
testspec.It = gprintf(it, gm.Get(i))
if assertFunc != nil {
testspec.AssertFn = func(a Assert) {
assertFunc(a)
}
testspec.NotImplemented = false
} else {
testspec.AssertFn = notImplemented()
testspec.NotImplemented = true
}
testspec.Run()
})
}
}, gm.Get(i))
}
}
gm.Update()
spec.Config().ResetLasts()
if spec.Config().Output != spec.OutputNone {
fmt.Println()
}
}
// Setup is used to define before/after (setup/teardown) functions.
func Setup(before, after func()) (fn func(fn func(Assert)) func(Assert)) {
fn = func(fn func(Assert)) func(Assert) {
before()
return func(assert Assert) {
fn(assert)
after()
}
}
return
}