github.com/ipld/go-ipld-prime@v0.21.0/datamodel/path_test.go (about)

     1  package datamodel
     2  
     3  import (
     4  	"reflect"
     5  	"testing"
     6  
     7  	qt "github.com/frankban/quicktest"
     8  	"github.com/google/go-cmp/cmp"
     9  )
    10  
    11  func TestParsePath(t *testing.T) {
    12  	// Allow equality checker to check all unexported fields in PathSegment.
    13  	pathSegmentEquals := qt.CmpEquals(cmp.Exporter(func(reflect.Type) bool { return true }))
    14  	t.Run("parsing one segment", func(t *testing.T) {
    15  		qt.Check(t, ParsePath("0").segments, pathSegmentEquals, []PathSegment{{s: "0", i: -1}})
    16  	})
    17  	t.Run("parsing three segments", func(t *testing.T) {
    18  		qt.Check(t, ParsePath("0/foo/2").segments, pathSegmentEquals, []PathSegment{{s: "0", i: -1}, {s: "foo", i: -1}, {s: "2", i: -1}})
    19  	})
    20  	t.Run("eliding leading slashes", func(t *testing.T) {
    21  		qt.Check(t, ParsePath("/0/2").segments, pathSegmentEquals, []PathSegment{{s: "0", i: -1}, {s: "2", i: -1}})
    22  	})
    23  	t.Run("eliding trailing", func(t *testing.T) {
    24  		qt.Check(t, ParsePath("0/2/").segments, pathSegmentEquals, []PathSegment{{s: "0", i: -1}, {s: "2", i: -1}})
    25  	})
    26  	t.Run("eliding empty segments", func(t *testing.T) { // NOTE: a spec for string encoding might cause this to change in the future!
    27  		qt.Check(t, ParsePath("0//2").segments, pathSegmentEquals, []PathSegment{{s: "0", i: -1}, {s: "2", i: -1}})
    28  	})
    29  	t.Run("escaping segments", func(t *testing.T) { // NOTE: a spec for string encoding might cause this to change in the future!
    30  		qt.Check(t, ParsePath(`0/\//2`).segments, pathSegmentEquals, []PathSegment{{s: "0", i: -1}, {s: `\`, i: -1}, {s: "2", i: -1}})
    31  	})
    32  }
    33  
    34  func TestPathSegmentZeroValue(t *testing.T) {
    35  	qt.Check(t, PathSegment{}.String(), qt.Equals, "0")
    36  	i, err := PathSegment{}.Index()
    37  	qt.Check(t, err, qt.IsNil)
    38  	qt.Check(t, i, qt.Equals, int64(0))
    39  }