-
Notifications
You must be signed in to change notification settings - Fork 0
/
godog_test.go
166 lines (130 loc) · 3.14 KB
/
godog_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
package bootstrap
import (
"bytes"
"flag"
"fmt"
"math/rand"
"os"
"path/filepath"
"strings"
"testing"
"time"
"github.com/cucumber/godog"
"github.com/nhatthm/timeparser"
"github.com/stretchr/testify/assert"
"github.com/godogx/clocksteps"
)
// Used by init().
//
//nolint:gochecknoglobals
var (
runGoDogTests bool
out = new(bytes.Buffer)
opt = godog.Options{
Strict: true,
Output: out,
}
)
// This has to run on init to define -godog flag, otherwise "undefined flag" error happens.
//
//nolint:gochecknoinits
func init() {
flag.BoolVar(&runGoDogTests, "godog", false, "Set this flag is you want to run godog BDD tests")
godog.BindCommandLineFlags("", &opt)
}
func TestIntegration(t *testing.T) {
if !runGoDogTests {
t.Skip(`Missing "-godog" flag, skipping integration test.`)
}
c := clocksteps.New()
RunSuite(t, "..", func(_ *testing.T, ctx *godog.ScenarioContext) {
c.RegisterContext(ctx)
registerClock(c, ctx)
})
}
func RunSuite(t *testing.T, path string, featureContext func(t *testing.T, ctx *godog.ScenarioContext)) {
t.Helper()
flag.Parse()
if opt.Randomize == 0 {
opt.Randomize = rand.Int63() // nolint: gosec
}
var paths []string
files, err := os.ReadDir(filepath.Clean(path))
assert.NoError(t, err)
paths = make([]string, 0, len(files))
for _, f := range files {
if strings.HasSuffix(f.Name(), ".feature") {
paths = append(paths, filepath.Join(path, f.Name()))
}
}
for _, path := range paths {
path := path
t.Run(path, func(t *testing.T) {
opt.Paths = []string{path}
suite := godog.TestSuite{
Name: "Integration",
TestSuiteInitializer: nil,
ScenarioInitializer: func(s *godog.ScenarioContext) {
featureContext(t, s)
},
Options: &opt,
}
status := suite.Run()
if status != 0 {
fmt.Println(out.String())
assert.Fail(t, "one or more scenarios failed in feature: "+path)
}
})
}
}
func registerClock(c *clocksteps.Clock, ctx *godog.ScenarioContext) {
ctx.Step(`the time is now`, func() error {
return isNow(c)
})
ctx.Step(`the time is not now`, func() error {
return isNotNow(c)
})
ctx.Step(`the time is "([^"]*)"`, func(s string) error {
return expectTime(c, s)
})
ctx.Step(`wait for ([^\s]*)`, waitFor)
}
func isNow(c *clocksteps.Clock) error {
now := time.Now()
ts := c.Now()
min := now.Add(-10 * time.Millisecond)
max := now.Add(10 * time.Millisecond)
if min.After(ts) || max.Before(ts) {
return fmt.Errorf("expected: %q < %q < %q", min.String(), ts.String(), max.String())
}
return nil
}
func isNotNow(c *clocksteps.Clock) error {
now := time.Now()
ts := c.Now()
min := now.Add(-10 * time.Millisecond)
max := now.Add(10 * time.Millisecond)
if ts.After(min) && ts.Before(max) {
return fmt.Errorf("the time is now")
}
return nil
}
func expectTime(c *clocksteps.Clock, s string) error {
expected, err := timeparser.Parse(s)
if err != nil {
return err
}
now := c.Now()
if now != expected {
return fmt.Errorf("expected: %q, got %q", expected.String(), now.String())
}
return nil
}
func waitFor(s string) error {
d, err := time.ParseDuration(s)
if err != nil {
return err
}
<-time.After(d)
return nil
}