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

     1  package fluent_test
     2  
     3  import (
     4  	"testing"
     5  
     6  	qt "github.com/frankban/quicktest"
     7  	"github.com/ipfs/go-cid"
     8  	"github.com/ipld/go-ipld-prime/fluent"
     9  	cidlink "github.com/ipld/go-ipld-prime/linking/cid"
    10  	"github.com/ipld/go-ipld-prime/node/basicnode"
    11  )
    12  
    13  var roundTripTestCases = []struct {
    14  	name  string
    15  	value interface{}
    16  }{
    17  	{name: "Number", value: int64(100)},
    18  	{name: "String", value: "hi"},
    19  	{name: "Bool", value: true},
    20  	{name: "Bytes", value: []byte("hi")},
    21  	{name: "Map", value: map[string]interface{}{"a": "1", "b": int64(2), "c": 3.14, "d": true}},
    22  	{name: "Array", value: []interface{}{"a", "b", "c"}},
    23  	{name: "Nil", value: nil},
    24  }
    25  
    26  func TestRoundTrip(t *testing.T) {
    27  	for _, testCase := range roundTripTestCases {
    28  		t.Run(testCase.name, func(t *testing.T) {
    29  			c := qt.New(t)
    30  			n, err := fluent.Reflect(basicnode.Prototype.Any, testCase.value)
    31  			c.Assert(err, qt.IsNil)
    32  			out, err := fluent.ToInterface(n)
    33  			c.Assert(err, qt.IsNil)
    34  			c.Check(out, qt.DeepEquals, testCase.value)
    35  		})
    36  	}
    37  }
    38  
    39  func TestLink(t *testing.T) {
    40  	c := qt.New(t)
    41  	someCid, err := cid.Parse("bafybeihrqe2hmfauph5yfbd6ucv7njqpiy4tvbewlvhzjl4bhnyiu6h7pm")
    42  	c.Assert(err, qt.IsNil)
    43  	link := cidlink.Link{Cid: someCid}
    44  	v, err := fluent.ToInterface(basicnode.NewLink(link))
    45  	c.Assert(err, qt.IsNil)
    46  	c.Assert(v.(cidlink.Link), qt.Equals, link)
    47  }