complete/compflag/compflag_test.go

152 lines
4.8 KiB
Go
Raw Permalink Normal View History

2019-11-13 22:51:44 -06:00
package compflag
import (
"flag"
"testing"
2019-11-27 13:33:55 -06:00
"time"
2019-11-13 22:51:44 -06:00
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"})
})
}
2019-11-27 13:33:55 -06:00
func TestDuration(t *testing.T) {
t.Parallel()
t.Run("options invalid not checked", func(t *testing.T) {
var cmd FlagSet
value := cmd.Duration("a", 0, "", predict.OptValues("1s", "1m"))
err := cmd.Parse([]string{"-a", "1h"})
assert.NoError(t, err)
assert.Equal(t, time.Hour, *value)
})
t.Run("options valid checked", func(t *testing.T) {
var cmd FlagSet
value := cmd.Duration("a", 0, "", predict.OptValues("1s", "1m"), predict.OptCheck())
err := cmd.Parse([]string{"-a", "1m"})
assert.NoError(t, err)
assert.Equal(t, time.Minute, *value)
})
t.Run("options invalid checked", func(t *testing.T) {
var cmd FlagSet
_ = cmd.Duration("a", 0, "", predict.OptValues("1s", "1m"), predict.OptCheck())
err := cmd.Parse([]string{"-a", "1h"})
assert.Error(t, err)
})
t.Run("options invalid duration value", func(t *testing.T) {
var cmd FlagSet
_ = cmd.Duration("a", 0, "", predict.OptValues("1h", "1m", "1"), predict.OptCheck())
err := cmd.Parse([]string{"-a", "1"})
assert.Error(t, err)
})
t.Run("complete", func(t *testing.T) {
var cmd FlagSet
_ = cmd.Duration("a", 0, "", predict.OptValues("1s", "1m"))
complete.Test(t, complete.FlagSet((*flag.FlagSet)(&cmd)), "-a ", []string{"1s", "1m"})
complete.Test(t, complete.FlagSet((*flag.FlagSet)(&cmd)), "-a=", []string{"1s", "1m"})
complete.Test(t, complete.FlagSet((*flag.FlagSet)(&cmd)), "-a 1", []string{"1s", "1m"})
complete.Test(t, complete.FlagSet((*flag.FlagSet)(&cmd)), "-a=1", []string{"1s", "1m"})
complete.Test(t, complete.FlagSet((*flag.FlagSet)(&cmd)), "-a=1m", []string{"1m"})
})
}