go-arg/parse_test.go

131 lines
2.7 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 (
"strings"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func split(s string) []string {
return strings.Split(s, " ")
}
func TestStringSingle(t *testing.T) {
var args struct {
Foo string
}
2015-10-31 20:26:58 -05:00
err := ParseFrom(split("--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-10-31 20:26:58 -05:00
err := ParseFrom(split("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)
assert.Equal(t, 1.2, args.Spam)
}
func TestRequired(t *testing.T) {
var args struct {
Foo string `arg:"required"`
}
2015-10-31 20:26:58 -05:00
err := ParseFrom(nil, &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-10-31 20:26:58 -05:00
err := ParseFrom(split("-f xyz"), &args)
2015-10-31 18:15:24 -05:00
require.NoError(t, err)
assert.Equal(t, "xyz", args.Foo)
2015-10-31 20:26:58 -05:00
err = ParseFrom(split("-foo xyz"), &args)
2015-10-31 18:15:24 -05:00
require.NoError(t, err)
assert.Equal(t, "xyz", args.Foo)
2015-10-31 20:26:58 -05:00
err = ParseFrom(split("--foo xyz"), &args)
2015-10-31 18:15:24 -05:00
require.NoError(t, err)
assert.Equal(t, "xyz", args.Foo)
}
func TestCaseSensitive(t *testing.T) {
var args struct {
Lower bool `arg:"-v"`
Upper bool `arg:"-V"`
}
2015-10-31 20:26:58 -05:00
err := ParseFrom(split("-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-10-31 20:26:58 -05:00
err := ParseFrom(split("-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-10-31 20:26:58 -05:00
err := ParseFrom(split("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)
}
func TestRequiredPositional(t *testing.T) {
var args struct {
Input string `arg:"positional"`
Output string `arg:"positional,required"`
}
2015-10-31 20:26:58 -05:00
err := ParseFrom(split("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-10-31 20:26:58 -05:00
err := ParseFrom(split("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-10-31 20:26:58 -05:00
err := ParseFrom(split("--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)
}