Documentation
¶
Overview ¶
Package cmptest includes helper functions to validate tab completion
Testing this interactively is possible, but generally tedious. Writing unittests provide a much quicker feedback loop to simulate a user.
There are examples in the godoc.
Example ¶
package main import ( "maps" "slices" "testing" "github.com/spf13/cobra" "github.com/coxley/complete" "github.com/coxley/complete/args" "github.com/coxley/complete/cmpcobra" "github.com/coxley/complete/cmptest" "github.com/coxley/complete/predict" ) func TestBasic(t *testing.T) { cp := complete.NopParser(complete.Command{ Sub: nil, Flags: complete.Flags{ "--force": predict.Nothing, "--help": predict.Nothing, "--version": predict.Nothing, "--target-directory": predict.Dirs("*"), }, Args: predict.Files("*"), }) cmptest.Assert(t, cp, "root --", slices.Collect(maps.Keys(cp.Command().Flags))) } func TestCustomPredictor(t *testing.T) { cp := complete.NopParser(complete.Command{ Sub: nil, Flags: complete.Flags{ "--names": predict.Func(func(args.Args) []string { return []string{"coxley", "posener"} }), }, }) cmptest.Assert(t, cp, "root --names <TAB>", []string{"coxley", "posener"}) } func TestCobra(t *testing.T) { cmd := &cobra.Command{ Use: "count", ValidArgs: []string{"one", "two", "three"}, CompletionOptions: cobra.CompletionOptions{ DisableDefaultCmd: true, }, } cmptest.Assert(t, cmpcobra.New(cmd), "count <TAB>", []string{"one", "two", "three"}) } func main() { // Example functions can't actually run tests themselves so leaving this empty. // // Just use the testcases as examples _ = TestBasic _ = TestCustomPredictor _ = TestCobra }
Output:
Index ¶
Examples ¶
Constants ¶
const TabMarker = "<TAB>"
TabMarker can be placed in the prompt to show where the hypothetical user pressed TAB
Variables ¶
This section is empty.
Functions ¶
func Assert ¶
Assert that the parser returns the correct suggestions given the prompt
See the docs for Suggestions on how the prompt should look
func Suggestions ¶
Suggestions returns the options returned by the parser with a given prompt
The prompt should look like it would on the command-line. If '<TAB>' is included, that is where we will assume the user pressed the tab key. The end of the prompt is used otherwise.
Example: "mycli sub --<TAB> --other"
Types ¶
This section is empty.