generated from dogmatiq/template-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
expect.go
158 lines (140 loc) 路 2.82 KB
/
expect.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
package test
import (
"time"
"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
"google.golang.org/protobuf/testing/protocmp"
)
// Expect compares two values and fails the test if they are different.
func Expect[T any](
t FailerT,
failMessage string,
got, want T,
options ...cmp.Option,
) {
t.Helper()
options = append(
[]cmp.Option{
protocmp.Transform(),
cmpopts.EquateEmpty(),
cmpopts.EquateErrors(),
},
options...,
)
if diff := cmp.Diff(
want,
got,
options...,
); diff != "" {
t.Log(failMessage)
t.Fatal(diff)
}
}
// ExpectChannelToReceive waits until a value is received from a channel and
// then compares it to the expected value.
func ExpectChannelToReceive[T any](
t FailerT,
ch <-chan T,
want T,
options ...cmp.Option,
) (got T) {
t.Helper()
ctx := contextOf(t)
var ok bool
select {
case <-ctx.Done():
t.Fatalf("no value received on channel: %s", ctx.Err())
case got, ok = <-ch:
if ok {
Expect(
t,
"channel received an unexpected value",
got,
want,
options...,
)
t.Log("received the expected value on the channel")
} else {
t.Fatal("channel closed while expecting to receive a value")
}
}
return got
}
// ExpectChannelToClose waits until a channel is closed.
func ExpectChannelToClose[T any](
t TestingT,
ch <-chan T,
options ...cmp.Option,
) {
t.Helper()
ctx := contextOf(t)
select {
case <-ctx.Done():
t.Fatalf("cannot was not closed: %s", ctx.Err())
case got, ok := <-ch:
if ok {
var want T // zero value
Expect(
t,
"channel received a value while expecting channel to be closed",
got,
want,
options...,
)
} else {
t.Log("channel closed, as expected")
}
}
}
// ExpectChannelToBlockForDuration expects reading from the channel to block
// until the given duration elapses.
func ExpectChannelToBlockForDuration[T any](
t TestingT,
d time.Duration,
ch <-chan T,
options ...cmp.Option,
) {
t.Helper()
select {
case <-time.After(d):
t.Logf("%s elapsed without receiving a value on the channel, as expected", d)
case got, ok := <-ch:
if ok {
var want T // zero value
Expect(
t,
"channel received a value while expecting channel to block",
got,
want,
options...,
)
} else {
t.Error("channel closed while expecting channel to block")
}
}
}
// ExpectChannelWouldBlock expects reading from the channel would block.
func ExpectChannelWouldBlock[T any](
t TestingT,
ch <-chan T,
options ...cmp.Option,
) {
t.Helper()
select {
default:
t.Log("reading from channel would block, as expected")
case got, ok := <-ch:
if ok {
var want T // zero value
Expect(
t,
"channel received a value while expecting channel to block",
got,
want,
options...,
)
} else {
t.Error("channel closed while expecting channel to block")
}
}
}