-
Notifications
You must be signed in to change notification settings - Fork 246
/
require.go
69 lines (58 loc) · 1.78 KB
/
require.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
// Package testutil implements various utilities to reduce boilerplate in unit
// tests a la testify.
package testutil
import (
"testing"
"time"
v0 "github.com/authzed/authzed-go/proto/authzed/api/v0"
"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
"github.com/stretchr/testify/require"
core "github.com/authzed/spicedb/pkg/proto/core/v1"
)
// RequireEqualEmptyNil is a version of require.Equal, but considers nil
// slices/maps to be equal to empty slices/maps.
func RequireEqualEmptyNil(t *testing.T, expected, actual interface{}, msgAndArgs ...interface{}) {
opts := []cmp.Option{
cmpopts.IgnoreUnexported(
v0.RelationTuple{},
v0.ObjectAndRelation{},
v0.RelationReference{},
v0.User_Userset{},
v0.User{},
v0.EditCheckResult{},
v0.EditCheckResultValidationError{},
v0.DeveloperError{},
core.RelationTuple{},
core.ObjectAndRelation{},
core.RelationReference{},
core.Relation{},
core.RelationTupleTreeNode_IntermediateNode{},
core.RelationTupleTreeNode{},
core.RelationTupleTreeNode_LeafNode{},
core.DirectSubjects{},
core.SetOperationUserset{}),
cmpopts.EquateEmpty(),
}
msgAndArgs = append(msgAndArgs, cmp.Diff(expected, actual, opts...))
require.Truef(t, cmp.Equal(expected, actual, opts...), "Should be equal", msgAndArgs...)
}
// RequireWithin requires that the runner complete its execution within the specified duration.
func RequireWithin(t *testing.T, runner func(t *testing.T), timeout time.Duration) {
t.Helper()
ch := make(chan bool, 1)
timer := time.NewTimer(timeout)
defer timer.Stop()
go (func() {
t.Helper()
runner(t)
ch <- true
})()
select {
case <-timer.C:
require.Failf(t, "timed out waiting for runner", "expected to complete in %v", timeout)
case <-ch:
timer.Stop()
return
}
}