diff options
| author | Eyal Posener <[email protected]> | 2017-11-04 11:52:49 +0200 |
|---|---|---|
| committer | GitHub <[email protected]> | 2017-11-04 11:52:49 +0200 |
| commit | a1f9ea005b8360153429321071da4e42ce6ce323 (patch) | |
| tree | 0f7976f712f3cb3c70eac8eb78b5027e445e16c8 /complete_test.go | |
| parent | aae7e1e39fb3554591e2d20b4c86fea9bf2b15bc (diff) | |
| parent | 00c86494ff7035cfd62f66042e9ca2b118b90122 (diff) | |
Merge branch 'master' into split-last-equal
Diffstat (limited to 'complete_test.go')
| -rw-r--r-- | complete_test.go | 44 |
1 files changed, 31 insertions, 13 deletions
diff --git a/complete_test.go b/complete_test.go index cd7880c..1611ad4 100644 --- a/complete_test.go +++ b/complete_test.go @@ -1,8 +1,10 @@ package complete import ( + "bytes" "os" "sort" + "strings" "testing" ) @@ -34,6 +36,7 @@ func TestCompleter_Complete(t *testing.T) { "-global1": PredictAnything, }, } + cmp := New("cmd", c) tests := []struct { args string @@ -195,18 +198,13 @@ func TestCompleter_Complete(t *testing.T) { for _, tt := range tests { t.Run(tt.args, func(t *testing.T) { - - tt.args = "cmd " + tt.args - os.Setenv(envComplete, tt.args) - line, _ := getLine() - - got := c.Predict(newArgs(line)) + got := runComplete(cmp, tt.args) sort.Strings(tt.want) sort.Strings(got) if !equalSlices(got, tt.want) { - t.Errorf("failed '%s'\ngot = %s\nwant: %s", t.Name(), got, tt.want) + t.Errorf("failed '%s'\ngot: %s\nwant: %s", t.Name(), got, tt.want) } }) } @@ -242,6 +240,8 @@ func TestCompleter_Complete_SharedPrefix(t *testing.T) { }, } + cmp := New("cmd", c) + tests := []struct { args string want []string @@ -278,12 +278,7 @@ func TestCompleter_Complete_SharedPrefix(t *testing.T) { for _, tt := range tests { t.Run(tt.args, func(t *testing.T) { - - tt.args = "cmd " + tt.args - os.Setenv(envComplete, tt.args) - line, _ := getLine() - - got := c.Predict(newArgs(line)) + got := runComplete(cmp, tt.args) sort.Strings(tt.want) sort.Strings(got) @@ -295,6 +290,29 @@ func TestCompleter_Complete_SharedPrefix(t *testing.T) { } } +// runComplete runs the complete login for test purposes +// it gets the complete struct and command line arguments and returns +// the complete options +func runComplete(c *Complete, args string) (completions []string) { + os.Setenv(envComplete, "cmd "+args) + b := bytes.NewBuffer(nil) + c.Out = b + c.Complete() + completions = parseOutput(b.String()) + return +} + +func parseOutput(output string) []string { + lines := strings.Split(output, "\n") + options := []string{} + for _, l := range lines { + if l != "" { + options = append(options, l) + } + } + return options +} + func equalSlices(a, b []string) bool { if len(a) != len(b) { return false |
