Skip to content

Commit

Permalink
Convert kinded union tests to use new helpers.
Browse files Browse the repository at this point in the history
This is probably now one of the nicer (directly-exercised) examples we
have for how the type-level and representation-level views of data can
differ with the use of schemas.
  • Loading branch information
warpfork committed Sep 5, 2020
1 parent 1027f24 commit 44513d6
Showing 1 changed file with 37 additions and 31 deletions.
68 changes: 37 additions & 31 deletions schema/gen/go/testUnionsKinded_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,7 @@ 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"
)

Expand Down Expand Up @@ -35,36 +31,46 @@ func TestUnionKinded(t *testing.T) {
}),
))

// These are the same *type-level* as in TestUnionKeyedComplexChildren,
// but (of course) have very different representations.
specs := []testcase{
{
name: "InhabitantA",
typeJson: `{"String":"whee"}`,
reprJson: `"whee"`,
typePoints: []testcasePoint{
{"", ipld.ReprKind_Map},
{"String", "whee"},
//{"SmolStruct", ipld.ErrNotExists{}}, // TODO: need better error typing from traversal package.
},
reprPoints: []testcasePoint{
{"", ipld.ReprKind_String},
{"", "whee"},
},
},
{
name: "InhabitantB",
typeJson: `{"SmolStruct":{"s":"whee"}}`,
reprJson: `{"q":"whee"}`,
typePoints: []testcasePoint{
{"", ipld.ReprKind_Map},
//{"String", ipld.ErrNotExists{}}, // TODO: need better error typing from traversal package.
{"SmolStruct", ipld.ReprKind_Map},
{"SmolStruct/s", "whee"},
},
reprPoints: []testcasePoint{
{"", ipld.ReprKind_Map},
{"q", "whee"},
},
},
}

test := func(t *testing.T, getPrototypeByName func(string) ipld.NodePrototype) {
np := getPrototypeByName("WheeUnion")
nrp := getPrototypeByName("WheeUnion.Repr")
var n schema.TypedNode
t.Run("typed-create", func(t *testing.T) {
n = fluent.MustBuildMap(np, 1, func(na fluent.MapAssembler) {
na.AssembleEntry("SmolStruct").CreateMap(1, func(na fluent.MapAssembler) {
na.AssembleEntry("s").AssignString("whee")
})
}).(schema.TypedNode)
t.Run("typed-read", func(t *testing.T) {
Require(t, n.ReprKind(), ShouldEqual, ipld.ReprKind_Map)
Wish(t, n.Length(), ShouldEqual, 1)
n2 := must.Node(n.LookupByString("SmolStruct"))
Require(t, n2.ReprKind(), ShouldEqual, ipld.ReprKind_Map)
Wish(t, must.String(must.Node(n2.LookupByString("s"))), ShouldEqual, "whee")
})
t.Run("repr-read", func(t *testing.T) {
nr := n.Representation()
Require(t, nr.ReprKind(), ShouldEqual, ipld.ReprKind_Map)
Wish(t, nr.Length(), ShouldEqual, 1)
Wish(t, must.String(must.Node(nr.LookupByString("q"))), ShouldEqual, "whee")
})
})
t.Run("repr-create", func(t *testing.T) {
nr := fluent.MustBuildMap(nrp, 1, func(na fluent.MapAssembler) {
na.AssembleEntry("q").AssignString("whee")
})
Wish(t, n, ShouldEqual, nr)
})
for _, tcase := range specs {
tcase.Test(t, np, nrp)
}
}

t.Run("union-using-embed", func(t *testing.T) {
Expand Down

0 comments on commit 44513d6

Please sign in to comment.