complete/complete_test.go

198 lines
3.6 KiB
Go
Raw Normal View History

2017-05-05 08:57:22 -05:00
package complete
import (
"os"
"sort"
"testing"
)
func TestCompleter_Complete(t *testing.T) {
t.Parallel()
2017-05-06 15:06:58 -05:00
initTests()
2017-05-05 08:57:22 -05:00
2017-05-06 00:24:17 -05:00
c := Command{
2017-05-15 11:32:59 -05:00
Sub: Commands{
2017-05-06 00:24:17 -05:00
"sub1": {
2017-05-15 11:32:59 -05:00
Flags: Flags{
2017-05-06 00:24:17 -05:00
"-flag1": PredictAnything,
"-flag2": PredictNothing,
2017-05-05 08:57:22 -05:00
},
},
2017-05-06 00:24:17 -05:00
"sub2": {
2017-05-15 11:32:59 -05:00
Flags: Flags{
2017-05-06 00:24:17 -05:00
"-flag2": PredictNothing,
"-flag3": PredictSet("opt1", "opt2", "opt12"),
},
Args: PredictFiles("*.md"),
2017-05-05 08:57:22 -05:00
},
},
2017-05-15 11:32:59 -05:00
Flags: Flags{
"-o": PredictFiles("*.txt"),
},
GlobalFlags: Flags{
2017-05-06 00:24:17 -05:00
"-h": PredictNothing,
"-global1": PredictAnything,
},
2017-05-05 08:57:22 -05:00
}
tests := []struct {
args string
want []string
}{
{
args: "",
2017-05-15 11:32:59 -05:00
want: []string{"sub1", "sub2", "-h", "-global1", "-o"},
2017-05-05 08:57:22 -05:00
},
{
args: "-",
2017-05-05 13:57:21 -05:00
want: []string{"-h", "-global1", "-o"},
2017-05-05 08:57:22 -05:00
},
{
args: "-h ",
2017-05-15 11:32:59 -05:00
want: []string{"sub1", "sub2", "-h", "-global1", "-o"},
2017-05-05 08:57:22 -05:00
},
{
args: "-global1 ", // global1 is known follow flag
want: []string{},
},
{
args: "sub",
want: []string{"sub1", "sub2"},
},
{
args: "sub1",
want: []string{"sub1"},
},
{
args: "sub2",
want: []string{"sub2"},
},
{
args: "sub1 ",
2017-05-15 11:32:59 -05:00
want: []string{"-flag1", "-flag2", "-h", "-global1"},
2017-05-05 08:57:22 -05:00
},
{
args: "sub2 ",
2017-05-15 15:52:04 -05:00
want: []string{"./", "dir/", "outer/", "readme.md", "-flag2", "-flag3", "-h", "-global1"},
},
{
args: "sub2 ./",
2017-05-12 16:40:26 -05:00
want: []string{"./", "./readme.md", "./dir/", "./outer/"},
},
{
args: "sub2 re",
2017-05-15 15:52:04 -05:00
want: []string{"readme.md"},
},
{
args: "sub2 ./re",
want: []string{"./readme.md"},
},
{
args: "sub2 -flag2 ",
2017-05-15 15:52:04 -05:00
want: []string{"./", "dir/", "outer/", "readme.md", "-flag2", "-flag3", "-h", "-global1"},
2017-05-05 08:57:22 -05:00
},
{
args: "sub1 -fl",
want: []string{"-flag1", "-flag2"},
},
{
args: "sub1 -flag1",
want: []string{"-flag1"},
},
{
args: "sub1 -flag1 ",
want: []string{}, // flag1 is unknown follow flag
},
{
args: "sub1 -flag2 ",
2017-05-15 11:32:59 -05:00
want: []string{"-flag1", "-flag2", "-h", "-global1"},
2017-05-05 08:57:22 -05:00
},
{
args: "-no-such-flag",
want: []string{},
},
{
args: "-no-such-flag ",
2017-05-15 11:32:59 -05:00
want: []string{"sub1", "sub2", "-h", "-global1", "-o"},
2017-05-05 08:57:22 -05:00
},
{
args: "no-such-command",
want: []string{},
},
{
args: "no-such-command ",
2017-05-15 11:32:59 -05:00
want: []string{"sub1", "sub2", "-h", "-global1", "-o"},
2017-05-05 08:57:22 -05:00
},
2017-05-05 13:57:21 -05:00
{
args: "-o ",
2017-05-15 15:52:04 -05:00
want: []string{"a.txt", "b.txt", "c.txt", ".dot.txt", "./", "dir/", "outer/"},
},
{
args: "-o ./no-su",
want: []string{},
},
{
args: "-o ./",
2017-05-15 15:52:04 -05:00
want: []string{"./a.txt", "./b.txt", "./c.txt", "./.dot.txt", "./", "./dir/", "./outer/"},
},
{
args: "-o .",
want: []string{"./a.txt", "./b.txt", "./c.txt", "./.dot.txt", "./", "./dir/", "./outer/"},
2017-05-05 13:57:21 -05:00
},
{
args: "-o ./read",
want: []string{},
2017-05-05 13:57:21 -05:00
},
{
args: "-o ./readme.md",
want: []string{},
},
{
args: "-o ./readme.md ",
2017-05-15 11:32:59 -05:00
want: []string{"sub1", "sub2", "-h", "-global1", "-o"},
2017-05-05 13:57:21 -05:00
},
2017-05-05 16:53:03 -05:00
{
args: "-o sub2 -flag3 ",
want: []string{"opt1", "opt2", "opt12"},
},
{
args: "-o sub2 -flag3 opt1",
want: []string{"opt1", "opt12"},
},
{
args: "-o sub2 -flag3 opt",
want: []string{"opt1", "opt2", "opt12"},
},
2017-05-05 08:57:22 -05:00
}
for _, tt := range tests {
t.Run(tt.args, func(t *testing.T) {
tt.args = "cmd " + tt.args
os.Setenv(envComplete, tt.args)
2017-05-11 10:49:59 -05:00
line, _ := getLine()
2017-05-05 08:57:22 -05:00
2017-05-11 12:28:31 -05:00
got := c.Predict(newArgs(line))
2017-05-05 08:57:22 -05:00
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)
}
})
}
}
func equalSlices(a, b []string) bool {
if len(a) != len(b) {
return false
}
for i := range a {
if a[i] != b[i] {
return false
}
}
return true
}