2017-05-05 08:57:22 -05:00
|
|
|
package complete
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"sort"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestCompleter_Complete(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
2017-05-05 13:57:21 -05:00
|
|
|
if testing.Verbose() {
|
|
|
|
os.Setenv(envDebug, "1")
|
|
|
|
}
|
2017-05-05 08:57:22 -05:00
|
|
|
|
|
|
|
c := Completer{
|
|
|
|
Command: Command{
|
|
|
|
Sub: map[string]Command{
|
|
|
|
"sub1": {
|
2017-05-05 13:59:10 -05:00
|
|
|
Flags: map[string]Predicate{
|
|
|
|
"-flag1": PredictAnything,
|
|
|
|
"-flag2": PredictNothing,
|
2017-05-05 08:57:22 -05:00
|
|
|
},
|
|
|
|
},
|
|
|
|
"sub2": {
|
2017-05-05 13:59:10 -05:00
|
|
|
Flags: map[string]Predicate{
|
|
|
|
"-flag2": PredictNothing,
|
2017-05-05 16:53:03 -05:00
|
|
|
"-flag3": PredictSet("opt1", "opt2", "opt12"),
|
2017-05-05 08:57:22 -05:00
|
|
|
},
|
2017-05-05 15:07:18 -05:00
|
|
|
Args: PredictDirs("./tests/").Or(PredictFiles("./tests/*.md")),
|
2017-05-05 08:57:22 -05:00
|
|
|
},
|
|
|
|
},
|
2017-05-05 13:59:10 -05:00
|
|
|
Flags: map[string]Predicate{
|
|
|
|
"-h": PredictNothing,
|
|
|
|
"-global1": PredictAnything,
|
2017-05-05 15:07:18 -05:00
|
|
|
"-o": PredictFiles("./tests/*.txt"),
|
2017-05-05 08:57:22 -05:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
allGlobals := []string{}
|
|
|
|
for sub := range c.Sub {
|
|
|
|
allGlobals = append(allGlobals, sub)
|
|
|
|
}
|
|
|
|
for flag := range c.Flags {
|
|
|
|
allGlobals = append(allGlobals, flag)
|
|
|
|
}
|
|
|
|
|
2017-05-05 15:07:18 -05:00
|
|
|
testTXTFiles := []string{"./tests/a.txt", "./tests/b.txt", "./tests/c.txt"}
|
|
|
|
|
2017-05-05 08:57:22 -05:00
|
|
|
tests := []struct {
|
|
|
|
args string
|
|
|
|
want []string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
args: "",
|
|
|
|
want: allGlobals,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
args: "-",
|
2017-05-05 13:57:21 -05:00
|
|
|
want: []string{"-h", "-global1", "-o"},
|
2017-05-05 08:57:22 -05:00
|
|
|
},
|
|
|
|
{
|
|
|
|
args: "-h ",
|
|
|
|
want: allGlobals,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
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-05 13:57:21 -05:00
|
|
|
want: []string{"-flag1", "-flag2", "-h", "-global1", "-o"},
|
2017-05-05 08:57:22 -05:00
|
|
|
},
|
|
|
|
{
|
|
|
|
args: "sub2 ",
|
2017-05-05 15:07:18 -05:00
|
|
|
want: []string{"./tests", "-flag2", "-flag3", "-h", "-global1", "-o"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
args: "sub2 tests",
|
|
|
|
want: []string{"./tests", "./tests/readme.md", "./tests/dir"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
args: "sub2 tests/re",
|
|
|
|
want: []string{"./tests/readme.md"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
args: "sub2 -flag2 ",
|
|
|
|
want: []string{"./tests", "-flag2", "-flag3", "-h", "-global1", "-o"},
|
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-05 13:57:21 -05:00
|
|
|
want: []string{"-flag1", "-flag2", "-h", "-global1", "-o"},
|
2017-05-05 08:57:22 -05:00
|
|
|
},
|
|
|
|
{
|
|
|
|
args: "-no-such-flag",
|
|
|
|
want: []string{},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
args: "-no-such-flag ",
|
|
|
|
want: allGlobals,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
args: "no-such-command",
|
|
|
|
want: []string{},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
args: "no-such-command ",
|
|
|
|
want: allGlobals,
|
|
|
|
},
|
2017-05-05 13:57:21 -05:00
|
|
|
{
|
|
|
|
args: "-o ",
|
2017-05-05 15:07:18 -05:00
|
|
|
want: []string{},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
args: "-o ./tes",
|
|
|
|
want: []string{},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
args: "-o tests/",
|
|
|
|
want: testTXTFiles,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
args: "-o tests",
|
|
|
|
want: testTXTFiles,
|
2017-05-05 13:57:21 -05:00
|
|
|
},
|
|
|
|
{
|
2017-05-05 15:07:18 -05:00
|
|
|
args: "-o ./compl",
|
|
|
|
want: []string{},
|
2017-05-05 13:57:21 -05:00
|
|
|
},
|
|
|
|
{
|
2017-05-05 15:07:18 -05:00
|
|
|
args: "-o ./complete.go",
|
|
|
|
want: []string{},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
args: "-o ./complete.go ",
|
|
|
|
want: allGlobals,
|
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)
|
|
|
|
args := getLine()
|
|
|
|
|
|
|
|
got := c.complete(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)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|