1
1
mirror of https://github.com/wader/fq.git synced 2024-11-23 18:56:52 +03:00
fq/pkg/interp/completion_test.go

56 lines
2.2 KiB
Go
Raw Normal View History

2020-06-08 03:29:51 +03:00
package interp_test
import (
"testing"
"github.com/wader/fq/pkg/interp"
2020-06-08 03:29:51 +03:00
)
func TestBuildCompletionQuery(t *testing.T) {
testCases := []struct {
input string
expectedQuery string
expectedType interp.CompletionType
expectedPrefix string
}{
{"", "", interp.CompletionTypeError, ""},
{`.`, `[.[] | . | keys?] | add`, interp.CompletionTypeIndex, ``},
{`.`, `[.[] | . | keys?] | add`, interp.CompletionTypeIndex, ``},
{`.a`, `[.[] | . | keys?] | add`, interp.CompletionTypeIndex, `a`},
{`.a.`, `[.[] | .a | keys?] | add`, interp.CompletionTypeIndex, ``},
{`.a.b`, `[.[] | .a | keys?] | add`, interp.CompletionTypeIndex, `b`},
{`.a.b.`, `[.[] | .a.b | keys?] | add`, interp.CompletionTypeIndex, ``},
{`.a.b`, `[.[] | .a | keys?] | add`, interp.CompletionTypeIndex, `b`},
{`.a | .b`, `[.[] | .a | . | keys?] | add`, interp.CompletionTypeIndex, `b`},
{`.a | .b.c`, `[.[] | .a | .b | keys?] | add`, interp.CompletionTypeIndex, `c`},
{`.a[]`, `.a[]`, interp.CompletionTypeNone, ``},
{`.a[].b`, `[.[] | .a[] | keys?] | add`, interp.CompletionTypeIndex, `b`},
{`.a[].b.c`, `[.[] | .a[].b | keys?] | add`, interp.CompletionTypeIndex, `c`},
{`.a["b"]`, `.a["b"]`, interp.CompletionTypeNone, ``},
{`.a["b"].c`, `[.[] | .a["b"] | keys?] | add`, interp.CompletionTypeIndex, `c`},
{`.a[1:2]`, `.a[1:2]`, interp.CompletionTypeNone, ``},
{`.a[1:2].c`, `[.[] | .a[1:2] | keys?] | add`, interp.CompletionTypeIndex, `c`},
{`a`, `[.[] | scope?] | add`, interp.CompletionTypeFunc, `a`},
{`a | b`, `[.[] | a | scope?] | add`, interp.CompletionTypeFunc, `b`},
2020-06-08 03:29:51 +03:00
}
for _, tC := range testCases {
t.Run(tC.input, func(t *testing.T) {
actualQuery, actualType, actualPrefix := interp.BuildCompletionQuery(tC.input)
actualQueryStr := ""
if actualQuery != nil {
actualQueryStr = actualQuery.String()
}
if tC.expectedQuery != actualQueryStr {
t.Errorf("expected query %q, got query %q", tC.expectedQuery, actualQueryStr)
}
if tC.expectedType != actualType {
t.Errorf("expected type %s, got type %s", tC.expectedType, actualType)
}
if tC.expectedPrefix != actualPrefix {
t.Errorf("expected prefix %s, got prefix %q", tC.expectedPrefix, actualPrefix)
}
})
}
}