github.com/ipld/go-ipld-prime@v0.21.0/codec/dagcbor/nongreedy_test.go (about)

     1  package dagcbor
     2  
     3  import (
     4  	"bytes"
     5  	"encoding/hex"
     6  	"testing"
     7  
     8  	qt "github.com/frankban/quicktest"
     9  	"github.com/ipld/go-ipld-prime"
    10  	"github.com/ipld/go-ipld-prime/datamodel"
    11  	"github.com/ipld/go-ipld-prime/fluent/qp"
    12  	"github.com/ipld/go-ipld-prime/node/basicnode"
    13  )
    14  
    15  func TestNonGreedy(t *testing.T) {
    16  	// same as JSON version of this test: {"a": 1}{"b": 2}
    17  	buf, err := hex.DecodeString("a1616101a1616202")
    18  	qt.Assert(t, err, qt.IsNil)
    19  	r := bytes.NewReader(buf)
    20  	opts := DecodeOptions{
    21  		DontParseBeyondEnd: true,
    22  	}
    23  
    24  	// first object
    25  	nb1 := basicnode.Prototype.Map.NewBuilder()
    26  	err = opts.Decode(nb1, r)
    27  	qt.Assert(t, err, qt.IsNil)
    28  	expected, err := qp.BuildMap(basicnode.Prototype.Any, 1, func(ma datamodel.MapAssembler) {
    29  		qp.MapEntry(ma, "a", qp.Int(1))
    30  	})
    31  	qt.Assert(t, err, qt.IsNil)
    32  	qt.Assert(t, ipld.DeepEqual(nb1.Build(), expected), qt.IsTrue)
    33  
    34  	// second object
    35  	nb2 := basicnode.Prototype.Map.NewBuilder()
    36  	err = opts.Decode(nb2, r)
    37  	qt.Assert(t, err, qt.IsNil)
    38  	expected, err = qp.BuildMap(basicnode.Prototype.Any, 1, func(ma datamodel.MapAssembler) {
    39  		qp.MapEntry(ma, "b", qp.Int(2))
    40  	})
    41  	qt.Assert(t, err, qt.IsNil)
    42  	qt.Assert(t, ipld.DeepEqual(nb2.Build(), expected), qt.IsTrue)
    43  }