Skip to content

Commit

Permalink
Merge pull request #102 from posener/sub-cmd-bug
Browse files Browse the repository at this point in the history
Fix off-by-one error for argument from method
  • Loading branch information
posener authored Oct 23, 2019
2 parents 2f2ff27 + f7264fe commit 98a0c28
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 23 deletions.
13 changes: 7 additions & 6 deletions args.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,16 +85,17 @@ func splitLastEqual(line []string) []string {
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 {
if i > len(a.All) {
i = len(a.All)
if 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) {
i = len(a.Completed)
if i >= len(a.Completed) {
i = len(a.Completed) - 1
}
a.Completed = a.Completed[i:]
a.Completed = a.Completed[i+1:]
return a
}

Expand Down
12 changes: 5 additions & 7 deletions args_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"fmt"
"strings"
"testing"

"github.com/stretchr/testify/assert"
)

func TestArgs(t *testing.T) {
Expand Down Expand Up @@ -131,15 +133,11 @@ func TestArgs_From(t *testing.T) {
for _, tt := range tests {
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)

if got, want := strings.Join(n.All, " "), tt.newLine; got != want {
t.Errorf("%s failed: all = %q, want %q", t.Name(), got, want)
}
if got, want := strings.Join(n.Completed, " "), tt.newCompleted; got != want {
t.Errorf("%s failed: completed = %q, want %q", t.Name(), got, want)
}
assert.Equal(t, tt.newLine, strings.Join(n.All, " "))
assert.Equal(t, tt.newCompleted, strings.Join(n.Completed, " "))
})
}
}
Expand Down
31 changes: 22 additions & 9 deletions complete_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ func TestCompleter_Complete(t *testing.T) {
"-flag1": PredictAnything,
"-flag2": PredictNothing,
},
Sub: Commands{
"sub11": {},
},
},
"sub2": {
Flags: Flags{
Expand All @@ -28,6 +31,11 @@ func TestCompleter_Complete(t *testing.T) {
},
Args: PredictFiles("*.md"),
},
"sub3": {
Sub: Commands{
"sub3": {},
},
},
},
Flags: Flags{
"-o": PredictFiles("*.txt"),
Expand All @@ -47,7 +55,7 @@ func TestCompleter_Complete(t *testing.T) {
{
line: "cmd ",
point: -1,
want: []string{"sub1", "sub2"},
want: []string{"sub1", "sub2", "sub3"},
},
{
line: "cmd -",
Expand All @@ -57,7 +65,7 @@ func TestCompleter_Complete(t *testing.T) {
{
line: "cmd -h ",
point: -1,
want: []string{"sub1", "sub2"},
want: []string{"sub1", "sub2", "sub3"},
},
{
line: "cmd -global1 ", // global1 is known follow flag
Expand All @@ -67,7 +75,7 @@ func TestCompleter_Complete(t *testing.T) {
{
line: "cmd sub",
point: -1,
want: []string{"sub1", "sub2"},
want: []string{"sub1", "sub2", "sub3"},
},
{
line: "cmd sub1",
Expand All @@ -82,7 +90,12 @@ func TestCompleter_Complete(t *testing.T) {
{
line: "cmd sub1 ",
point: -1,
want: []string{},
want: []string{"sub11"},
},
{
line: "cmd sub3 ",
point: -1,
want: []string{"sub3"},
},
{
line: "cmd sub1 -",
Expand Down Expand Up @@ -142,7 +155,7 @@ func TestCompleter_Complete(t *testing.T) {
{
line: "cmd -no-such-flag ",
point: -1,
want: []string{"sub1", "sub2"},
want: []string{"sub1", "sub2", "sub3"},
},
{
line: "cmd -no-such-flag -",
Expand All @@ -157,7 +170,7 @@ func TestCompleter_Complete(t *testing.T) {
{
line: "cmd no-such-command ",
point: -1,
want: []string{"sub1", "sub2"},
want: []string{"sub1", "sub2", "sub3"},
},
{
line: "cmd -o ",
Expand Down Expand Up @@ -212,12 +225,12 @@ func TestCompleter_Complete(t *testing.T) {
{
line: "cmd -o ./readme.md ",
point: -1,
want: []string{"sub1", "sub2"},
want: []string{"sub1", "sub2", "sub3"},
},
{
line: "cmd -o=./readme.md ",
point: -1,
want: []string{"sub1", "sub2"},
want: []string{"sub1", "sub2", "sub3"},
},
{
line: "cmd -o sub2 -flag3 ",
Expand Down Expand Up @@ -256,7 +269,7 @@ func TestCompleter_Complete(t *testing.T) {
line: "cmd -o ",
// ^
point: 4,
want: []string{"sub1", "sub2"},
want: []string{"sub1", "sub2", "sub3"},
},
}

Expand Down
7 changes: 6 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
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
11 changes: 11 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -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/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/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=

0 comments on commit 98a0c28

Please sign in to comment.