5 "github.com/pelletier/go-toml"
9 // dump path tree to a string
10 func pathString(root pathFn) string {
11 result := fmt.Sprintf("%T:", root)
12 switch fn := root.(type) {
16 result += fmt.Sprintf("{%s}", fn.Name)
17 result += pathString(fn.next)
19 result += fmt.Sprintf("{%d}", fn.Idx)
20 result += pathString(fn.next)
22 result += fmt.Sprintf("{%d:%d:%d}",
23 fn.Start, fn.End, fn.Step)
24 result += pathString(fn.next)
27 result += pathString(fn.next)
30 for _, v := range fn.Union {
31 result += pathString(v) + ", "
34 case *matchRecursiveFn:
36 result += pathString(fn.next)
38 result += fmt.Sprintf("{%s}", fn.Name)
39 result += pathString(fn.next)
44 func assertPathMatch(t *testing.T, path, ref *Query) bool {
45 pathStr := pathString(path.root)
46 refStr := pathString(ref.root)
47 if pathStr != refStr {
48 t.Errorf("paths do not match")
49 t.Log("test:", pathStr)
50 t.Log("ref: ", refStr)
56 func assertPath(t *testing.T, query string, ref *Query) {
57 path, _ := parseQuery(lexQuery(query))
58 assertPathMatch(t, path, ref)
61 func buildPath(parts ...pathFn) *Query {
63 for _, v := range parts {
69 func TestPathRoot(t *testing.T) {
77 func TestPathKey(t *testing.T) {
85 func TestPathBracketKey(t *testing.T) {
93 func TestPathBracketStringKey(t *testing.T) {
101 func TestPathIndex(t *testing.T) {
105 newMatchIndexFn(123),
109 func TestPathSliceStart(t *testing.T) {
113 newMatchSliceFn(123, maxInt, 1),
117 func TestPathSliceStartEnd(t *testing.T) {
121 newMatchSliceFn(123, 456, 1),
125 func TestPathSliceStartEndColon(t *testing.T) {
129 newMatchSliceFn(123, 456, 1),
133 func TestPathSliceStartStep(t *testing.T) {
137 newMatchSliceFn(123, maxInt, 7),
141 func TestPathSliceEndStep(t *testing.T) {
145 newMatchSliceFn(0, 456, 7),
149 func TestPathSliceStep(t *testing.T) {
153 newMatchSliceFn(0, maxInt, 7),
157 func TestPathSliceAll(t *testing.T) {
161 newMatchSliceFn(123, 456, 7),
165 func TestPathAny(t *testing.T) {
173 func TestPathUnion(t *testing.T) {
177 &matchUnionFn{[]pathFn{
178 newMatchKeyFn("foo"),
179 newMatchKeyFn("bar"),
180 newMatchKeyFn("baz"),
185 func TestPathRecurse(t *testing.T) {
189 newMatchRecursiveFn(),
193 func TestPathFilterExpr(t *testing.T) {
195 "$[?('foo'),?(bar)]",
197 &matchUnionFn{[]pathFn{
198 newMatchFilterFn("foo", toml.Position{}),
199 newMatchFilterFn("bar", toml.Position{}),