-
Notifications
You must be signed in to change notification settings - Fork 1
/
lib.go
95 lines (79 loc) · 1.63 KB
/
lib.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
package ensure
import (
"context"
"fmt"
"testing"
)
type ScenarioPrinter interface {
Write(s string)
}
type fmtPrinter struct {
}
func (d fmtPrinter) Write(s string) {
fmt.Println(s)
}
var (
Printer ScenarioPrinter = &fmtPrinter{}
)
func That(s string, f func(s *Scenario), t *testing.T) {
Printer.Write("Scenario: " + s)
scn := &Scenario{
t: t,
}
f(scn)
for _, f2 := range scn.teardownMethods {
Printer.Write("Tearing down " + f2.name)
f2.f()
}
}
type Scenario struct {
teardownMethods []tearDown
t *testing.T
}
func (s2 *Scenario) Given(s string, f func()) *Scenario {
Printer.Write(" Given " + s)
f()
return s2
}
func (s2 *Scenario) And(s string, f func()) *Scenario {
Printer.Write(" And " + s)
f()
return s2
}
func (s2 *Scenario) When(s string, f func()) *Scenario {
Printer.Write(" When " + s)
f()
return s2
}
func (s2 *Scenario) Background(s string, f func()) *Scenario {
Printer.Write(" Background " + s)
f()
return s2
}
func (s2 *Scenario) Then(s string, f func()) *Scenario {
Printer.Write(" Then " + s)
f()
return s2
}
// Teardown adds a function to be called when the scenario ends.
// The function is passed a context that is cancelled when the scenario ends.
func (s2 *Scenario) Teardown(s string, ctx context.Context, f func(ctx context.Context)) *Scenario {
s2.addTearDown(s, func() {
ctx.Done()
f(ctx)
})
return s2
}
func (s2 *Scenario) addTearDown(s string, f func()) {
s2.teardownMethods = append(s2.teardownMethods, tearDown{
name: s,
f: f,
})
}
func (s2 *Scenario) NotImplemented() {
s2.t.Fatal("Not implemented")
}
type tearDown struct {
name string
f func()
}