fix off-by-one error for sub command completion.

Fixes #97
This commit is contained in:
Eyal Posener 2019-10-23 22:46:40 +03:00
parent e24ae2ec88
commit f7264fe385
4 changed files with 27 additions and 14 deletions

13
args.go
View File

@ -85,16 +85,17 @@ func splitLastEqual(line []string) []string {
return append(line[:len(line)-1], parts...) return append(line[:len(line)-1], parts...)
} }
// from returns a copy of Args of all arguments after the i'th argument.
func (a Args) from(i int) Args { func (a Args) from(i int) Args {
if i > len(a.All) { if i >= len(a.All) {
i = len(a.All) i = len(a.All) - 1
} }
a.All = a.All[i:] a.All = a.All[i+1:]
if i > len(a.Completed) { if i >= len(a.Completed) {
i = len(a.Completed) i = len(a.Completed) - 1
} }
a.Completed = a.Completed[i:] a.Completed = a.Completed[i+1:]
return a return a
} }

View File

@ -4,6 +4,8 @@ import (
"fmt" "fmt"
"strings" "strings"
"testing" "testing"
"github.com/stretchr/testify/assert"
) )
func TestArgs(t *testing.T) { func TestArgs(t *testing.T) {
@ -131,15 +133,11 @@ func TestArgs_From(t *testing.T) {
for _, tt := range tests { for _, tt := range tests {
t.Run(fmt.Sprintf("%s/%d", tt.line, tt.from), func(t *testing.T) { t.Run(fmt.Sprintf("%s/%d", tt.line, tt.from), func(t *testing.T) {
a := newArgs(tt.line) a := newArgs("cmd " + tt.line)
n := a.from(tt.from) n := a.from(tt.from)
if got, want := strings.Join(n.All, " "), tt.newLine; got != want { assert.Equal(t, tt.newLine, strings.Join(n.All, " "))
t.Errorf("%s failed: all = %q, want %q", t.Name(), got, want) assert.Equal(t, tt.newCompleted, strings.Join(n.Completed, " "))
}
if got, want := strings.Join(n.Completed, " "), tt.newCompleted; got != want {
t.Errorf("%s failed: completed = %q, want %q", t.Name(), got, want)
}
}) })
} }
} }

5
go.mod
View File

@ -1,5 +1,8 @@
module github.com/posener/complete module github.com/posener/complete
require github.com/hashicorp/go-multierror v1.0.0 require (
github.com/hashicorp/go-multierror v1.0.0
github.com/stretchr/testify v1.4.0
)
go 1.13 go 1.13

11
go.sum
View File

@ -1,4 +1,15 @@
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/hashicorp/errwrap v1.0.0 h1:hLrqtEDnRye3+sgx6z4qVLNuviH3MR5aQ0ykNJa/UYA= github.com/hashicorp/errwrap v1.0.0 h1:hLrqtEDnRye3+sgx6z4qVLNuviH3MR5aQ0ykNJa/UYA=
github.com/hashicorp/errwrap v1.0.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4= github.com/hashicorp/errwrap v1.0.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4=
github.com/hashicorp/go-multierror v1.0.0 h1:iVjPR7a6H0tWELX5NxNe7bYopibicUzc7uPribsnS6o= github.com/hashicorp/go-multierror v1.0.0 h1:iVjPR7a6H0tWELX5NxNe7bYopibicUzc7uPribsnS6o=
github.com/hashicorp/go-multierror v1.0.0/go.mod h1:dHtQlpGsu+cZNNAkkCN/P3hoUDHhCYQXV3UM06sGGrk= github.com/hashicorp/go-multierror v1.0.0/go.mod h1:dHtQlpGsu+cZNNAkkCN/P3hoUDHhCYQXV3UM06sGGrk=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=