forked from data-preservation-programs/singularity
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patherrors_test.go
71 lines (63 loc) · 1.68 KB
/
errors_test.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
70
71
package util
import (
"fmt"
"testing"
"github.com/cockroachdb/errors"
"github.com/stretchr/testify/require"
)
func TestAggregateError(t *testing.T) {
tests := []struct {
name string
errors []error
expected string
}{
{
name: "multiple errors",
errors: []error{errors.New("error1"), errors.New("error2"), errors.New("error3")},
expected: "error1, error2, error3",
},
{
name: "single error",
errors: []error{errors.New("only error")},
expected: "only error",
},
{
name: "no errors",
errors: []error{},
expected: "",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
aggErr := AggregateError{Errors: tt.errors}
if aggErr.Error() != tt.expected {
t.Errorf("got %q, want %q", aggErr.Error(), tt.expected)
}
})
}
}
type TestError struct {
msg string
}
func (e TestError) Error() string {
return e.msg
}
func TestAggregateErrorOthers(t *testing.T) {
err1 := errors.New("error1")
err2 := errors.New("error2")
err3 := errors.New("error3")
testError := TestError{msg: "test error"}
agg := AggregateError{Errors: []error{err1, err2, err3}}
err := agg.Unwrap()
require.ErrorContains(t, err, "error3")
require.True(t, errors.Is(err, err3))
require.True(t, errors.As(err, &err3))
require.Equal(t, "error1, error2, error3", fmt.Sprintf("%v", agg))
require.Contains(t, fmt.Sprintf("%+v", agg), "error1, error2, error3\n")
require.Equal(t, "error1, error2, error3", fmt.Sprintf("%s", agg))
require.Equal(t, "\"error1, error2, error3\"", fmt.Sprintf("%q", agg))
require.True(t, agg.Is(err1))
require.True(t, agg.As(&err1))
require.False(t, agg.Is(testError))
require.False(t, agg.As(&testError))
}