diff options
| author | Eyal Posener <[email protected]> | 2017-11-04 10:21:41 +0200 |
|---|---|---|
| committer | Eyal Posener <[email protected]> | 2017-11-04 10:51:40 +0200 |
| commit | 7ee9623f2b5d4685a91a51d0823275754b4d3a0a (patch) | |
| tree | f45cd54539dd9a03cbb5314bb6a9c0891d013712 /complete_test.go | |
| parent | 88e59760adaddb8276c9b15511302890690e2dae (diff) | |
Filter matches as a final stage
This simplifies the prediction logic writing, the predictor
doesn't need to filter our according to line matching, instead
it returns everything and the filtering is done at the end.
This does not break current behavior.
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 |
