Commit fa013972 authored by Eric Myhre's avatar Eric Myhre

Tests for keyed unions; some fixes.

parent 605efe83
...@@ -88,10 +88,10 @@ func (g unionReprKeyedReprGenerator) EmitNodeMethodLookupByString(w io.Writer) { ...@@ -88,10 +88,10 @@ func (g unionReprKeyedReprGenerator) EmitNodeMethodLookupByString(w io.Writer) {
if n.tag != {{ add $i 1 }} { if n.tag != {{ add $i 1 }} {
return nil, ipld.ErrNotExists{ipld.PathSegmentOfString(key)} return nil, ipld.ErrNotExists{ipld.PathSegmentOfString(key)}
} }
return &n.x{{ add $i 1 }}, nil return n.x{{ add $i 1 }}.Representation(), nil
{{- else if (eq (dot.AdjCfg.UnionMemlayout dot.Type) "interface") }} {{- else if (eq (dot.AdjCfg.UnionMemlayout dot.Type) "interface") }}
if n2, ok := n.x.({{ $member | TypeSymbol }}); ok { if n2, ok := n.x.({{ $member | TypeSymbol }}); ok {
return n2, nil return n2.Representation(), nil
} else { } else {
return nil, ipld.ErrNotExists{ipld.PathSegmentOfString(key)} return nil, ipld.ErrNotExists{ipld.PathSegmentOfString(key)}
} }
...@@ -351,7 +351,7 @@ func (g unionReprKeyedReprBuilderGenerator) emitMapAssemblerMethods(w io.Writer) ...@@ -351,7 +351,7 @@ func (g unionReprKeyedReprBuilderGenerator) emitMapAssemblerMethods(w io.Writer)
x := &_{{ $member | TypeSymbol }}{} x := &_{{ $member | TypeSymbol }}{}
ma.w.x = x ma.w.x = x
if ma.ca{{ add $i 1 }} == nil { if ma.ca{{ add $i 1 }} == nil {
ma.ca{{ add $i 1 }} = &_{{ $member | TypeSymbol }}__Assembler{} ma.ca{{ add $i 1 }} = &_{{ $member | TypeSymbol }}__ReprAssembler{}
} }
ma.ca{{ add $i 1 }}.w = x ma.ca{{ add $i 1 }}.w = x
ma.ca{{ add $i 1 }}.m = &ma.cm ma.ca{{ add $i 1 }}.m = &ma.cm
......
...@@ -75,3 +75,90 @@ func TestUnionKeyed(t *testing.T) { ...@@ -75,3 +75,90 @@ func TestUnionKeyed(t *testing.T) {
}) })
}) })
} }
// Test keyed unions again, but this time with more complex types as children.
//
// The previous tests used scalar types as the children; this exercises most things,
// but also has a couple (extremely non-obvious) simplifications:
// namely, because the default representation for strings are "natural" representations,
// the ReprAssemblers are actually aliases of the type-level Assemblers!
// Aaaand that makes a few things "work" by coincidence that wouldn't otherwise fly.
func TestUnionKeyedComplexChildren(t *testing.T) {
ts := schema.TypeSystem{}
ts.Init()
adjCfg := &AdjunctCfg{}
ts.Accumulate(schema.SpawnString("String"))
ts.Accumulate(schema.SpawnStruct("SmolStruct",
[]schema.StructField{
schema.SpawnStructField("s", "String", false, false),
},
schema.SpawnStructRepresentationMap(map[string]string{
"s": "q",
}),
))
ts.Accumulate(schema.SpawnUnion("WheeUnion",
[]schema.TypeName{
"String",
"SmolStruct",
},
schema.SpawnUnionRepresentationKeyed(map[string]schema.TypeName{
"a": "String",
"b": "SmolStruct",
}),
))
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)
n2 := must.Node(nr.LookupByString("b"))
Require(t, n2.ReprKind(), ShouldEqual, ipld.ReprKind_Map)
Wish(t, must.String(must.Node(n2.LookupByString("q"))), ShouldEqual, "whee")
})
})
t.Run("repr-create", func(t *testing.T) {
nr := fluent.MustBuildMap(nrp, 2, func(na fluent.MapAssembler) {
na.AssembleEntry("b").CreateMap(1, func(na fluent.MapAssembler) {
na.AssembleEntry("q").AssignString("whee")
})
})
Wish(t, n, ShouldEqual, nr)
})
}
t.Run("union-using-embed", func(t *testing.T) {
adjCfg.CfgUnionMemlayout = map[schema.TypeName]string{"WheeUnion": "embedAll"}
prefix := "union-keyed-complex-child-using-embed"
pkgName := "main"
genAndCompileAndTest(t, prefix, pkgName, ts, adjCfg, func(t *testing.T, getPrototypeByName func(string) ipld.NodePrototype) {
test(t, getPrototypeByName)
})
})
t.Run("union-using-interface", func(t *testing.T) {
adjCfg.CfgUnionMemlayout = map[schema.TypeName]string{"WheeUnion": "interface"}
prefix := "union-keyed-complex-child-using-interface"
pkgName := "main"
genAndCompileAndTest(t, prefix, pkgName, ts, adjCfg, func(t *testing.T, getPrototypeByName func(string) ipld.NodePrototype) {
test(t, getPrototypeByName)
})
})
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment