fluentRecover_test.go 919 Bytes
Newer Older
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
package fluent

import (
	"fmt"
	"testing"

	. "github.com/warpfork/go-wish"

	"github.com/ipld/go-ipld-prime/impl/free"
)

func TestRecover(t *testing.T) {
	t.Run("simple traversal error should capture", func(t *testing.T) {
		Wish(t,
			Recover(func() {
				WrapNode(&ipldfree.Node{}).TraverseIndex(0).AsString()
				t.Fatal("should not be reached")
			}),
			ShouldEqual,
			Error{fmt.Errorf("cannot traverse a node that is undefined")},
		)
	})
	t.Run("correct traversal should return nil", func(t *testing.T) {
		Wish(t,
			Recover(func() {
Eric Myhre's avatar
Eric Myhre committed
26
				n, _ := ipldfree.NodeBuilder().CreateString("foo")
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
				WrapNode(n).AsString()
			}),
			ShouldEqual,
			nil,
		)
	})
	t.Run("other panics should continue to rise", func(t *testing.T) {
		Wish(t,
			func() (r interface{}) {
				defer func() { r = recover() }()
				Recover(func() {
					panic("fuqawds")
				})
				return
			}(),
			ShouldEqual,
			"fuqawds",
		)
	})
}