jsonpath/path_test.go

42 lines
1000 B
Go
Raw Permalink Normal View History

2019-02-26 13:05:15 +04:00
package jsonpath
import (
"testing"
"github.com/stretchr/testify/assert"
)
type optest struct {
name string
path string
expected []int
}
var optests = []optest{
{"single key (period) ", `$.aKey`, []int{opTypeName}},
{"single key (bracket)", `$["aKey"]`, []int{opTypeName}},
{"single key (period) ", `$.*`, []int{opTypeNameWild}},
{"single index", `$[12]`, []int{opTypeIndex}},
{"single key", `$[23:45]`, []int{opTypeIndexRange}},
{"single key", `$[*]`, []int{opTypeIndexWild}},
{"double key", `$["aKey"]["bKey"]`, []int{opTypeName, opTypeName}},
{"double key", `$["aKey"].bKey`, []int{opTypeName, opTypeName}},
2019-02-26 13:05:15 +04:00
}
func TestQueryOperators(t *testing.T) {
as := assert.New(t)
2019-10-19 01:00:37 +04:00
for _, optest := range optests {
t.Log(optest.name)
path, err := parsePath(optest.path)
2019-02-26 13:05:15 +04:00
as.NoError(err)
2019-10-19 01:00:37 +04:00
as.EqualValues(len(optest.expected), len(path.operators))
2019-02-26 13:05:15 +04:00
2019-10-19 01:00:37 +04:00
for x, op := range optest.expected {
2019-02-26 13:05:15 +04:00
as.EqualValues(pathTokenNames[op], pathTokenNames[path.operators[x].typ])
}
}
}