Commit 613da6e7 authored by Eric Myhre's avatar Eric Myhre

gengo: add unmarshall test in generated package.

Exercise unmarshal against our generated "Strang" type.  Works!

(Note: this will fail if you *haven't* run the codegen yet; but it's in
an underscore-prefixed package, so it won't run unless you dive in here
intentionally.  Overall, this continues to be early-days hijinx, and
does not represent a good long-term testing strategy.)
parent f2ed5780
package whee
import (
"testing"
"github.com/polydawn/refmt/tok"
. "github.com/warpfork/go-wish"
ipld "github.com/ipld/go-ipld-prime"
"github.com/ipld/go-ipld-prime/encoding"
"github.com/ipld/go-ipld-prime/fluent"
)
// TokenSourceBucket acts like a TokenSource by yielding tokens from a pre-made
// slice; and also keeps track of how far it's been read.
type TokenSourceBucket struct {
tokens []tok.Token
read int
}
func (tb *TokenSourceBucket) Step(yield *tok.Token) (done bool, err error) {
*yield = tb.tokens[tb.read]
tb.read++
return tb.read > len(tb.tokens), nil
}
func TestScalarUnmarshal(t *testing.T) {
t.Run("string node", func(t *testing.T) {
tb := &TokenSourceBucket{tokens: []tok.Token{
{Type: tok.TString, Str: "zooooom"},
}}
nb := Strang__NodeBuilder{}
n, err := encoding.Unmarshal(nb, tb)
Wish(t, err, ShouldEqual, nil)
Wish(t, n.ReprKind(), ShouldEqual, ipld.ReprKind_String)
Wish(t, fluent.WrapNode(n).AsString(), ShouldEqual, "zooooom")
Wish(t, tb.read, ShouldEqual, 1)
})
}
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