github.com/ipld/go-ipld-prime@v0.21.0/traversal/selector/exploreAll_test.go (about)

     1  package selector
     2  
     3  import (
     4  	"testing"
     5  
     6  	qt "github.com/frankban/quicktest"
     7  
     8  	"github.com/ipld/go-ipld-prime/fluent"
     9  	"github.com/ipld/go-ipld-prime/node/basicnode"
    10  )
    11  
    12  func TestParseExploreAll(t *testing.T) {
    13  	t.Run("parsing non map node should error", func(t *testing.T) {
    14  		sn := basicnode.NewInt(0)
    15  		_, err := ParseContext{}.ParseExploreAll(sn)
    16  		qt.Check(t, err, qt.ErrorMatches, "selector spec parse rejected: selector body must be a map")
    17  	})
    18  	t.Run("parsing map node without next field should error", func(t *testing.T) {
    19  		sn := fluent.MustBuildMap(basicnode.Prototype.Map, 0, func(na fluent.MapAssembler) {})
    20  		_, err := ParseContext{}.ParseExploreAll(sn)
    21  		qt.Check(t, err, qt.ErrorMatches, "selector spec parse rejected: next field must be present in ExploreAll selector")
    22  	})
    23  
    24  	t.Run("parsing map node without next field with invalid selector node should return child's error", func(t *testing.T) {
    25  		sn := fluent.MustBuildMap(basicnode.Prototype.Map, 1, func(na fluent.MapAssembler) {
    26  			na.AssembleEntry(SelectorKey_Next).AssignInt(0)
    27  		})
    28  		_, err := ParseContext{}.ParseExploreAll(sn)
    29  		qt.Check(t, err, qt.ErrorMatches, "selector spec parse rejected: selector is a keyed union and thus must be a map")
    30  	})
    31  	t.Run("parsing map node with next field with valid selector node should parse", func(t *testing.T) {
    32  		sn := fluent.MustBuildMap(basicnode.Prototype.Map, 1, func(na fluent.MapAssembler) {
    33  			na.AssembleEntry(SelectorKey_Next).CreateMap(1, func(na fluent.MapAssembler) {
    34  				na.AssembleEntry(SelectorKey_Matcher).CreateMap(0, func(na fluent.MapAssembler) {})
    35  			})
    36  		})
    37  		s, err := ParseContext{}.ParseExploreAll(sn)
    38  		qt.Check(t, err, qt.IsNil)
    39  		qt.Check(t, s, qt.Equals, ExploreAll{Matcher{}})
    40  	})
    41  }