github.com/ipld/go-ipld-prime@v0.21.0/fluent/reflect_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/must"
    11  	"github.com/ipld/go-ipld-prime/node/basicnode"
    12  )
    13  
    14  func TestReflect(t *testing.T) {
    15  	t.Run("Map", func(t *testing.T) {
    16  		n, err := fluent.Reflect(basicnode.Prototype.Any, map[string]interface{}{
    17  			"k1": "fine",
    18  			"k2": "super",
    19  			"k3": map[string]string{
    20  				"k31": "thanks",
    21  				"k32": "for",
    22  				"k33": "asking",
    23  			},
    24  		})
    25  		qt.Check(t, err, qt.IsNil)
    26  		qt.Check(t, n.Kind(), qt.Equals, datamodel.Kind_Map)
    27  		t.Run("CorrectContents", func(t *testing.T) {
    28  			qt.Check(t, n.Length(), qt.Equals, int64(3))
    29  			qt.Check(t, must.String(must.Node(n.LookupByString("k1"))), qt.Equals, "fine")
    30  			qt.Check(t, must.String(must.Node(n.LookupByString("k2"))), qt.Equals, "super")
    31  			n := must.Node(n.LookupByString("k3"))
    32  			qt.Check(t, n.Length(), qt.Equals, int64(3))
    33  			qt.Check(t, must.String(must.Node(n.LookupByString("k31"))), qt.Equals, "thanks")
    34  			qt.Check(t, must.String(must.Node(n.LookupByString("k32"))), qt.Equals, "for")
    35  			qt.Check(t, must.String(must.Node(n.LookupByString("k33"))), qt.Equals, "asking")
    36  		})
    37  		t.Run("CorrectOrder", func(t *testing.T) {
    38  			itr := n.MapIterator()
    39  			k, _, _ := itr.Next()
    40  			qt.Check(t, must.String(k), qt.Equals, "k1")
    41  			k, _, _ = itr.Next()
    42  			qt.Check(t, must.String(k), qt.Equals, "k2")
    43  			k, v, _ := itr.Next()
    44  			qt.Check(t, must.String(k), qt.Equals, "k3")
    45  			itr = v.MapIterator()
    46  			k, _, _ = itr.Next()
    47  			qt.Check(t, must.String(k), qt.Equals, "k31")
    48  			k, _, _ = itr.Next()
    49  			qt.Check(t, must.String(k), qt.Equals, "k32")
    50  			k, _, _ = itr.Next()
    51  			qt.Check(t, must.String(k), qt.Equals, "k33")
    52  		})
    53  	})
    54  	t.Run("Struct", func(t *testing.T) {
    55  		type Woo struct {
    56  			A string
    57  			B string
    58  		}
    59  		type Whee struct {
    60  			X string
    61  			Z string
    62  			M Woo
    63  		}
    64  		n, err := fluent.Reflect(basicnode.Prototype.Any, Whee{
    65  			X: "fine",
    66  			Z: "super",
    67  			M: Woo{"thanks", "really"},
    68  		})
    69  		qt.Check(t, err, qt.IsNil)
    70  		qt.Check(t, n.Kind(), qt.Equals, datamodel.Kind_Map)
    71  		t.Run("CorrectContents", func(t *testing.T) {
    72  			qt.Check(t, n.Length(), qt.Equals, int64(3))
    73  			qt.Check(t, must.String(must.Node(n.LookupByString("X"))), qt.Equals, "fine")
    74  			qt.Check(t, must.String(must.Node(n.LookupByString("Z"))), qt.Equals, "super")
    75  			n := must.Node(n.LookupByString("M"))
    76  			qt.Check(t, n.Length(), qt.Equals, int64(2))
    77  			qt.Check(t, must.String(must.Node(n.LookupByString("A"))), qt.Equals, "thanks")
    78  			qt.Check(t, must.String(must.Node(n.LookupByString("B"))), qt.Equals, "really")
    79  		})
    80  		t.Run("CorrectOrder", func(t *testing.T) {
    81  			itr := n.MapIterator()
    82  			k, _, _ := itr.Next()
    83  			qt.Check(t, must.String(k), qt.Equals, "X")
    84  			k, _, _ = itr.Next()
    85  			qt.Check(t, must.String(k), qt.Equals, "Z")
    86  			k, v, _ := itr.Next()
    87  			qt.Check(t, must.String(k), qt.Equals, "M")
    88  			itr = v.MapIterator()
    89  			k, _, _ = itr.Next()
    90  			qt.Check(t, must.String(k), qt.Equals, "A")
    91  			k, _, _ = itr.Next()
    92  			qt.Check(t, must.String(k), qt.Equals, "B")
    93  		})
    94  	})
    95  	t.Run("NamedString", func(t *testing.T) {
    96  		type Foo string
    97  		type Bar struct {
    98  			Z Foo
    99  		}
   100  		n, err := fluent.Reflect(basicnode.Prototype.Any, Bar{"foo"})
   101  		qt.Check(t, err, qt.IsNil)
   102  		qt.Check(t, n.Kind(), qt.Equals, datamodel.Kind_Map)
   103  		qt.Check(t, must.String(must.Node(n.LookupByString("Z"))), qt.Equals, "foo")
   104  	})
   105  	t.Run("Interface", func(t *testing.T) {
   106  		type Zaz struct {
   107  			Z interface{}
   108  		}
   109  		n, err := fluent.Reflect(basicnode.Prototype.Any, Zaz{map[string]interface{}{"wow": "wee"}})
   110  		qt.Check(t, err, qt.IsNil)
   111  		qt.Check(t, n.Kind(), qt.Equals, datamodel.Kind_Map)
   112  		n, err = n.LookupByString("Z")
   113  		qt.Check(t, err, qt.IsNil)
   114  		qt.Check(t, n.Kind(), qt.Equals, datamodel.Kind_Map)
   115  		qt.Check(t, must.String(must.Node(n.LookupByString("wow"))), qt.Equals, "wee")
   116  	})
   117  	t.Run("Bytes", func(t *testing.T) {
   118  		n, err := fluent.Reflect(basicnode.Prototype.Any, []byte{0x1, 0x2, 0x3})
   119  		qt.Check(t, err, qt.IsNil)
   120  		qt.Check(t, n.Kind(), qt.Equals, datamodel.Kind_Bytes)
   121  		b, err := n.AsBytes()
   122  		qt.Check(t, err, qt.IsNil)
   123  		qt.Check(t, b, qt.DeepEquals, []byte{0x1, 0x2, 0x3})
   124  	})
   125  	t.Run("NamedBytes", func(t *testing.T) {
   126  		type Foo []byte
   127  		type Bar struct {
   128  			Z Foo
   129  		}
   130  		n, err := fluent.Reflect(basicnode.Prototype.Any, Bar{[]byte{0x1, 0x2, 0x3}})
   131  		qt.Check(t, err, qt.IsNil)
   132  		qt.Check(t, n.Kind(), qt.Equals, datamodel.Kind_Map)
   133  		n, err = n.LookupByString("Z")
   134  		qt.Check(t, err, qt.IsNil)
   135  		qt.Check(t, n.Kind(), qt.Equals, datamodel.Kind_Bytes)
   136  		b, err := n.AsBytes()
   137  		qt.Check(t, err, qt.IsNil)
   138  		qt.Check(t, b, qt.DeepEquals, []byte{0x1, 0x2, 0x3})
   139  	})
   140  	t.Run("InterfaceContainingBytes", func(t *testing.T) {
   141  		type Zaz struct {
   142  			Z interface{}
   143  		}
   144  		n, err := fluent.Reflect(basicnode.Prototype.Any, Zaz{[]byte{0x1, 0x2, 0x3}})
   145  		qt.Check(t, err, qt.IsNil)
   146  		qt.Check(t, n.Kind(), qt.Equals, datamodel.Kind_Map)
   147  		n, err = n.LookupByString("Z")
   148  		qt.Check(t, err, qt.IsNil)
   149  		qt.Check(t, n.Kind(), qt.Equals, datamodel.Kind_Bytes)
   150  		b, err := n.AsBytes()
   151  		qt.Check(t, err, qt.IsNil)
   152  		qt.Check(t, b, qt.DeepEquals, []byte{0x1, 0x2, 0x3})
   153  	})
   154  }