github.com/ipld/go-ipld-prime@v0.21.0/traversal/selector/exploreIndex_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/datamodel" 9 "github.com/ipld/go-ipld-prime/fluent" 10 "github.com/ipld/go-ipld-prime/node/basicnode" 11 ) 12 13 func TestParseExploreIndex(t *testing.T) { 14 t.Run("parsing non map node should error", func(t *testing.T) { 15 sn := basicnode.NewInt(0) 16 _, err := ParseContext{}.ParseExploreIndex(sn) 17 qt.Check(t, err, qt.ErrorMatches, "selector spec parse rejected: selector body must be a map") 18 }) 19 t.Run("parsing map node without next field should error", func(t *testing.T) { 20 sn := fluent.MustBuildMap(basicnode.Prototype__Map{}, 1, func(na fluent.MapAssembler) { 21 na.AssembleEntry(SelectorKey_Index).AssignInt(2) 22 }) 23 _, err := ParseContext{}.ParseExploreIndex(sn) 24 qt.Check(t, err, qt.ErrorMatches, "selector spec parse rejected: next field must be present in ExploreIndex selector") 25 }) 26 t.Run("parsing map node without index field should error", func(t *testing.T) { 27 sn := fluent.MustBuildMap(basicnode.Prototype__Map{}, 1, func(na fluent.MapAssembler) { 28 na.AssembleEntry(SelectorKey_Next).CreateMap(1, func(na fluent.MapAssembler) { 29 na.AssembleEntry(SelectorKey_Matcher).CreateMap(0, func(na fluent.MapAssembler) {}) 30 }) 31 }) 32 _, err := ParseContext{}.ParseExploreIndex(sn) 33 qt.Check(t, err, qt.ErrorMatches, "selector spec parse rejected: index field must be present in ExploreIndex selector") 34 }) 35 t.Run("parsing map node with index field that is not an int should error", func(t *testing.T) { 36 sn := fluent.MustBuildMap(basicnode.Prototype__Map{}, 2, func(na fluent.MapAssembler) { 37 na.AssembleEntry(SelectorKey_Index).AssignString("cheese") 38 na.AssembleEntry(SelectorKey_Next).CreateMap(1, func(na fluent.MapAssembler) { 39 na.AssembleEntry(SelectorKey_Matcher).CreateMap(0, func(na fluent.MapAssembler) {}) 40 }) 41 }) 42 _, err := ParseContext{}.ParseExploreIndex(sn) 43 qt.Check(t, err, qt.ErrorMatches, "selector spec parse rejected: index field must be a number in ExploreIndex selector") 44 }) 45 t.Run("parsing map node with next field with invalid selector node should return child's error", func(t *testing.T) { 46 sn := fluent.MustBuildMap(basicnode.Prototype__Map{}, 2, func(na fluent.MapAssembler) { 47 na.AssembleEntry(SelectorKey_Index).AssignInt(2) 48 na.AssembleEntry(SelectorKey_Next).AssignInt(0) 49 }) 50 _, err := ParseContext{}.ParseExploreIndex(sn) 51 qt.Check(t, err, qt.ErrorMatches, "selector spec parse rejected: selector is a keyed union and thus must be a map") 52 }) 53 t.Run("parsing map node with next field with valid selector node should parse", func(t *testing.T) { 54 sn := fluent.MustBuildMap(basicnode.Prototype__Map{}, 2, func(na fluent.MapAssembler) { 55 na.AssembleEntry(SelectorKey_Index).AssignInt(2) 56 na.AssembleEntry(SelectorKey_Next).CreateMap(1, func(na fluent.MapAssembler) { 57 na.AssembleEntry(SelectorKey_Matcher).CreateMap(0, func(na fluent.MapAssembler) {}) 58 }) 59 }) 60 s, err := ParseContext{}.ParseExploreIndex(sn) 61 qt.Check(t, err, qt.IsNil) 62 qt.Check(t, s, qt.Equals, ExploreIndex{Matcher{}, [1]datamodel.PathSegment{datamodel.PathSegmentOfInt(2)}}) 63 }) 64 } 65 66 func TestExploreIndexExplore(t *testing.T) { 67 s := ExploreIndex{Matcher{}, [1]datamodel.PathSegment{datamodel.PathSegmentOfInt(3)}} 68 t.Run("exploring should return nil unless node is a list", func(t *testing.T) { 69 n := fluent.MustBuildMap(basicnode.Prototype__Map{}, 0, func(na fluent.MapAssembler) {}) 70 returnedSelector, _ := s.Explore(n, datamodel.PathSegmentOfInt(3)) 71 qt.Check(t, returnedSelector, qt.IsNil) 72 }) 73 n := fluent.MustBuildList(basicnode.Prototype__List{}, 4, func(na fluent.ListAssembler) { 74 na.AssembleValue().AssignInt(0) 75 na.AssembleValue().AssignInt(1) 76 na.AssembleValue().AssignInt(2) 77 na.AssembleValue().AssignInt(3) 78 }) 79 t.Run("exploring should return nil when given a path segment with a different index", func(t *testing.T) { 80 returnedSelector, _ := s.Explore(n, datamodel.PathSegmentOfInt(2)) 81 qt.Check(t, returnedSelector, qt.IsNil) 82 }) 83 t.Run("exploring should return nil when given a path segment that isn't an index", func(t *testing.T) { 84 returnedSelector, _ := s.Explore(n, datamodel.PathSegmentOfString("cheese")) 85 qt.Check(t, returnedSelector, qt.IsNil) 86 }) 87 t.Run("exploring should return the next selector when given a path segment with the right index", func(t *testing.T) { 88 returnedSelector, _ := s.Explore(n, datamodel.PathSegmentOfInt(3)) 89 qt.Check(t, returnedSelector, qt.Equals, Matcher{}) 90 }) 91 }