forked from cedar-policy/cedar-go
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtestutil_test.go
More file actions
83 lines (71 loc) · 1.52 KB
/
testutil_test.go
File metadata and controls
83 lines (71 loc) · 1.52 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
package cedar
import (
"errors"
"fmt"
"reflect"
"testing"
)
func testutilEquals[T any](t testing.TB, a, b T) {
t.Helper()
if reflect.DeepEqual(a, b) {
return
}
t.Fatalf("got %+v want %+v", a, b)
}
func testutilFatalIf(t testing.TB, c bool, f string, args ...any) {
t.Helper()
if !c {
return
}
t.Fatalf(f, args...)
}
func testutilOK(t testing.TB, err error) {
t.Helper()
if err == nil {
return
}
t.Fatalf("got %v want nil", err)
}
func testutilError(t testing.TB, err error) {
t.Helper()
if err != nil {
return
}
t.Fatalf("got nil want error")
}
func assertError(t *testing.T, got, want error) {
t.Helper()
testutilFatalIf(t, !errors.Is(got, want), "err got %v want %v", got, want)
}
func assertValue(t *testing.T, got, want Value) {
t.Helper()
testutilFatalIf(
t,
!((got == zeroValue() && want == zeroValue()) ||
(got != zeroValue() && want != zeroValue() && got.equal(want))),
"got %v want %v", got, want)
}
func assertBoolValue(t *testing.T, got Value, want bool) {
t.Helper()
testutilEquals[Value](t, got, Boolean(want))
}
func assertLongValue(t *testing.T, got Value, want int64) {
t.Helper()
testutilEquals[Value](t, got, Long(want))
}
func assertZeroValue(t *testing.T, got Value) {
t.Helper()
testutilEquals(t, got, zeroValue())
}
func assertValueString(t *testing.T, v Value, want string) {
t.Helper()
testutilEquals(t, v.String(), want)
}
func safeDoErr(f func() error) (err error) {
defer func() {
if r := recover(); r != nil {
err = fmt.Errorf("%v", r)
}
}()
return f()
}