Commit c4285526 authored by Eric Myhre's avatar Eric Myhre

Exhaustive test cases for nullable and optionals.

One bug fixed.
Signed-off-by: default avatarEric Myhre <hash@exultant.us>
parent f84f4fcb
......@@ -66,6 +66,68 @@ func TestStructBuilder(t *testing.T) {
Wish(t, plz(n.LookupString("f3")), ShouldEqual, plz(String__NodeBuilder{}.CreateString("c")))
Wish(t, plz(n.LookupString("f4")), ShouldEqual, plz(String__NodeBuilder{}.CreateString("d")))
})
t.Run("using null nullable", func(t *testing.T) {
mb, err := Stroct__NodeBuilder{}.CreateMap()
Require(t, err, ShouldEqual, nil)
mb.Insert(ipldfree.String("f1"), plz(String__NodeBuilder{}.CreateString("a")))
mb.Insert(ipldfree.String("f2"), plz(String__NodeBuilder{}.CreateString("b")))
mb.Insert(ipldfree.String("f3"), plz(String__NodeBuilder{}.CreateString("c")))
mb.Insert(ipldfree.String("f4"), ipld.Null)
n, err := mb.Build()
Wish(t, err, ShouldEqual, nil)
Wish(t, n.ReprKind(), ShouldEqual, ipld.ReprKind_Map)
Wish(t, plz(n.LookupString("f1")), ShouldEqual, plz(String__NodeBuilder{}.CreateString("a")))
Wish(t, plz(n.LookupString("f2")), ShouldEqual, plz(String__NodeBuilder{}.CreateString("b")))
Wish(t, plz(n.LookupString("f3")), ShouldEqual, plz(String__NodeBuilder{}.CreateString("c")))
Wish(t, plz(n.LookupString("f4")), ShouldEqual, ipld.Null)
})
t.Run("using null optional nullable", func(t *testing.T) {
mb, err := Stroct__NodeBuilder{}.CreateMap()
Require(t, err, ShouldEqual, nil)
mb.Insert(ipldfree.String("f1"), plz(String__NodeBuilder{}.CreateString("a")))
mb.Insert(ipldfree.String("f2"), plz(String__NodeBuilder{}.CreateString("b")))
mb.Insert(ipldfree.String("f3"), ipld.Null)
mb.Insert(ipldfree.String("f4"), plz(String__NodeBuilder{}.CreateString("d")))
n, err := mb.Build()
Wish(t, err, ShouldEqual, nil)
Wish(t, n.ReprKind(), ShouldEqual, ipld.ReprKind_Map)
Wish(t, plz(n.LookupString("f1")), ShouldEqual, plz(String__NodeBuilder{}.CreateString("a")))
Wish(t, plz(n.LookupString("f2")), ShouldEqual, plz(String__NodeBuilder{}.CreateString("b")))
Wish(t, plz(n.LookupString("f3")), ShouldEqual, ipld.Null)
Wish(t, plz(n.LookupString("f4")), ShouldEqual, plz(String__NodeBuilder{}.CreateString("d")))
})
t.Run("using skipped optional", func(t *testing.T) {
mb, err := Stroct__NodeBuilder{}.CreateMap()
Require(t, err, ShouldEqual, nil)
mb.Insert(ipldfree.String("f1"), plz(String__NodeBuilder{}.CreateString("a")))
mb.Insert(ipldfree.String("f3"), plz(String__NodeBuilder{}.CreateString("c")))
mb.Insert(ipldfree.String("f4"), plz(String__NodeBuilder{}.CreateString("d")))
n, err := mb.Build()
Wish(t, err, ShouldEqual, nil)
Wish(t, n.ReprKind(), ShouldEqual, ipld.ReprKind_Map)
Wish(t, plz(n.LookupString("f1")), ShouldEqual, plz(String__NodeBuilder{}.CreateString("a")))
Wish(t, plz(n.LookupString("f2")), ShouldEqual, ipld.Undef)
Wish(t, plz(n.LookupString("f3")), ShouldEqual, plz(String__NodeBuilder{}.CreateString("c")))
Wish(t, plz(n.LookupString("f4")), ShouldEqual, plz(String__NodeBuilder{}.CreateString("d")))
})
t.Run("using skipped optional nullable", func(t *testing.T) {
mb, err := Stroct__NodeBuilder{}.CreateMap()
Require(t, err, ShouldEqual, nil)
mb.Insert(ipldfree.String("f1"), plz(String__NodeBuilder{}.CreateString("a")))
mb.Insert(ipldfree.String("f2"), plz(String__NodeBuilder{}.CreateString("b")))
mb.Insert(ipldfree.String("f4"), plz(String__NodeBuilder{}.CreateString("d")))
n, err := mb.Build()
Wish(t, err, ShouldEqual, nil)
Wish(t, n.ReprKind(), ShouldEqual, ipld.ReprKind_Map)
Wish(t, plz(n.LookupString("f1")), ShouldEqual, plz(String__NodeBuilder{}.CreateString("a")))
Wish(t, plz(n.LookupString("f2")), ShouldEqual, plz(String__NodeBuilder{}.CreateString("b")))
Wish(t, plz(n.LookupString("f3")), ShouldEqual, ipld.Undef)
Wish(t, plz(n.LookupString("f4")), ShouldEqual, plz(String__NodeBuilder{}.CreateString("d")))
})
})
}
......
......@@ -91,13 +91,15 @@ func (gk generateNbKindStruct) EmitNodebuilderMethodCreateMap(w io.Writer) {
if !ok {
panic("field '{{$field.Name}}' in type {{.Type.Name}} is type {{$field.Type.Name}}; cannot assign "+tv.Type().Name()) // FIXME need an error type for this
}
{{- if and $field.IsOptional $field.IsNullable }}
mb.v.{{ $field.Name }} = &x
mb.v.{{ $field.Name }}__exists = true
{{- else if or $field.IsOptional $field.IsNullable }}
{{- if or $field.IsOptional $field.IsNullable }}
mb.v.{{ $field.Name }} = &x
{{- else}}
mb.v.{{ $field.Name }} = x
{{- end}}
{{- if and $field.IsOptional $field.IsNullable }}
mb.v.{{ $field.Name }}__exists = true
{{- else if not $field.IsOptional }}
mb.{{ $field.Name }}__isset = true
{{- end}}
{{- end}}
......
......@@ -81,7 +81,7 @@ func TestNuevo(t *testing.T) {
schema.SpawnStructField("f1", tString, false, false),
schema.SpawnStructField("f2", tString, true, false),
schema.SpawnStructField("f3", tString, true, true),
schema.SpawnStructField("f4", tString, false, false),
schema.SpawnStructField("f4", tString, false, true),
},
schema.StructRepresentation_Map{},
)
......
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