diff options
| author | Eyal Posener <[email protected]> | 2017-11-04 11:52:09 +0200 |
|---|---|---|
| committer | GitHub <[email protected]> | 2017-11-04 11:52:09 +0200 |
| commit | 00c86494ff7035cfd62f66042e9ca2b118b90122 (patch) | |
| tree | b507c4736c6f8de2e887e76b952961d1c8dcc44c /complete_test.go | |
| parent | 88e59760adaddb8276c9b15511302890690e2dae (diff) | |
| parent | c45e6fe8516b89faca97fc3a485949a07a9530c7 (diff) | |
Merge pull request #53 from posener/finally-filter-matches
Filter matches as a final stage
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 ba4df4a..19bc688 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 @@ -175,18 +178,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) } }) } @@ -222,6 +220,8 @@ func TestCompleter_Complete_SharedPrefix(t *testing.T) { }, } + cmp := New("cmd", c) + tests := []struct { args string want []string @@ -258,12 +258,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) @@ -275,6 +270,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 |
