blob: 203ff7684d5bea3f1d1d173179300b9bffd07662 [file] [log] [blame]
package flags
import (
"path"
"path/filepath"
"reflect"
"runtime"
"strings"
"testing"
)
type TestComplete struct {
}
func (t *TestComplete) Complete(match string) []string {
options := []string{
"hello world",
"hello universe",
"hello multiverse",
}
ret := make([]string, 0, len(options))
for _, o := range options {
if strings.HasPrefix(o, match) {
ret = append(ret, o)
}
}
return ret
}
var completionTestOptions struct {
Verbose bool `short:"v" long:"verbose"`
Debug bool `short:"d" long:"debug"`
Version bool `long:"version"`
AddCommand struct {
Positional struct {
Filename Filename
} `positional-args:"yes"`
} `command:"add"`
RemoveCommand struct {
Other bool `short:"o"`
File Filename `short:"f" long:"filename"`
} `command:"rm"`
RenameCommand struct {
Completed TestComplete `short:"c" long:"completed"`
} `command:"rename"`
}
func TestCompletion(t *testing.T) {
_, sourcefile, _, _ := runtime.Caller(0)
sourcedir := filepath.Join(filepath.SplitList(path.Dir(sourcefile))...)
excompl := []string{filepath.Join(sourcedir, "completion.go"), filepath.Join(sourcedir, "completion_test.go")}
tests := []struct {
Args []string
Completed []string
}{
{
// Short names
[]string{"-"},
[]string{"-d", "-v"},
},
{
// Short names concatenated
[]string{"-dv"},
[]string{"-dv"},
},
{
// Long names
[]string{"--"},
[]string{"--debug", "--verbose", "--version"},
},
{
// Long names partial
[]string{"--ver"},
[]string{"--verbose", "--version"},
},
{
// Commands
[]string{""},
[]string{"add", "rename", "rm"},
},
{
// Commands partial
[]string{"r"},
[]string{"rename", "rm"},
},
{
// Positional filename
[]string{"add", filepath.Join(sourcedir, "completion")},
excompl,
},
{
// Flag filename
[]string{"rm", "-f", path.Join(sourcedir, "completion")},
excompl,
},
{
// Flag short concat last filename
[]string{"rm", "-of", path.Join(sourcedir, "completion")},
excompl,
},
{
// Flag concat filename
[]string{"rm", "-f" + path.Join(sourcedir, "completion")},
[]string{"-f" + excompl[0], "-f" + excompl[1]},
},
{
// Flag equal concat filename
[]string{"rm", "-f=" + path.Join(sourcedir, "completion")},
[]string{"-f=" + excompl[0], "-f=" + excompl[1]},
},
{
// Flag concat long filename
[]string{"rm", "--filename=" + path.Join(sourcedir, "completion")},
[]string{"--filename=" + excompl[0], "--filename=" + excompl[1]},
},
{
// Flag long filename
[]string{"rm", "--filename", path.Join(sourcedir, "completion")},
excompl,
},
{
// Custom completed
[]string{"rename", "-c", "hello un"},
[]string{"hello universe"},
},
}
p := NewParser(&completionTestOptions, Default)
c := &completion{parser: p}
for _, test := range tests {
ret := c.complete(test.Args)
if !reflect.DeepEqual(ret, test.Completed) {
t.Errorf("Args: %#v\n Expected: %#v\n Got: %#v", test.Args, test.Completed, ret)
}
}
}