complete/compflag/compflag_test.go

107 lines
3.3 KiB
Go
Raw Normal View History

2019-11-13 22:51:44 -06:00
package compflag
import (
"flag"
"testing"
2019-11-17 17:25:16 -06:00
"github.com/posener/complete/v2"
"github.com/posener/complete/v2/predict"
2019-11-13 22:51:44 -06:00
"github.com/stretchr/testify/assert"
)
func TestBool(t *testing.T) {
t.Parallel()
t.Run("complete default off", func(t *testing.T) {
var cmd FlagSet
_ = cmd.Bool("a", false, "")
complete.Test(t, complete.FlagSet((*flag.FlagSet)(&cmd)), "-a ", []string{"-a", "-h"})
})
t.Run("complete default on", func(t *testing.T) {
var cmd FlagSet
_ = cmd.Bool("a", true, "")
complete.Test(t, complete.FlagSet((*flag.FlagSet)(&cmd)), "-a ", []string{"false"})
complete.Test(t, complete.FlagSet((*flag.FlagSet)(&cmd)), "-a=", []string{"false"})
})
}
func TestString(t *testing.T) {
t.Parallel()
t.Run("options invalid not checked", func(t *testing.T) {
var cmd FlagSet
value := cmd.String("a", "", "", predict.OptValues("1", "2"))
2019-11-13 22:51:44 -06:00
err := cmd.Parse([]string{"-a", "3"})
assert.NoError(t, err)
assert.Equal(t, "3", *value)
})
t.Run("options valid checked", func(t *testing.T) {
var cmd FlagSet
value := cmd.String("a", "", "", predict.OptValues("1", "2"), predict.OptCheck())
2019-11-13 22:51:44 -06:00
err := cmd.Parse([]string{"-a", "2"})
assert.NoError(t, err)
assert.Equal(t, "2", *value)
})
t.Run("options invalid checked", func(t *testing.T) {
var cmd FlagSet
_ = cmd.String("a", "", "", predict.OptValues("1", "2"), predict.OptCheck())
2019-11-13 22:51:44 -06:00
err := cmd.Parse([]string{"-a", "3"})
assert.Error(t, err)
})
t.Run("complete", func(t *testing.T) {
var cmd FlagSet
_ = cmd.String("a", "", "", predict.OptValues("1", "2"))
2019-11-13 22:51:44 -06:00
complete.Test(t, complete.FlagSet((*flag.FlagSet)(&cmd)), "-a ", []string{"1", "2"})
complete.Test(t, complete.FlagSet((*flag.FlagSet)(&cmd)), "-a=", []string{"1", "2"})
complete.Test(t, complete.FlagSet((*flag.FlagSet)(&cmd)), "-a 1", []string{"1"})
complete.Test(t, complete.FlagSet((*flag.FlagSet)(&cmd)), "-a=1", []string{"1"})
})
}
func TestInt(t *testing.T) {
t.Parallel()
t.Run("options invalid not checked", func(t *testing.T) {
var cmd FlagSet
value := cmd.Int("a", 0, "", predict.OptValues("1", "2"))
2019-11-13 22:51:44 -06:00
err := cmd.Parse([]string{"-a", "3"})
assert.NoError(t, err)
assert.Equal(t, 3, *value)
})
t.Run("options valid checked", func(t *testing.T) {
var cmd FlagSet
value := cmd.Int("a", 0, "", predict.OptValues("1", "2"), predict.OptCheck())
2019-11-13 22:51:44 -06:00
err := cmd.Parse([]string{"-a", "2"})
assert.NoError(t, err)
assert.Equal(t, 2, *value)
})
t.Run("options invalid checked", func(t *testing.T) {
var cmd FlagSet
_ = cmd.Int("a", 0, "", predict.OptValues("1", "2"), predict.OptCheck())
2019-11-13 22:51:44 -06:00
err := cmd.Parse([]string{"-a", "3"})
assert.Error(t, err)
})
t.Run("options invalid int value", func(t *testing.T) {
var cmd FlagSet
_ = cmd.Int("a", 0, "", predict.OptValues("1", "2", "x"), predict.OptCheck())
2019-11-13 22:51:44 -06:00
err := cmd.Parse([]string{"-a", "x"})
assert.Error(t, err)
})
t.Run("complete", func(t *testing.T) {
var cmd FlagSet
_ = cmd.Int("a", 0, "", predict.OptValues("1", "2"))
2019-11-13 22:51:44 -06:00
complete.Test(t, complete.FlagSet((*flag.FlagSet)(&cmd)), "-a ", []string{"1", "2"})
complete.Test(t, complete.FlagSet((*flag.FlagSet)(&cmd)), "-a=", []string{"1", "2"})
complete.Test(t, complete.FlagSet((*flag.FlagSet)(&cmd)), "-a 1", []string{"1"})
complete.Test(t, complete.FlagSet((*flag.FlagSet)(&cmd)), "-a=1", []string{"1"})
})
}