testStructReprStringjoin_test.go 5.18 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
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"
)

// TestStructReprStringjoin exercises... well, what it says on the tin.
//
// These should pass even if the natural map representation doesn't.
// No maybes are exercised.
func TestStructReprStringjoin(t *testing.T) {
19 20
	t.Parallel()

21 22 23 24 25 26 27 28 29 30 31
	prefix := "structstrjoin"
	pkgName := "main"

	ts := schema.TypeSystem{}
	ts.Init()
	adjCfg := &AdjunctCfg{
		maybeUsesPtr: map[schema.TypeName]bool{},
	}
	ts.Accumulate(schema.SpawnString("String"))
	ts.Accumulate(schema.SpawnStruct("StringyStruct",
		[]schema.StructField{
32
			schema.SpawnStructField("field", "String", false, false),
33 34 35 36 37
		},
		schema.SpawnStructRepresentationStringjoin(":"),
	))
	ts.Accumulate(schema.SpawnStruct("ManystringStruct",
		[]schema.StructField{
38 39
			schema.SpawnStructField("foo", "String", false, false),
			schema.SpawnStructField("bar", "String", false, false),
40 41 42
		},
		schema.SpawnStructRepresentationStringjoin(":"),
	))
43 44
	ts.Accumulate(schema.SpawnStruct("Recurzorator",
		[]schema.StructField{
45 46 47
			schema.SpawnStructField("foo", "String", false, false),
			schema.SpawnStructField("zap", "ManystringStruct", false, false),
			schema.SpawnStructField("bar", "String", false, false),
48 49 50
		},
		schema.SpawnStructRepresentationStringjoin("-"),
	))
51

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

		t.Run("several fields work", func(t *testing.T) {
81 82
			np := getPrototypeByName("ManystringStruct")
			nrp := getPrototypeByName("ManystringStruct.Repr")
83 84
			var n schema.TypedNode
			t.Run("typed-create", func(t *testing.T) {
85
				n = fluent.MustBuildMap(np, 2, func(ma fluent.MapAssembler) {
86 87 88 89
					ma.AssembleEntry("foo").AssignString("v1")
					ma.AssembleEntry("bar").AssignString("v2")
				}).(schema.TypedNode)
				t.Run("typed-read", func(t *testing.T) {
90
					Require(t, n.Kind(), ShouldEqual, ipld.Kind_Map)
91
					Wish(t, n.Length(), ShouldEqual, int64(2))
92 93
					Wish(t, must.String(must.Node(n.LookupByString("foo"))), ShouldEqual, "v1")
					Wish(t, must.String(must.Node(n.LookupByString("bar"))), ShouldEqual, "v2")
94 95 96
				})
				t.Run("repr-read", func(t *testing.T) {
					nr := n.Representation()
97
					Require(t, nr.Kind(), ShouldEqual, ipld.Kind_String)
98 99 100 101
					Wish(t, must.String(nr), ShouldEqual, "v1:v2")
				})
			})
			t.Run("repr-create", func(t *testing.T) {
102
				nr := fluent.MustBuild(nrp, func(na fluent.NodeAssembler) {
103 104
					na.AssignString("v1:v2")
				})
105
				Wish(t, ipld.DeepEqual(n, nr), ShouldEqual, true)
106 107
			})
		})
108 109

		t.Run("nested stringjoin structs work", func(t *testing.T) {
110 111
			np := getPrototypeByName("Recurzorator")
			nrp := getPrototypeByName("Recurzorator.Repr")
112 113
			var n schema.TypedNode
			t.Run("typed-create", func(t *testing.T) {
114
				n = fluent.MustBuildMap(np, 3, func(ma fluent.MapAssembler) {
115 116 117 118 119 120 121 122
					ma.AssembleEntry("foo").AssignString("v1")
					ma.AssembleEntry("zap").CreateMap(2, func(ma fluent.MapAssembler) {
						ma.AssembleEntry("foo").AssignString("v2")
						ma.AssembleEntry("bar").AssignString("v3")
					})
					ma.AssembleEntry("bar").AssignString("v4")
				}).(schema.TypedNode)
				t.Run("typed-read", func(t *testing.T) {
123
					Require(t, n.Kind(), ShouldEqual, ipld.Kind_Map)
124
					Wish(t, n.Length(), ShouldEqual, int64(3))
125 126 127
					Wish(t, must.String(must.Node(n.LookupByString("foo"))), ShouldEqual, "v1")
					Wish(t, must.String(must.Node(n.LookupByString("bar"))), ShouldEqual, "v4")
					n2 := must.Node(n.LookupByString("zap"))
128
					Wish(t, n2.Length(), ShouldEqual, int64(2))
129 130
					Wish(t, must.String(must.Node(n2.LookupByString("foo"))), ShouldEqual, "v2")
					Wish(t, must.String(must.Node(n2.LookupByString("bar"))), ShouldEqual, "v3")
131 132 133
				})
				t.Run("repr-read", func(t *testing.T) {
					nr := n.Representation()
134
					Require(t, nr.Kind(), ShouldEqual, ipld.Kind_String)
135 136 137 138
					Wish(t, must.String(nr), ShouldEqual, "v1-v2:v3-v4")
				})
			})
			t.Run("repr-create", func(t *testing.T) {
139
				nr := fluent.MustBuild(nrp, func(na fluent.NodeAssembler) {
140 141
					na.AssignString("v1-v2:v3-v4")
				})
142
				Wish(t, ipld.DeepEqual(n, nr), ShouldEqual, true)
143 144
			})
		})
145 146
	})
}