open-nomad/helper/flags/flag_test.go

60 lines
1.1 KiB
Go
Raw Normal View History

package flags
2015-08-16 21:34:38 +00:00
import (
"flag"
"reflect"
"testing"
"github.com/hashicorp/nomad/ci"
"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) {
ci.Parallel(t)
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) {
ci.Parallel(t)
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)
}
}
func TestStringFlagSet_Append(t *testing.T) {
ci.Parallel(t)
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)
}