sq/libsq/core/cleanup/cleanup_test.go
Neil O'Toole 58ccfc9ded
Json driver; refactoring of core packages (#66)
* Type Detector refactor

* json driver impl; refactoring of source.Files reader et al

* working on kind detector

* significant switcheroo of packages

* partway throught refactoring Kind

* major package switcheroo for Kind
2020-08-23 04:42:15 -06:00

56 lines
929 B
Go

package cleanup_test
import (
"testing"
"github.com/stretchr/testify/require"
"github.com/neilotoole/sq/libsq/core/errz"
"github.com/neilotoole/sq/libsq/core/cleanup"
)
func TestCleanup(t *testing.T) {
want := []int{4, 3, 2, 1, 0}
var got []int
clnup := cleanup.New()
for i := 0; i < 5; i++ {
i := i
clnup.AddE(func() error {
got = append(got, i)
return nil
})
}
err := clnup.Run()
require.NoError(t, err)
require.Equal(t, want, got)
}
func TestCleanup_Error(t *testing.T) {
clnup := cleanup.New()
clnup.AddE(func() error {
return nil
})
clnup.AddE(func() error {
return errz.New("err1")
})
clnup.AddE(func() error {
return errz.New("err2")
})
err := clnup.Run()
require.Error(t, err)
require.Equal(t, "err2; err1", err.Error())
errs := errz.Errors(err)
require.Equal(t, 2, len(errs))
require.Equal(t, "err2", errs[0].Error())
require.Equal(t, "err1", errs[1].Error())
}