2020-12-14 16:07:27 +00:00
|
|
|
package flags
|
2015-08-16 21:34:38 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"flag"
|
|
|
|
"reflect"
|
|
|
|
"testing"
|
2020-12-11 03:26:10 +00:00
|
|
|
|
|
|
|
"github.com/stretchr/testify/require"
|
2015-08-16 21:34:38 +00:00
|
|
|
)
|
|
|
|
|
2015-08-23 23:57:54 +00:00
|
|
|
func TestStringFlag_implements(t *testing.T) {
|
2020-12-11 03:26:10 +00:00
|
|
|
t.Parallel()
|
|
|
|
|
2015-08-16 21:34:38 +00:00
|
|
|
var raw interface{}
|
2015-08-23 23:57:54 +00:00
|
|
|
raw = new(StringFlag)
|
2015-08-16 21:34:38 +00:00
|
|
|
if _, ok := raw.(flag.Value); !ok {
|
2015-08-23 23:57:54 +00:00
|
|
|
t.Fatalf("StringFlag should be a Value")
|
2015-08-16 21:34:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-23 23:57:54 +00:00
|
|
|
func TestStringFlagSet(t *testing.T) {
|
2020-12-11 03:26:10 +00:00
|
|
|
t.Parallel()
|
|
|
|
|
2015-08-23 23:57:54 +00:00
|
|
|
sv := new(StringFlag)
|
2015-08-16 21:34:38 +00:00
|
|
|
err := sv.Set("foo")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = sv.Set("bar")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
expected := []string{"foo", "bar"}
|
|
|
|
if !reflect.DeepEqual([]string(*sv), expected) {
|
|
|
|
t.Fatalf("Bad: %#v", sv)
|
|
|
|
}
|
|
|
|
}
|
2020-12-11 03:26:10 +00:00
|
|
|
func TestStringFlagSet_Append(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
var (
|
|
|
|
// A test to make sure StringFlag can replace AppendSliceValue
|
|
|
|
// for autopilot flags inherited from Consul.
|
|
|
|
hosts StringFlag
|
|
|
|
)
|
|
|
|
|
|
|
|
flagSet := flag.NewFlagSet("test", flag.PanicOnError)
|
|
|
|
flagSet.Var(&hosts, "host", "host, specify more than once")
|
|
|
|
|
|
|
|
args := []string{"-host", "foo", "-host", "bar", "-host", "baz"}
|
|
|
|
err := flagSet.Parse(args)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
result := hosts.String()
|
|
|
|
require.Equal(t, "foo,bar,baz", result)
|
|
|
|
}
|