reflect_test.go 5.02 KB
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 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
package fluent_test

import (
	"testing"

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

	ipld "github.com/ipld/go-ipld-prime"
	"github.com/ipld/go-ipld-prime/fluent"
	"github.com/ipld/go-ipld-prime/must"
	basicnode "github.com/ipld/go-ipld-prime/node/basic"
)

func TestReflect(t *testing.T) {
	t.Run("Map", func(t *testing.T) {
		n, err := fluent.Reflect(basicnode.Prototype.Any, map[string]interface{}{
			"k1": "fine",
			"k2": "super",
			"k3": map[string]string{
				"k31": "thanks",
				"k32": "for",
				"k33": "asking",
			},
		})
		Wish(t, err, ShouldEqual, nil)
		Wish(t, n.ReprKind(), ShouldEqual, ipld.ReprKind_Map)
		t.Run("CorrectContents", func(t *testing.T) {
			Wish(t, n.Length(), ShouldEqual, 3)
			Wish(t, must.String(must.Node(n.LookupByString("k1"))), ShouldEqual, "fine")
			Wish(t, must.String(must.Node(n.LookupByString("k2"))), ShouldEqual, "super")
			n := must.Node(n.LookupByString("k3"))
			Wish(t, n.Length(), ShouldEqual, 3)
			Wish(t, must.String(must.Node(n.LookupByString("k31"))), ShouldEqual, "thanks")
			Wish(t, must.String(must.Node(n.LookupByString("k32"))), ShouldEqual, "for")
			Wish(t, must.String(must.Node(n.LookupByString("k33"))), ShouldEqual, "asking")
		})
		t.Run("CorrectOrder", func(t *testing.T) {
			itr := n.MapIterator()
			k, _, _ := itr.Next()
			Wish(t, must.String(k), ShouldEqual, "k1")
			k, _, _ = itr.Next()
			Wish(t, must.String(k), ShouldEqual, "k2")
			k, v, _ := itr.Next()
			Wish(t, must.String(k), ShouldEqual, "k3")
			itr = v.MapIterator()
			k, _, _ = itr.Next()
			Wish(t, must.String(k), ShouldEqual, "k31")
			k, _, _ = itr.Next()
			Wish(t, must.String(k), ShouldEqual, "k32")
			k, _, _ = itr.Next()
			Wish(t, must.String(k), ShouldEqual, "k33")
		})
	})
	t.Run("Struct", func(t *testing.T) {
		type Woo struct {
			A string
			B string
		}
		type Whee struct {
			X string
			Z string
			M Woo
		}
		n, err := fluent.Reflect(basicnode.Prototype.Any, Whee{
			X: "fine",
			Z: "super",
			M: Woo{"thanks", "really"},
		})
		Wish(t, err, ShouldEqual, nil)
		Wish(t, n.ReprKind(), ShouldEqual, ipld.ReprKind_Map)
		t.Run("CorrectContents", func(t *testing.T) {
			Wish(t, n.Length(), ShouldEqual, 3)
			Wish(t, must.String(must.Node(n.LookupByString("X"))), ShouldEqual, "fine")
			Wish(t, must.String(must.Node(n.LookupByString("Z"))), ShouldEqual, "super")
			n := must.Node(n.LookupByString("M"))
			Wish(t, n.Length(), ShouldEqual, 2)
			Wish(t, must.String(must.Node(n.LookupByString("A"))), ShouldEqual, "thanks")
			Wish(t, must.String(must.Node(n.LookupByString("B"))), ShouldEqual, "really")
		})
		t.Run("CorrectOrder", func(t *testing.T) {
			itr := n.MapIterator()
			k, _, _ := itr.Next()
			Wish(t, must.String(k), ShouldEqual, "X")
			k, _, _ = itr.Next()
			Wish(t, must.String(k), ShouldEqual, "Z")
			k, v, _ := itr.Next()
			Wish(t, must.String(k), ShouldEqual, "M")
			itr = v.MapIterator()
			k, _, _ = itr.Next()
			Wish(t, must.String(k), ShouldEqual, "A")
			k, _, _ = itr.Next()
			Wish(t, must.String(k), ShouldEqual, "B")
		})
	})
	t.Run("NamedString", func(t *testing.T) {
		type Foo string
		type Bar struct {
			Z Foo
		}
		n, err := fluent.Reflect(basicnode.Prototype.Any, Bar{"foo"})
		Wish(t, err, ShouldEqual, nil)
		Wish(t, n.ReprKind(), ShouldEqual, ipld.ReprKind_Map)
		Wish(t, must.String(must.Node(n.LookupByString("Z"))), ShouldEqual, "foo")
	})
	t.Run("Interface", func(t *testing.T) {
		type Zaz struct {
			Z interface{}
		}
		n, err := fluent.Reflect(basicnode.Prototype.Any, Zaz{map[string]interface{}{"wow": "wee"}})
		Wish(t, err, ShouldEqual, nil)
		Wish(t, n.ReprKind(), ShouldEqual, ipld.ReprKind_Map)
		n, err = n.LookupByString("Z")
		Wish(t, err, ShouldEqual, nil)
		Wish(t, n.ReprKind(), ShouldEqual, ipld.ReprKind_Map)
		Wish(t, must.String(must.Node(n.LookupByString("wow"))), ShouldEqual, "wee")
	})
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
	t.Run("Bytes", func(t *testing.T) {
		n, err := fluent.Reflect(basicnode.Prototype.Any, []byte{0x1, 0x2, 0x3})
		Wish(t, err, ShouldEqual, nil)
		Wish(t, n.ReprKind(), ShouldEqual, ipld.ReprKind_Bytes)
		b, err := n.AsBytes()
		Wish(t, err, ShouldEqual, nil)
		Wish(t, b, ShouldEqual, []byte{0x1, 0x2, 0x3})
	})
	t.Run("NamedBytes", func(t *testing.T) {
		type Foo []byte
		type Bar struct {
			Z Foo
		}
		n, err := fluent.Reflect(basicnode.Prototype.Any, Bar{[]byte{0x1, 0x2, 0x3}})
		Wish(t, err, ShouldEqual, nil)
		Wish(t, n.ReprKind(), ShouldEqual, ipld.ReprKind_Map)
		n, err = n.LookupByString("Z")
		Wish(t, err, ShouldEqual, nil)
		Wish(t, n.ReprKind(), ShouldEqual, ipld.ReprKind_Bytes)
		b, err := n.AsBytes()
		Wish(t, err, ShouldEqual, nil)
		Wish(t, b, ShouldEqual, []byte{0x1, 0x2, 0x3})
	})
	t.Run("InterfaceContainingBytes", func(t *testing.T) {
		type Zaz struct {
			Z interface{}
		}
		n, err := fluent.Reflect(basicnode.Prototype.Any, Zaz{[]byte{0x1, 0x2, 0x3}})
		Wish(t, err, ShouldEqual, nil)
		Wish(t, n.ReprKind(), ShouldEqual, ipld.ReprKind_Map)
		n, err = n.LookupByString("Z")
		Wish(t, err, ShouldEqual, nil)
		Wish(t, n.ReprKind(), ShouldEqual, ipld.ReprKind_Bytes)
		b, err := n.AsBytes()
		Wish(t, err, ShouldEqual, nil)
		Wish(t, b, ShouldEqual, []byte{0x1, 0x2, 0x3})
	})
154
}