arg/parse_test.go

359 lines
6.8 KiB
Go
Raw Normal View History

2015-10-31 20:26:58 -05:00
package arg
2015-10-31 18:15:24 -05:00
import (
2015-11-04 12:27:17 -06:00
"os"
2015-10-31 18:15:24 -05:00
"strings"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
2015-11-01 01:57:26 -05:00
func parse(cmdline string, dest interface{}) error {
p, err := NewParser(dest)
if err != nil {
return err
}
return p.Parse(strings.Split(cmdline, " "))
2015-10-31 18:15:24 -05:00
}
func TestStringSingle(t *testing.T) {
var args struct {
Foo string
}
2015-11-01 01:57:26 -05:00
err := parse("--foo bar", &args)
2015-10-31 18:15:24 -05:00
require.NoError(t, err)
assert.Equal(t, "bar", args.Foo)
}
func TestMixed(t *testing.T) {
var args struct {
Foo string `arg:"-f"`
Bar int
2015-10-31 19:05:14 -05:00
Baz uint `arg:"positional"`
2015-10-31 18:15:24 -05:00
Ham bool
Spam float32
}
args.Bar = 3
2015-11-01 01:57:26 -05:00
err := parse("123 -spam=1.2 -ham -f xyz", &args)
2015-10-31 18:15:24 -05:00
require.NoError(t, err)
assert.Equal(t, "xyz", args.Foo)
assert.Equal(t, 3, args.Bar)
2015-10-31 19:05:14 -05:00
assert.Equal(t, uint(123), args.Baz)
2015-10-31 18:15:24 -05:00
assert.Equal(t, true, args.Ham)
2015-11-01 15:38:04 -06:00
assert.EqualValues(t, 1.2, args.Spam)
2015-10-31 18:15:24 -05:00
}
func TestRequired(t *testing.T) {
var args struct {
Foo string `arg:"required"`
}
2015-11-01 01:57:26 -05:00
err := parse("", &args)
2015-10-31 18:15:24 -05:00
require.Error(t, err, "--foo is required")
}
func TestShortFlag(t *testing.T) {
var args struct {
Foo string `arg:"-f"`
}
2015-11-01 01:57:26 -05:00
err := parse("-f xyz", &args)
2015-10-31 18:15:24 -05:00
require.NoError(t, err)
assert.Equal(t, "xyz", args.Foo)
2015-11-01 01:57:26 -05:00
err = parse("-foo xyz", &args)
2015-10-31 18:15:24 -05:00
require.NoError(t, err)
assert.Equal(t, "xyz", args.Foo)
2015-11-01 01:57:26 -05:00
err = parse("--foo xyz", &args)
2015-10-31 18:15:24 -05:00
require.NoError(t, err)
assert.Equal(t, "xyz", args.Foo)
}
2015-11-04 12:27:17 -06:00
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)
}
2015-10-31 18:15:24 -05:00
func TestCaseSensitive(t *testing.T) {
var args struct {
Lower bool `arg:"-v"`
Upper bool `arg:"-V"`
}
2015-11-01 01:57:26 -05:00
err := parse("-v", &args)
2015-10-31 18:15:24 -05:00
require.NoError(t, err)
assert.True(t, args.Lower)
assert.False(t, args.Upper)
}
func TestCaseSensitive2(t *testing.T) {
var args struct {
Lower bool `arg:"-v"`
Upper bool `arg:"-V"`
}
2015-11-01 01:57:26 -05:00
err := parse("-V", &args)
2015-10-31 18:15:24 -05:00
require.NoError(t, err)
assert.False(t, args.Lower)
assert.True(t, args.Upper)
}
2015-10-31 19:05:14 -05:00
func TestPositional(t *testing.T) {
var args struct {
Input string `arg:"positional"`
Output string `arg:"positional"`
}
2015-11-01 01:57:26 -05:00
err := parse("foo", &args)
2015-10-31 19:05:14 -05:00
require.NoError(t, err)
assert.Equal(t, "foo", args.Input)
assert.Equal(t, "", args.Output)
}
2015-11-04 12:27:17 -06:00
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)
}
2015-10-31 19:05:14 -05:00
func TestRequiredPositional(t *testing.T) {
var args struct {
Input string `arg:"positional"`
Output string `arg:"positional,required"`
}
2015-11-01 01:57:26 -05:00
err := parse("foo", &args)
2015-10-31 19:05:14 -05:00
assert.Error(t, err)
}
func TestTooManyPositional(t *testing.T) {
var args struct {
Input string `arg:"positional"`
Output string `arg:"positional"`
}
2015-11-01 01:57:26 -05:00
err := parse("foo bar baz", &args)
2015-10-31 19:05:14 -05:00
assert.Error(t, err)
}
func TestMultiple(t *testing.T) {
var args struct {
Foo []int
Bar []string
}
2015-11-01 01:57:26 -05:00
err := parse("--foo 1 2 3 --bar x y z", &args)
2015-10-31 19:05:14 -05:00
require.NoError(t, err)
assert.Equal(t, []int{1, 2, 3}, args.Foo)
assert.Equal(t, []string{"x", "y", "z"}, args.Bar)
}
2015-11-04 12:27:17 -06:00
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)
}