testStructReprTuple_test.go 5.39 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
package gengo

import (
	"testing"

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

	"github.com/ipld/go-ipld-prime"
	"github.com/ipld/go-ipld-prime/fluent"
	"github.com/ipld/go-ipld-prime/must"
	"github.com/ipld/go-ipld-prime/schema"
)

func TestStructReprTuple(t *testing.T) {
15 16
	t.Parallel()

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
	prefix := "structtuple"
	pkgName := "main"

	ts := schema.TypeSystem{}
	ts.Init()
	adjCfg := &AdjunctCfg{
		maybeUsesPtr: map[schema.TypeName]bool{},
	}
	ts.Accumulate(schema.SpawnString("String"))
	ts.Accumulate(schema.SpawnStruct("OneTuple",
		[]schema.StructField{
			schema.SpawnStructField("field", "String", false, false),
		},
		schema.SpawnStructRepresentationTuple(),
	))
	ts.Accumulate(schema.SpawnStruct("FourTuple",
		[]schema.StructField{
			schema.SpawnStructField("foo", "String", false, false),
			schema.SpawnStructField("bar", "String", false, true),
			schema.SpawnStructField("baz", "String", true, true),
			schema.SpawnStructField("qux", "String", true, false),
		},
		schema.SpawnStructRepresentationTuple(),
	))

	genAndCompileAndTest(t, prefix, pkgName, ts, adjCfg, func(t *testing.T, getPrototypeByName func(string) ipld.NodePrototype) {
		t.Run("onetuple works", func(t *testing.T) {
			np := getPrototypeByName("OneTuple")
			nrp := getPrototypeByName("OneTuple.Repr")
			var n schema.TypedNode
			t.Run("typed-create", func(t *testing.T) {
				n = fluent.MustBuildMap(np, 1, func(ma fluent.MapAssembler) {
					ma.AssembleEntry("field").AssignString("valoo")
				}).(schema.TypedNode)
				t.Run("typed-read", func(t *testing.T) {
52
					Require(t, n.Kind(), ShouldEqual, ipld.Kind_Map)
53
					Wish(t, n.Length(), ShouldEqual, int64(1))
54 55 56 57
					Wish(t, must.String(must.Node(n.LookupByString("field"))), ShouldEqual, "valoo")
				})
				t.Run("repr-read", func(t *testing.T) {
					nr := n.Representation()
58
					Require(t, nr.Kind(), ShouldEqual, ipld.Kind_List)
59
					Wish(t, nr.Length(), ShouldEqual, int64(1))
60 61 62 63 64 65 66
					Wish(t, must.String(must.Node(nr.LookupByIndex(0))), ShouldEqual, "valoo")
				})
			})
			t.Run("repr-create", func(t *testing.T) {
				nr := fluent.MustBuildList(nrp, 1, func(la fluent.ListAssembler) {
					la.AssembleValue().AssignString("valoo")
				})
67
				Wish(t, ipld.DeepEqual(n, nr), ShouldEqual, true)
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
			})
		})

		t.Run("fourtuple works", func(t *testing.T) {
			np := getPrototypeByName("FourTuple")
			nrp := getPrototypeByName("FourTuple.Repr")
			var n schema.TypedNode
			t.Run("typed-create", func(t *testing.T) {
				n = fluent.MustBuildMap(np, 4, func(ma fluent.MapAssembler) {
					ma.AssembleEntry("foo").AssignString("0")
					ma.AssembleEntry("bar").AssignString("1")
					ma.AssembleEntry("baz").AssignString("2")
					ma.AssembleEntry("qux").AssignString("3")
				}).(schema.TypedNode)
				t.Run("typed-read", func(t *testing.T) {
83
					Require(t, n.Kind(), ShouldEqual, ipld.Kind_Map)
84
					Wish(t, n.Length(), ShouldEqual, int64(4))
85 86 87 88 89 90 91
					Wish(t, must.String(must.Node(n.LookupByString("foo"))), ShouldEqual, "0")
					Wish(t, must.String(must.Node(n.LookupByString("bar"))), ShouldEqual, "1")
					Wish(t, must.String(must.Node(n.LookupByString("baz"))), ShouldEqual, "2")
					Wish(t, must.String(must.Node(n.LookupByString("qux"))), ShouldEqual, "3")
				})
				t.Run("repr-read", func(t *testing.T) {
					nr := n.Representation()
92
					Require(t, nr.Kind(), ShouldEqual, ipld.Kind_List)
93
					Wish(t, nr.Length(), ShouldEqual, int64(4))
94 95 96 97 98 99 100 101 102 103 104 105 106
					Wish(t, must.String(must.Node(nr.LookupByIndex(0))), ShouldEqual, "0")
					Wish(t, must.String(must.Node(nr.LookupByIndex(1))), ShouldEqual, "1")
					Wish(t, must.String(must.Node(nr.LookupByIndex(2))), ShouldEqual, "2")
					Wish(t, must.String(must.Node(nr.LookupByIndex(3))), ShouldEqual, "3")
				})
			})
			t.Run("repr-create", func(t *testing.T) {
				nr := fluent.MustBuildList(nrp, 4, func(la fluent.ListAssembler) {
					la.AssembleValue().AssignString("0")
					la.AssembleValue().AssignString("1")
					la.AssembleValue().AssignString("2")
					la.AssembleValue().AssignString("3")
				})
107
				Wish(t, ipld.DeepEqual(n, nr), ShouldEqual, true)
108 109 110
			})
		})

111 112 113 114 115 116 117 118 119 120
		t.Run("fourtuple with absents", func(t *testing.T) {
			np := getPrototypeByName("FourTuple")
			nrp := getPrototypeByName("FourTuple.Repr")
			var n schema.TypedNode
			t.Run("typed-create", func(t *testing.T) {
				n = fluent.MustBuildMap(np, 2, func(ma fluent.MapAssembler) {
					ma.AssembleEntry("foo").AssignString("0")
					ma.AssembleEntry("bar").AssignNull()
				}).(schema.TypedNode)
				t.Run("typed-read", func(t *testing.T) {
121
					Require(t, n.Kind(), ShouldEqual, ipld.Kind_Map)
122
					Wish(t, n.Length(), ShouldEqual, int64(4))
123 124 125 126 127 128 129
					Wish(t, must.String(must.Node(n.LookupByString("foo"))), ShouldEqual, "0")
					Wish(t, must.Node(n.LookupByString("bar")), ShouldEqual, ipld.Null)
					Wish(t, must.Node(n.LookupByString("baz")), ShouldEqual, ipld.Absent)
					Wish(t, must.Node(n.LookupByString("qux")), ShouldEqual, ipld.Absent)
				})
				t.Run("repr-read", func(t *testing.T) {
					nr := n.Representation()
130
					Require(t, nr.Kind(), ShouldEqual, ipld.Kind_List)
131
					Wish(t, nr.Length(), ShouldEqual, int64(2))
132 133 134 135 136 137 138 139 140
					Wish(t, must.String(must.Node(nr.LookupByIndex(0))), ShouldEqual, "0")
					Wish(t, must.Node(nr.LookupByIndex(1)), ShouldEqual, ipld.Null)
				})
			})
			t.Run("repr-create", func(t *testing.T) {
				nr := fluent.MustBuildList(nrp, 4, func(la fluent.ListAssembler) {
					la.AssembleValue().AssignString("0")
					la.AssembleValue().AssignNull()
				})
141
				Wish(t, ipld.DeepEqual(n, nr), ShouldEqual, true)
142 143
			})
		})
144 145
	})
}