-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathxerrors_go120_test.go
More file actions
64 lines (60 loc) · 1.9 KB
/
xerrors_go120_test.go
File metadata and controls
64 lines (60 loc) · 1.9 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
//go:build go1.20
// +build go1.20
package xerrors
import (
"errors"
"fmt"
"testing"
)
func TestJoinf_Go120(t *testing.T) {
err1 := Message("first error")
err2 := Message("second error")
tests := []struct {
format string
args []any
want string
}{
{format: "multiple errors: %w: %w", args: []any{err1, err2}, want: "multiple errors: first error: second error"},
{format: "wrapped multiple nil errors: %w %w", args: []any{nil, nil}, want: "wrapped multiple nil errors: %!w(<nil>) %!w(<nil>)"},
{format: "first error nil: %w %w", args: []any{nil, err2}, want: "first error nil: %!w(<nil>) second error"},
{format: "second error nil: %w %w", args: []any{err1, nil}, want: "second error nil: first error %!w(<nil>)"},
}
for n, tt := range tests {
t.Run(fmt.Sprintf("case-%d", n+1), func(t *testing.T) {
got := Joinf(tt.format, tt.args...)
if got == nil {
t.Errorf("Joinf(%q, %#v): expected non-nil error", tt.format, tt.args)
return
}
if got.Error() != tt.want {
t.Errorf("Joinf(%q, %#v): got: %q, want %q", tt.format, tt.args, got, tt.want)
}
if len(StackTrace(got)) != 0 {
t.Errorf("Joinf(%q, %#v): returned error must not contain a stack trace", tt.format, tt.args)
}
for _, v := range tt.args {
if err, ok := v.(error); ok {
if !errors.Is(got, err) {
t.Errorf("errors.Is(Joinf(errs...), err): must return true")
}
}
}
})
}
}
func TestJoinf_Unwrap_Go120(t *testing.T) {
err1 := Message("first error")
err2 := Message("second error")
got := Joinf("%w: %w", err1, err2)
unwrapper, ok := got.(interface{ Unwrap() error })
if !ok {
t.Fatalf("Join(err1, err2) must implement Unwrap()")
}
unwrapped := unwrapper.Unwrap()
if unwrapped == nil {
t.Fatalf("Join(err1, err2).Unwrap() must not return nil")
}
if errors.Is(unwrapped, err1) || !errors.Is(unwrapped, err2) {
t.Fatalf("Join(err1, err2).Unwrap() must return the second error")
}
}