1
1
// Package assert is a helper package for test assertions.
2
+ //
3
+ // On failure, every assertion will fatal the test.
4
+ //
5
+ // The name parameter is available in each assertion for easier debugging.
2
6
package assert // import "cdr.dev/slog/sloggers/slogtest/assert"
3
7
4
8
import (
9
+ "errors"
5
10
"testing"
6
11
7
12
"cdr.dev/slog"
@@ -13,8 +18,15 @@ import (
13
18
//
14
19
// If they are not equal, it will fatal the test with a diff of the
15
20
// two objects.
21
+ //
22
+ // If act is an error it will be unwrapped.
16
23
func Equal (t testing.TB , exp , act interface {}, name string ) {
17
24
slog .Helper ()
25
+
26
+ if err , ok := act .(error ); ok {
27
+ act = unwrapErr (err )
28
+ }
29
+
18
30
if diff := assert .CmpDiff (exp , act ); diff != "" {
19
31
slogtest .Fatal (t , "unexpected value" ,
20
32
slog .F ("name" , name ),
@@ -24,8 +36,6 @@ func Equal(t testing.TB, exp, act interface{}, name string) {
24
36
}
25
37
26
38
// Success asserts err == nil.
27
- //
28
- // If err isn't nil, it will fatal the test with the error.
29
39
func Success (t testing.TB , err error , name string ) {
30
40
slog .Helper ()
31
41
if err != nil {
@@ -37,9 +47,26 @@ func Success(t testing.TB, err error, name string) {
37
47
}
38
48
39
49
// True asserts act == true.
40
- //
41
- // If act isn't true, it will fatal the test.
42
50
func True (t testing.TB , act bool , name string ) {
43
51
slog .Helper ()
44
52
Equal (t , true , act , name )
45
53
}
54
+
55
+ // Error asserts err != nil.
56
+ func Error (t testing.TB , err error , name string ) {
57
+ slog .Helper ()
58
+ if err == nil {
59
+ slogtest .Fatal (t , "expected error" ,
60
+ slog .F ("name" , name ),
61
+ )
62
+ }
63
+ }
64
+
65
+ func unwrapErr (err error ) error {
66
+ uerr := errors .Unwrap (err )
67
+ for uerr != nil {
68
+ err = uerr
69
+ uerr = errors .Unwrap (uerr )
70
+ }
71
+ return err
72
+ }
0 commit comments