From 70c56eff661a679ce7a9826e1d8062a524099355 Mon Sep 17 00:00:00 2001 From: Alex Flint Date: Wed, 4 Nov 2015 10:27:17 -0800 Subject: [PATCH] add more tests --- parse.go | 6 +- parse_test.go | 224 ++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 227 insertions(+), 3 deletions(-) diff --git a/parse.go b/parse.go index 6f90611..923e749 100644 --- a/parse.go +++ b/parse.go @@ -333,10 +333,10 @@ func setSlice(dest reflect.Value, values []string) error { if err := setScalar(v.Elem(), s); err != nil { return err } - if ptr { - v = v.Addr() + if !ptr { + v = v.Elem() } - dest.Set(reflect.Append(dest, v.Elem())) + dest.Set(reflect.Append(dest, v)) } return nil } diff --git a/parse_test.go b/parse_test.go index 4eff473..eb6080c 100644 --- a/parse_test.go +++ b/parse_test.go @@ -1,6 +1,7 @@ package arg import ( + "os" "strings" "testing" @@ -69,6 +70,28 @@ func TestShortFlag(t *testing.T) { assert.Equal(t, "xyz", args.Foo) } +func TestInvalidShortFlag(t *testing.T) { + var args struct { + Foo string `arg:"-foo"` + } + err := parse("", &args) + assert.Error(t, err) +} + +func TestLongFlag(t *testing.T) { + var args struct { + Foo string `arg:"--abc"` + } + + err := parse("-abc xyz", &args) + require.NoError(t, err) + assert.Equal(t, "xyz", args.Foo) + + err = parse("--abc xyz", &args) + require.NoError(t, err) + assert.Equal(t, "xyz", args.Foo) +} + func TestCaseSensitive(t *testing.T) { var args struct { Lower bool `arg:"-v"` @@ -104,6 +127,19 @@ func TestPositional(t *testing.T) { assert.Equal(t, "", args.Output) } +func TestPositionalPointer(t *testing.T) { + var args struct { + Input string `arg:"positional"` + Output []*string `arg:"positional"` + } + err := parse("foo bar baz", &args) + require.NoError(t, err) + assert.Equal(t, "foo", args.Input) + bar := "bar" + baz := "baz" + assert.Equal(t, []*string{&bar, &baz}, args.Output) +} + func TestRequiredPositional(t *testing.T) { var args struct { Input string `arg:"positional"` @@ -132,3 +168,191 @@ func TestMultiple(t *testing.T) { assert.Equal(t, []int{1, 2, 3}, args.Foo) assert.Equal(t, []string{"x", "y", "z"}, args.Bar) } + +func TestMultipleWithEq(t *testing.T) { + var args struct { + Foo []int + Bar []string + } + err := parse("--foo 1 2 3 --bar=x", &args) + require.NoError(t, err) + assert.Equal(t, []int{1, 2, 3}, args.Foo) + assert.Equal(t, []string{"x"}, args.Bar) +} + +func TestExemptField(t *testing.T) { + var args struct { + Foo string + Bar interface{} `arg:"-"` + } + err := parse("--foo xyz", &args) + require.NoError(t, err) + assert.Equal(t, "xyz", args.Foo) +} + +func TestUnknownField(t *testing.T) { + var args struct { + Foo string + } + err := parse("--bar xyz", &args) + assert.Error(t, err) +} + +func TestMissingRequired(t *testing.T) { + var args struct { + Foo string `arg:"required"` + X []string `arg:"positional"` + } + err := parse("x", &args) + assert.Error(t, err) +} + +func TestMissingValue(t *testing.T) { + var args struct { + Foo string + } + err := parse("--foo", &args) + assert.Error(t, err) +} + +func TestInvalidInt(t *testing.T) { + var args struct { + Foo int + } + err := parse("--foo=xyz", &args) + assert.Error(t, err) +} + +func TestInvalidUint(t *testing.T) { + var args struct { + Foo uint + } + err := parse("--foo=xyz", &args) + assert.Error(t, err) +} + +func TestInvalidFloat(t *testing.T) { + var args struct { + Foo float64 + } + err := parse("--foo xyz", &args) + require.Error(t, err) +} + +func TestInvalidBool(t *testing.T) { + var args struct { + Foo bool + } + err := parse("--foo=xyz", &args) + require.Error(t, err) +} + +func TestInvalidIntSlice(t *testing.T) { + var args struct { + Foo []int + } + err := parse("--foo 1 2 xyz", &args) + require.Error(t, err) +} + +func TestInvalidPositional(t *testing.T) { + var args struct { + Foo int `arg:"positional"` + } + err := parse("xyz", &args) + require.Error(t, err) +} + +func TestInvalidPositionalSlice(t *testing.T) { + var args struct { + Foo []int `arg:"positional"` + } + err := parse("1 2 xyz", &args) + require.Error(t, err) +} + +func TestNoMoreOptions(t *testing.T) { + var args struct { + Foo string + Bar []string `arg:"positional"` + } + err := parse("abc -- --foo xyz", &args) + require.NoError(t, err) + assert.Equal(t, "", args.Foo) + assert.Equal(t, []string{"abc", "--foo", "xyz"}, args.Bar) +} + +func TestHelpFlag(t *testing.T) { + var args struct { + Foo string + Bar interface{} `arg:"-"` + } + err := parse("--help", &args) + assert.Equal(t, ErrHelp, err) +} + +func TestPanicOnNonPointer(t *testing.T) { + var args struct{} + assert.Panics(t, func() { + parse("", args) + }) +} + +func TestPanicOnNonStruct(t *testing.T) { + var args string + assert.Panics(t, func() { + parse("", &args) + }) +} + +func TestUnsupportedType(t *testing.T) { + var args struct { + Foo interface{} + } + err := parse("--foo", &args) + assert.Error(t, err) +} + +func TestUnsupportedSliceElement(t *testing.T) { + var args struct { + Foo []interface{} + } + err := parse("--foo", &args) + assert.Error(t, err) +} + +func TestUnknownTag(t *testing.T) { + var args struct { + Foo string `arg:"this_is_not_valid"` + } + err := parse("--foo xyz", &args) + assert.Error(t, err) +} + +func TestParse(t *testing.T) { + var args struct { + Foo string + } + os.Args = []string{"example", "--foo", "bar"} + err := Parse(&args) + require.NoError(t, err) + assert.Equal(t, "bar", args.Foo) +} + +func TestParseError(t *testing.T) { + var args struct { + Foo string `arg:"this_is_not_valid"` + } + os.Args = []string{"example", "--bar"} + err := Parse(&args) + assert.Error(t, err) +} + +func TestMustParse(t *testing.T) { + var args struct { + Foo string + } + os.Args = []string{"example", "--foo", "bar"} + MustParse(&args) + assert.Equal(t, "bar", args.Foo) +}