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
|
package cleanups
import (
"context"
"errors"
"fmt"
"testing"
"gotest.tools/v3/assert"
is "gotest.tools/v3/assert/cmp"
)
func TestCall(t *testing.T) {
c := Composite{}
err1 := errors.New("error1")
err2 := errors.New("error2")
errX := errors.New("errorX")
errY := errors.New("errorY")
errZ := errors.New("errorZ")
errYZ := errors.Join(errY, errZ)
c.Add(func(ctx context.Context) error {
return err1
})
c.Add(func(ctx context.Context) error {
return nil
})
c.Add(func(ctx context.Context) error {
return fmt.Errorf("something happened: %w", err2)
})
c.Add(func(ctx context.Context) error {
return errors.Join(errX, fmt.Errorf("joined: %w", errYZ))
})
err := c.Call(context.Background())
errs := err.(interface{ Unwrap() []error }).Unwrap()
assert.Check(t, is.ErrorContains(err, err1.Error()))
assert.Check(t, is.ErrorContains(err, err2.Error()))
assert.Check(t, is.ErrorContains(err, errX.Error()))
assert.Check(t, is.ErrorContains(err, errY.Error()))
assert.Check(t, is.ErrorContains(err, errZ.Error()))
assert.Check(t, is.ErrorContains(err, "something happened: "+err2.Error()))
t.Log(err)
assert.Assert(t, is.Len(errs, 3))
// Cleanups executed in reverse order.
assert.Check(t, is.ErrorIs(errs[2], err1))
assert.Check(t, is.ErrorIs(errs[1], err2))
assert.Check(t, is.ErrorIs(errs[0], errX))
assert.Check(t, is.ErrorIs(errs[0], errYZ))
}
|