testStruct_test.go 4.32 KB
Newer Older
Eric Myhre's avatar
Eric Myhre committed
1 2 3 4 5 6 7 8 9 10 11 12 13
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"
)

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
func TestRequiredFields(t *testing.T) {
	t.Parallel()

	ts := schema.TypeSystem{}
	ts.Init()
	adjCfg := &AdjunctCfg{}
	ts.Accumulate(schema.SpawnString("String"))
	ts.Accumulate(schema.SpawnStruct("StructOne",
		[]schema.StructField{
			schema.SpawnStructField("a", "String", false, false),
			schema.SpawnStructField("b", "String", false, false),
		},
		schema.SpawnStructRepresentationMap(map[string]string{
			// no renames.  we expect a simpler error message in this case.
		}),
	))
	ts.Accumulate(schema.SpawnStruct("StructTwo",
		[]schema.StructField{
			schema.SpawnStructField("a", "String", false, false),
			schema.SpawnStructField("b", "String", false, false),
		},
		schema.SpawnStructRepresentationMap(map[string]string{
			"b": "z",
		}),
	))

	prefix := "struct-required-fields"
	pkgName := "main"
	genAndCompileAndTest(t, prefix, pkgName, ts, adjCfg, func(t *testing.T, getPrototypeByName func(string) ipld.NodePrototype) {
		t.Run("building-type-without-required-fields-errors", func(t *testing.T) {
			np := getPrototypeByName("StructOne")

			nb := np.NewBuilder()
			ma, _ := nb.BeginMap(0)
			err := ma.Finish()

			Wish(t, err, ShouldBeSameTypeAs, ipld.ErrMissingRequiredField{})
			Wish(t, err.Error(), ShouldEqual, `missing required fields: a,b`)
		})
		t.Run("building-representation-without-required-fields-errors", func(t *testing.T) {
			nrp := getPrototypeByName("StructOne.Repr")

			nb := nrp.NewBuilder()
			ma, _ := nb.BeginMap(0)
			err := ma.Finish()

			Wish(t, err, ShouldBeSameTypeAs, ipld.ErrMissingRequiredField{})
			Wish(t, err.Error(), ShouldEqual, `missing required fields: a,b`)
		})
		t.Run("building-representation-with-renames-without-required-fields-errors", func(t *testing.T) {
			nrp := getPrototypeByName("StructTwo.Repr")

			nb := nrp.NewBuilder()
			ma, _ := nb.BeginMap(0)
			err := ma.Finish()

			Wish(t, err, ShouldBeSameTypeAs, ipld.ErrMissingRequiredField{})
			Wish(t, err.Error(), ShouldEqual, `missing required fields: a,b (serial:"z")`)
		})
	})
}

Eric Myhre's avatar
Eric Myhre committed
76
func TestStructNesting(t *testing.T) {
77 78
	t.Parallel()

Eric Myhre's avatar
Eric Myhre committed
79 80 81 82 83 84 85 86
	ts := schema.TypeSystem{}
	ts.Init()
	adjCfg := &AdjunctCfg{
		maybeUsesPtr: map[schema.TypeName]bool{},
	}
	ts.Accumulate(schema.SpawnString("String"))
	ts.Accumulate(schema.SpawnStruct("SmolStruct",
		[]schema.StructField{
87
			schema.SpawnStructField("s", "String", false, false),
Eric Myhre's avatar
Eric Myhre committed
88 89 90 91 92 93 94
		},
		schema.SpawnStructRepresentationMap(map[string]string{
			"s": "q",
		}),
	))
	ts.Accumulate(schema.SpawnStruct("GulpoStruct",
		[]schema.StructField{
95
			schema.SpawnStructField("x", "SmolStruct", false, false),
Eric Myhre's avatar
Eric Myhre committed
96 97 98 99 100 101 102 103
		},
		schema.SpawnStructRepresentationMap(map[string]string{
			"x": "r",
		}),
	))

	prefix := "struct-nesting"
	pkgName := "main"
104 105 106
	genAndCompileAndTest(t, prefix, pkgName, ts, adjCfg, func(t *testing.T, getPrototypeByName func(string) ipld.NodePrototype) {
		np := getPrototypeByName("GulpoStruct")
		nrp := getPrototypeByName("GulpoStruct.Repr")
Eric Myhre's avatar
Eric Myhre committed
107 108
		var n schema.TypedNode
		t.Run("typed-create", func(t *testing.T) {
109
			n = fluent.MustBuildMap(np, 1, func(ma fluent.MapAssembler) {
Eric Myhre's avatar
Eric Myhre committed
110 111 112 113 114
				ma.AssembleEntry("x").CreateMap(1, func(ma fluent.MapAssembler) {
					ma.AssembleEntry("s").AssignString("woo")
				})
			}).(schema.TypedNode)
			t.Run("typed-read", func(t *testing.T) {
115
				Require(t, n.Kind(), ShouldEqual, ipld.Kind_Map)
116
				Wish(t, n.Length(), ShouldEqual, int64(1))
117
				n2 := must.Node(n.LookupByString("x"))
118
				Require(t, n2.Kind(), ShouldEqual, ipld.Kind_Map)
119
				Wish(t, must.String(must.Node(n2.LookupByString("s"))), ShouldEqual, "woo")
Eric Myhre's avatar
Eric Myhre committed
120 121 122
			})
			t.Run("repr-read", func(t *testing.T) {
				nr := n.Representation()
123
				Require(t, nr.Kind(), ShouldEqual, ipld.Kind_Map)
124
				Wish(t, nr.Length(), ShouldEqual, int64(1))
125
				n2 := must.Node(nr.LookupByString("r"))
126
				Require(t, n2.Kind(), ShouldEqual, ipld.Kind_Map)
127
				Wish(t, must.String(must.Node(n2.LookupByString("q"))), ShouldEqual, "woo")
Eric Myhre's avatar
Eric Myhre committed
128 129 130
			})
		})
		t.Run("repr-create", func(t *testing.T) {
131
			nr := fluent.MustBuildMap(nrp, 1, func(ma fluent.MapAssembler) {
Eric Myhre's avatar
Eric Myhre committed
132 133 134 135
				ma.AssembleEntry("r").CreateMap(1, func(ma fluent.MapAssembler) {
					ma.AssembleEntry("q").AssignString("woo")
				})
			})
136
			Wish(t, ipld.DeepEqual(n, nr), ShouldEqual, true)
Eric Myhre's avatar
Eric Myhre committed
137 138 139
		})
	})
}