parse_test: add tests covering new TextUnamarshaler value support
Signed-off-by: Pavel Borzenkov <pavel.borzenkov@gmail.com>
This commit is contained in:
parent
96b097bef3
commit
f1aabd5026
|
@ -644,6 +644,16 @@ func (f *textUnmarshaler) UnmarshalText(b []byte) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestTextUnmarshaler(t *testing.T) {
|
func TestTextUnmarshaler(t *testing.T) {
|
||||||
|
// fields that implement TextUnmarshaler should be parsed using that interface
|
||||||
|
var args struct {
|
||||||
|
Foo textUnmarshaler
|
||||||
|
}
|
||||||
|
err := parse("--foo abc", &args)
|
||||||
|
require.NoError(t, err)
|
||||||
|
assert.Equal(t, 3, args.Foo.val)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestPtrToTextUnmarshaler(t *testing.T) {
|
||||||
// fields that implement TextUnmarshaler should be parsed using that interface
|
// fields that implement TextUnmarshaler should be parsed using that interface
|
||||||
var args struct {
|
var args struct {
|
||||||
Foo *textUnmarshaler
|
Foo *textUnmarshaler
|
||||||
|
@ -654,6 +664,19 @@ func TestTextUnmarshaler(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestRepeatedTextUnmarshaler(t *testing.T) {
|
func TestRepeatedTextUnmarshaler(t *testing.T) {
|
||||||
|
// fields that implement TextUnmarshaler should be parsed using that interface
|
||||||
|
var args struct {
|
||||||
|
Foo []textUnmarshaler
|
||||||
|
}
|
||||||
|
err := parse("--foo abc d ef", &args)
|
||||||
|
require.NoError(t, err)
|
||||||
|
require.Len(t, args.Foo, 3)
|
||||||
|
assert.Equal(t, 3, args.Foo[0].val)
|
||||||
|
assert.Equal(t, 1, args.Foo[1].val)
|
||||||
|
assert.Equal(t, 2, args.Foo[2].val)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestRepeatedPtrToTextUnmarshaler(t *testing.T) {
|
||||||
// fields that implement TextUnmarshaler should be parsed using that interface
|
// fields that implement TextUnmarshaler should be parsed using that interface
|
||||||
var args struct {
|
var args struct {
|
||||||
Foo []*textUnmarshaler
|
Foo []*textUnmarshaler
|
||||||
|
@ -667,6 +690,19 @@ func TestRepeatedTextUnmarshaler(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestPositionalTextUnmarshaler(t *testing.T) {
|
func TestPositionalTextUnmarshaler(t *testing.T) {
|
||||||
|
// fields that implement TextUnmarshaler should be parsed using that interface
|
||||||
|
var args struct {
|
||||||
|
Foo []textUnmarshaler `arg:"positional"`
|
||||||
|
}
|
||||||
|
err := parse("abc d ef", &args)
|
||||||
|
require.NoError(t, err)
|
||||||
|
require.Len(t, args.Foo, 3)
|
||||||
|
assert.Equal(t, 3, args.Foo[0].val)
|
||||||
|
assert.Equal(t, 1, args.Foo[1].val)
|
||||||
|
assert.Equal(t, 2, args.Foo[2].val)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestPositionalPtrToTextUnmarshaler(t *testing.T) {
|
||||||
// fields that implement TextUnmarshaler should be parsed using that interface
|
// fields that implement TextUnmarshaler should be parsed using that interface
|
||||||
var args struct {
|
var args struct {
|
||||||
Foo []*textUnmarshaler `arg:"positional"`
|
Foo []*textUnmarshaler `arg:"positional"`
|
||||||
|
|
Loading…
Reference in New Issue