4
4
"context"
5
5
"testing"
6
6
7
+ "golang.org/x/xerrors"
8
+
9
+ "cdr.dev/slog"
7
10
"cdr.dev/slog/internal/assert"
8
11
"cdr.dev/slog/sloggers/slogtest"
9
12
)
@@ -15,6 +18,12 @@ func TestStateless(t *testing.T) {
15
18
slogtest .Debug (tb , "hello" )
16
19
slogtest .Info (tb , "hello" )
17
20
21
+ slogtest .Error (tb , "canceled" , slog .Error (xerrors .Errorf ("test %w:" , context .Canceled )))
22
+ assert .Equal (t , "errors" , 0 , tb .errors )
23
+
24
+ slogtest .Error (tb , "deadline" , slog .Error (xerrors .Errorf ("test %w:" , context .DeadlineExceeded )))
25
+ assert .Equal (t , "errors" , 0 , tb .errors )
26
+
18
27
slogtest .Error (tb , "hello" )
19
28
assert .Equal (t , "errors" , 1 , tb .errors )
20
29
@@ -45,6 +54,60 @@ func TestIgnoreErrors(t *testing.T) {
45
54
l .Fatal (bg , "hello" )
46
55
}
47
56
57
+ func TestIgnoreErrorIs_Default (t * testing.T ) {
58
+ t .Parallel ()
59
+
60
+ tb := & fakeTB {}
61
+ l := slogtest .Make (tb , nil )
62
+
63
+ l .Error (bg , "canceled" , slog .Error (xerrors .Errorf ("test %w:" , context .Canceled )))
64
+ assert .Equal (t , "errors" , 0 , tb .errors )
65
+
66
+ l .Error (bg , "deadline" , slog .Error (xerrors .Errorf ("test %w:" , context .DeadlineExceeded )))
67
+ assert .Equal (t , "errors" , 0 , tb .errors )
68
+
69
+ l .Error (bg , "new" , slog .Error (xerrors .New ("test" )))
70
+ assert .Equal (t , "errors" , 1 , tb .errors )
71
+
72
+ defer func () {
73
+ recover ()
74
+ assert .Equal (t , "fatals" , 1 , tb .fatals )
75
+ }()
76
+
77
+ l .Fatal (bg , "hello" , slog .Error (xerrors .Errorf ("fatal %w:" , context .Canceled )))
78
+ }
79
+
80
+ func TestIgnoreErrorIs_Explicit (t * testing.T ) {
81
+ t .Parallel ()
82
+
83
+ tb := & fakeTB {}
84
+ ignored := xerrors .New ("ignored" )
85
+ notIgnored := xerrors .New ("not ignored" )
86
+ l := slogtest .Make (tb , & slogtest.Options {IgnoredErrorIs : []error {ignored }})
87
+
88
+ l .Error (bg , "ignored" , slog .Error (xerrors .Errorf ("test %w:" , ignored )))
89
+ assert .Equal (t , "errors" , 0 , tb .errors )
90
+
91
+ l .Error (bg , "not ignored" , slog .Error (xerrors .Errorf ("test %w:" , notIgnored )))
92
+ assert .Equal (t , "errors" , 1 , tb .errors )
93
+
94
+ l .Error (bg , "canceled" , slog .Error (xerrors .Errorf ("test %w:" , context .Canceled )))
95
+ assert .Equal (t , "errors" , 2 , tb .errors )
96
+
97
+ l .Error (bg , "deadline" , slog .Error (xerrors .Errorf ("test %w:" , context .DeadlineExceeded )))
98
+ assert .Equal (t , "errors" , 3 , tb .errors )
99
+
100
+ l .Error (bg , "new" , slog .Error (xerrors .New ("test" )))
101
+ assert .Equal (t , "errors" , 4 , tb .errors )
102
+
103
+ defer func () {
104
+ recover ()
105
+ assert .Equal (t , "fatals" , 1 , tb .fatals )
106
+ }()
107
+
108
+ l .Fatal (bg , "hello" , slog .Error (xerrors .Errorf ("test %w:" , ignored )))
109
+ }
110
+
48
111
func TestCleanup (t * testing.T ) {
49
112
t .Parallel ()
50
113
0 commit comments