openmeter / pkg /errorsx /errorsx_test.go
Leon4gr45's picture
Upload folder using huggingface_hub (part 9)
fea99b3 verified
Raw
History Blame Contribute Delete
1.19 kB
package errorsx
import (
"errors"
"fmt"
"testing"
"github.com/stretchr/testify/assert"
)
func TestWithPrefix(t *testing.T) {
const prefix = "prefix"
tests := []struct {
name string
err error
expected string
}{
{
name: "single error",
err: errors.New("error"),
expected: "prefix: error",
},
{
name: "multiple errors",
err: errors.Join(
errors.New("error 1"),
errors.New("error 2"),
),
expected: "prefix: error 1\nprefix: error 2",
},
{
name: "multiple errors (not top-level)",
err: fmt.Errorf("%w", errors.Join(
errors.New("error 1"),
errors.New("error 2"),
)),
expected: "prefix: error 1\nerror 2",
},
{
name: "multiple errors",
err: errors.Join(
WithPrefix(
errors.Join(
errors.New("error 1.1"),
errors.New("error 1.2"),
),
"subprefix",
),
errors.New("error 2"),
),
expected: "prefix: subprefix: error 1.1\nprefix: subprefix: error 1.2\nprefix: error 2",
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
actual := WithPrefix(test.err, prefix).Error()
assert.Equal(t, test.expected, actual)
})
}
}