2022-11-30 06:10:52 +08:00
|
|
|
package errutil
|
2020-04-02 13:03:51 +08:00
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
2021-05-22 12:14:37 +08:00
|
|
|
var (
|
|
|
|
err1 = errors.New("error 1")
|
|
|
|
err2 = errors.New("error 2")
|
|
|
|
err3 = errors.New("error 3")
|
|
|
|
)
|
|
|
|
|
2020-04-02 13:03:51 +08:00
|
|
|
var errorsTests = []struct {
|
|
|
|
e error
|
|
|
|
wantString string
|
|
|
|
}{
|
2022-11-30 06:10:52 +08:00
|
|
|
{Multi(), ""},
|
|
|
|
{Multi(errors.New("some error")), "some error"},
|
2020-04-02 13:03:51 +08:00
|
|
|
{
|
2022-11-30 06:10:52 +08:00
|
|
|
Multi(err1, err2),
|
2020-04-02 13:03:51 +08:00
|
|
|
"multiple errors: error 1; error 2",
|
|
|
|
},
|
|
|
|
{
|
2022-11-30 06:10:52 +08:00
|
|
|
Multi(err1, err2, err3),
|
2020-04-02 13:03:51 +08:00
|
|
|
"multiple errors: error 1; error 2; error 3",
|
|
|
|
},
|
|
|
|
{
|
2022-11-30 06:10:52 +08:00
|
|
|
Multi(err1, Multi(err2, err3)),
|
2020-04-02 13:03:51 +08:00
|
|
|
"multiple errors: error 1; error 2; error 3",
|
|
|
|
},
|
|
|
|
{
|
2022-11-30 06:10:52 +08:00
|
|
|
Multi(Multi(err1, err2), err3),
|
2020-04-02 13:03:51 +08:00
|
|
|
"multiple errors: error 1; error 2; error 3",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestErrors(t *testing.T) {
|
|
|
|
for _, test := range errorsTests {
|
|
|
|
if test.e == nil {
|
|
|
|
if test.wantString != "" {
|
|
|
|
t.Errorf("got nil, want %q", test.wantString)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
gotString := test.e.Error()
|
|
|
|
if gotString != test.wantString {
|
|
|
|
t.Errorf("got %q, want %q", gotString, test.wantString)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|