Commit 50e2df1e authored by Eric Myhre's avatar Eric Myhre

Add more test specs for list and map nesting.

parent 44af1145
package basicnode
import (
"testing"
"github.com/ipld/go-ipld-prime/node/tests"
)
func TestList(t *testing.T) {
tests.SpecTestListString(t, Style__List{})
}
...@@ -9,6 +9,7 @@ import ( ...@@ -9,6 +9,7 @@ import (
func TestMap(t *testing.T) { func TestMap(t *testing.T) {
tests.SpecTestMapStrInt(t, Style__Map{}) tests.SpecTestMapStrInt(t, Style__Map{})
tests.SpecTestMapStrMapStrInt(t, Style__Map{}) tests.SpecTestMapStrMapStrInt(t, Style__Map{})
tests.SpecTestMapStrListStr(t, Style__Map{})
} }
func BenchmarkMapStrInt_3n_AssembleStandard(b *testing.B) { func BenchmarkMapStrInt_3n_AssembleStandard(b *testing.B) {
......
package tests
import (
"testing"
. "github.com/warpfork/go-wish"
ipld "github.com/ipld/go-ipld-prime"
"github.com/ipld/go-ipld-prime/fluent"
"github.com/ipld/go-ipld-prime/must"
)
func SpecTestListString(t *testing.T, ns ipld.NodeStyle) {
t.Run("list<string>, 3 entries", func(t *testing.T) {
n := fluent.MustBuildList(ns, 3, func(la fluent.ListAssembler) {
la.AssembleValue().AssignString("one")
la.AssembleValue().AssignString("two")
la.AssembleValue().AssignString("three")
})
t.Run("reads back out", func(t *testing.T) {
Wish(t, n.Length(), ShouldEqual, 3)
v, err := n.LookupIndex(0)
Wish(t, err, ShouldEqual, nil)
Wish(t, must.String(v), ShouldEqual, "one")
v, err = n.LookupIndex(1)
Wish(t, err, ShouldEqual, nil)
Wish(t, must.String(v), ShouldEqual, "two")
v, err = n.LookupIndex(2)
Wish(t, err, ShouldEqual, nil)
Wish(t, must.String(v), ShouldEqual, "three")
})
t.Run("reads via iteration", func(t *testing.T) {
itr := n.ListIterator()
Wish(t, itr.Done(), ShouldEqual, false)
idx, v, err := itr.Next()
Wish(t, err, ShouldEqual, nil)
Wish(t, idx, ShouldEqual, 0)
Wish(t, must.String(v), ShouldEqual, "one")
Wish(t, itr.Done(), ShouldEqual, false)
idx, v, err = itr.Next()
Wish(t, err, ShouldEqual, nil)
Wish(t, idx, ShouldEqual, 1)
Wish(t, must.String(v), ShouldEqual, "two")
Wish(t, itr.Done(), ShouldEqual, false)
idx, v, err = itr.Next()
Wish(t, err, ShouldEqual, nil)
Wish(t, idx, ShouldEqual, 2)
Wish(t, must.String(v), ShouldEqual, "three")
Wish(t, itr.Done(), ShouldEqual, true)
idx, v, err = itr.Next()
Wish(t, err, ShouldEqual, ipld.ErrIteratorOverread{})
Wish(t, idx, ShouldEqual, -1)
Wish(t, v, ShouldEqual, nil)
})
})
}
...@@ -186,3 +186,43 @@ func SpecTestMapStrMapStrInt(t *testing.T, ns ipld.NodeStyle) { ...@@ -186,3 +186,43 @@ func SpecTestMapStrMapStrInt(t *testing.T, ns ipld.NodeStyle) {
}) })
}) })
} }
func SpecTestMapStrListStr(t *testing.T, ns ipld.NodeStyle) {
t.Run("map<str,list<str>>", func(t *testing.T) {
nb := ns.NewBuilder()
ma, err := nb.BeginMap(3)
must.NotError(err)
must.NotError(ma.AssembleKey().AssignString("asdf"))
func(la ipld.ListAssembler, err error) {
must.NotError(la.AssembleValue().AssignString("eleven"))
must.NotError(la.AssembleValue().AssignString("twelve"))
must.NotError(la.AssembleValue().AssignString("thirteen"))
must.NotError(la.Finish())
}(ma.AssembleValue().BeginList(3))
must.NotError(ma.AssembleKey().AssignString("qwer"))
func(la ipld.ListAssembler, err error) {
must.NotError(la.AssembleValue().AssignString("twentyone"))
must.NotError(la.AssembleValue().AssignString("twentytwo"))
must.NotError(la.Finish())
}(ma.AssembleValue().BeginList(2))
must.NotError(ma.AssembleKey().AssignString("zxcv"))
func(la ipld.ListAssembler, err error) {
must.NotError(la.AssembleValue().AssignString("thirtyone"))
must.NotError(la.Finish())
}(ma.AssembleValue().BeginList(1))
must.NotError(ma.Finish())
n := nb.Build()
t.Run("reads back out", func(t *testing.T) {
Wish(t, n.Length(), ShouldEqual, 3)
v, err := n.LookupString("qwer")
Wish(t, err, ShouldEqual, nil)
v2, err := v.LookupIndex(1)
Wish(t, err, ShouldEqual, nil)
v3, err := v2.AsString()
Wish(t, err, ShouldEqual, nil)
Wish(t, v3, ShouldEqual, "twentytwo")
})
})
}
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