testStructReprStringjoin_test.go 5.17 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
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) {
	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{
			schema.SpawnStructField("field", ts.TypeByName("String"), false, false),
		},
		schema.SpawnStructRepresentationStringjoin(":"),
	))
	ts.Accumulate(schema.SpawnStruct("ManystringStruct",
		[]schema.StructField{
			schema.SpawnStructField("foo", ts.TypeByName("String"), false, false),
			schema.SpawnStructField("bar", ts.TypeByName("String"), false, false),
		},
		schema.SpawnStructRepresentationStringjoin(":"),
	))
41 42 43 44 45 46 47 48
	ts.Accumulate(schema.SpawnStruct("Recurzorator",
		[]schema.StructField{
			schema.SpawnStructField("foo", ts.TypeByName("String"), false, false),
			schema.SpawnStructField("zap", ts.TypeByName("ManystringStruct"), false, false),
			schema.SpawnStructField("bar", ts.TypeByName("String"), false, false),
		},
		schema.SpawnStructRepresentationStringjoin("-"),
	))
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

	genAndCompileAndTest(t, prefix, pkgName, ts, adjCfg, func(t *testing.T, getStyleByName func(string) ipld.NodeStyle) {
		t.Run("single field works", func(t *testing.T) {
			ns := getStyleByName("StringyStruct")
			nsr := getStyleByName("StringyStruct.Repr")
			var n schema.TypedNode
			t.Run("typed-create", func(t *testing.T) {
				n = fluent.MustBuildMap(ns, 1, func(ma fluent.MapAssembler) {
					ma.AssembleEntry("field").AssignString("valoo")
				}).(schema.TypedNode)
				t.Run("typed-read", func(t *testing.T) {
					Require(t, n.ReprKind(), ShouldEqual, ipld.ReprKind_Map)
					Wish(t, n.Length(), ShouldEqual, 1)
					Wish(t, must.String(must.Node(n.LookupString("field"))), ShouldEqual, "valoo")
				})
				t.Run("repr-read", func(t *testing.T) {
					nr := n.Representation()
					Require(t, nr.ReprKind(), ShouldEqual, ipld.ReprKind_String)
					Wish(t, must.String(nr), ShouldEqual, "valoo")
				})
			})
			t.Run("repr-create", func(t *testing.T) {
				nr := fluent.MustBuild(nsr, func(na fluent.NodeAssembler) {
					na.AssignString("valoo")
				})
				Wish(t, n, ShouldEqual, nr)
			})
		})

		t.Run("several fields work", func(t *testing.T) {
			ns := getStyleByName("ManystringStruct")
			nsr := getStyleByName("ManystringStruct.Repr")
			var n schema.TypedNode
			t.Run("typed-create", func(t *testing.T) {
				n = fluent.MustBuildMap(ns, 2, func(ma fluent.MapAssembler) {
					ma.AssembleEntry("foo").AssignString("v1")
					ma.AssembleEntry("bar").AssignString("v2")
				}).(schema.TypedNode)
				t.Run("typed-read", func(t *testing.T) {
					Require(t, n.ReprKind(), ShouldEqual, ipld.ReprKind_Map)
					Wish(t, n.Length(), ShouldEqual, 2)
					Wish(t, must.String(must.Node(n.LookupString("foo"))), ShouldEqual, "v1")
					Wish(t, must.String(must.Node(n.LookupString("bar"))), ShouldEqual, "v2")
				})
				t.Run("repr-read", func(t *testing.T) {
					nr := n.Representation()
					Require(t, nr.ReprKind(), ShouldEqual, ipld.ReprKind_String)
					Wish(t, must.String(nr), ShouldEqual, "v1:v2")
				})
			})
			t.Run("repr-create", func(t *testing.T) {
				nr := fluent.MustBuild(nsr, func(na fluent.NodeAssembler) {
					na.AssignString("v1:v2")
				})
				Wish(t, n, ShouldEqual, nr)
			})
		})
106 107 108 109 110 111 112 113 114 115 116 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

		t.Run("nested stringjoin structs work", func(t *testing.T) {
			ns := getStyleByName("Recurzorator")
			nsr := getStyleByName("Recurzorator.Repr")
			var n schema.TypedNode
			t.Run("typed-create", func(t *testing.T) {
				n = fluent.MustBuildMap(ns, 3, func(ma fluent.MapAssembler) {
					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) {
					Require(t, n.ReprKind(), ShouldEqual, ipld.ReprKind_Map)
					Wish(t, n.Length(), ShouldEqual, 3)
					Wish(t, must.String(must.Node(n.LookupString("foo"))), ShouldEqual, "v1")
					Wish(t, must.String(must.Node(n.LookupString("bar"))), ShouldEqual, "v4")
					n2 := must.Node(n.LookupString("zap"))
					Wish(t, n2.Length(), ShouldEqual, 2)
					Wish(t, must.String(must.Node(n2.LookupString("foo"))), ShouldEqual, "v2")
					Wish(t, must.String(must.Node(n2.LookupString("bar"))), ShouldEqual, "v3")
				})
				t.Run("repr-read", func(t *testing.T) {
					nr := n.Representation()
					Require(t, nr.ReprKind(), ShouldEqual, ipld.ReprKind_String)
					Wish(t, must.String(nr), ShouldEqual, "v1-v2:v3-v4")
				})
			})
			t.Run("repr-create", func(t *testing.T) {
				nr := fluent.MustBuild(nsr, func(na fluent.NodeAssembler) {
					na.AssignString("v1-v2:v3-v4")
				})
				Wish(t, n, ShouldEqual, nr)
			})
		})
143 144
	})
}