github.com/ipld/go-ipld-prime@v0.21.0/fluent/fluentRecover_test.go (about)

     1  package fluent_test
     2  
     3  import (
     4  	"testing"
     5  
     6  	qt "github.com/frankban/quicktest"
     7  
     8  	"github.com/ipld/go-ipld-prime/datamodel"
     9  	"github.com/ipld/go-ipld-prime/fluent"
    10  	"github.com/ipld/go-ipld-prime/node/basicnode"
    11  )
    12  
    13  func TestRecover(t *testing.T) {
    14  	t.Run("simple build error should capture", func(t *testing.T) {
    15  		qt.Check(t,
    16  			fluent.Recover(func() {
    17  				fluent.MustBuild(basicnode.Prototype__String{}, func(fna fluent.NodeAssembler) {
    18  					fna.AssignInt(9)
    19  				})
    20  				t.Fatal("should not be reached")
    21  			}),
    22  			qt.DeepEquals,
    23  			fluent.Error{datamodel.ErrWrongKind{TypeName: "string", MethodName: "AssignInt", AppropriateKind: datamodel.KindSet_JustInt, ActualKind: datamodel.Kind_String}},
    24  		)
    25  	})
    26  	t.Run("correct build should return nil", func(t *testing.T) {
    27  		qt.Check(t,
    28  			fluent.Recover(func() {
    29  				fluent.MustBuild(basicnode.Prototype__String{}, func(fna fluent.NodeAssembler) {
    30  					fna.AssignString("fine")
    31  				})
    32  			}),
    33  			qt.IsNil,
    34  		)
    35  	})
    36  	t.Run("other panics should continue to rise", func(t *testing.T) {
    37  		qt.Check(t,
    38  			func() (r interface{}) {
    39  				defer func() { r = recover() }()
    40  				fluent.Recover(func() {
    41  					panic("fuqawds")
    42  				})
    43  				return
    44  			}(),
    45  			qt.Equals,
    46  			"fuqawds",
    47  		)
    48  	})
    49  }