Repository navigation
Expand file tree
/
Copy pathtestc.go
More file actions
91 lines (77 loc) · 1.87 KB
/
Copy pathtestc.go
File metadata and controls
91 lines (77 loc) · 1.87 KB
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
package testc
import (
"sync"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
// New .
func New(t *testing.T) *TestC {
return &TestC{
t: t,
Assert: assert.New(t),
Require: require.New(t),
}
}
// TestC extends the standard testing functions with assertions and BDD logging
type TestC struct {
t *testing.T
Assert *assert.Assertions
Require *require.Assertions
}
func (t *TestC) logItf(name string, format string, args ...interface{}) {
t.t.Helper()
t.t.Logf(name+" "+format, args...)
}
// Givenf prepends the BDD GIVEN word
func (t *TestC) Givenf(format string, args ...interface{}) {
t.t.Helper()
t.logItf("GIVEN", format, args...)
}
// Whenf prepends the BDD WHEN word
func (t *TestC) Whenf(format string, args ...interface{}) {
t.t.Helper()
t.logItf("WHEN", format, args...)
}
// Thenf prepends the BDD THEN word
func (t *TestC) Thenf(format string, args ...interface{}) {
t.t.Helper()
t.logItf("THEN", format, args...)
}
func (t *TestC) logIt(name string, args ...interface{}) {
t.t.Helper()
newArgs := append([]interface{}{}, name)
newArgs = append(newArgs, args...)
t.t.Log(newArgs...)
}
// Given prepends the BDD GIVEN word
func (t *TestC) Given(args ...interface{}) {
t.t.Helper()
t.logIt("GIVEN", args...)
}
// When prepends the BDD WHEN word
func (t *TestC) When(args ...interface{}) {
t.t.Helper()
t.logIt("WHEN", args...)
}
// Then prepends the BDD THEN word
func (t *TestC) Then(args ...interface{}) {
t.t.Helper()
t.logIt("THEN", args...)
}
// WaitTimeout waits for the sync group or times out and fails
func (t *TestC) WaitTimeout(wait *sync.WaitGroup, timeout time.Duration) {
t.t.Helper()
finished := make(chan struct{})
go func() {
wait.Wait()
finished <- struct{}{}
}()
select {
case <-finished:
break
case <-time.After(timeout):
t.Assert.FailNow("timeout triggered")
}
}