forked from regen-network/gocuke
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hooks_test.go
114 lines (95 loc) · 2.44 KB
/
hooks_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
package gocuke
import (
"testing"
"gotest.tools/v3/assert"
"pgregory.net/rapid"
)
func TestHooks(t *testing.T) {
assert.Assert(t, !longRun)
assert.Assert(t, !shortRun)
// test tag expression
NewRunner(t, &hooksSuite{}).
Path("features/hooks.feature").
Tags("@long").
NonParallel().
Run()
assert.Assert(t, longRun)
assert.Assert(t, !shortRun)
if open != 0 {
t.Fatalf("expected 0 open resources, got: %d", open)
}
NewRunner(t, &hooksSuite{}).
Path("features/hooks.feature").
ShortTags("not @long").
NonParallel().
Run()
assert.Assert(t, longRun)
assert.Assert(t, shortRun)
if open != 0 {
t.Fatalf("expected 0 open resources, got: %d", open)
}
}
var (
longRun = false
shortRun = false
open int64 = 0
)
type hooksSuite struct {
TestingT
numOpenForScenario int64
numOpenForStep int64
numOpenForCleanup int64
scenario Scenario
}
func (s *hooksSuite) IOpenAResource(step Step) {
assert.Equal(s, "I open a resource", step.Text())
shortRun = true
s.numOpenForScenario = 1
open += s.numOpenForScenario
}
func (s *hooksSuite) IOpenAnyResources(t *rapid.T) {
longRun = true
s.numOpenForScenario = rapid.Int64Range(1, 100).AsAny().Draw(t, "numResources").(int64)
open += s.numOpenForScenario
}
func (s *hooksSuite) IOpenAResourceWithCleanup(step Step) {
assert.Equal(s, "I open a resource with cleanup", step.Text())
s.numOpenForScenario = 1
s.numOpenForCleanup = 1
open += s.numOpenForScenario + s.numOpenForCleanup
s.Cleanup(func() {
open -= s.numOpenForCleanup
})
}
func (s *hooksSuite) ItIsOpen(step Step) {
assert.Equal(s, "it is open", step.Text())
if open < s.numOpenForScenario {
s.Fatalf("expected at least %d resources to be open", s.numOpenForScenario)
}
}
func (s *hooksSuite) ExpectScenarioName(a string) {
assert.Assert(s, s.scenario != nil)
assert.Equal(s, a, s.scenario.Name())
}
func (s *hooksSuite) ExpectScenarioTag(a string) {
assert.Equal(s, 1, len(s.scenario.Tags()))
assert.Equal(s, a, s.scenario.Tags()[0])
}
func (s *hooksSuite) Before(scenario Scenario) {
s.scenario = scenario
}
func (s *hooksSuite) BeforeStep() {
if s.numOpenForStep != 0 {
s.Fatalf("expected step resources to be 0 before step")
}
s.numOpenForStep = 1
}
func (s *hooksSuite) AfterStep() {
if s.numOpenForStep != 1 {
s.Fatalf("expected step resources to be 1 after step, got: %d", s.numOpenForStep)
}
s.numOpenForStep = 0
}
func (s *hooksSuite) After() {
open -= s.numOpenForScenario
}