diff options
| author | Alex Flint <[email protected]> | 2016-01-23 21:03:39 -0800 |
|---|---|---|
| committer | Alex Flint <[email protected]> | 2016-01-23 21:03:39 -0800 |
| commit | c0809e537fc01b4a33ced1bf56212837108d7264 (patch) | |
| tree | 4af108e4daa53fd7e2a82d6c72c7f35c50e166a8 /parse_test.go | |
| parent | 93247e2f3bf9921859417154cf91f46b2892d0ed (diff) | |
| parent | 8fee8f7bbe5933cfe3ba8c82479b91a8e777e5a0 (diff) | |
Merge pull request #30 from alexflint/scalar_pointers
add support for pointers and TextUnmarshaler
Diffstat (limited to 'parse_test.go')
| -rw-r--r-- | parse_test.go | 91 |
1 files changed, 90 insertions, 1 deletions
diff --git a/parse_test.go b/parse_test.go index c30809d..a915910 100644 --- a/parse_test.go +++ b/parse_test.go @@ -15,7 +15,11 @@ func parse(cmdline string, dest interface{}) error { if err != nil { return err } - return p.Parse(strings.Split(cmdline, " ")) + var parts []string + if len(cmdline) > 0 { + parts = strings.Split(cmdline, " ") + } + return p.Parse(parts) } func TestString(t *testing.T) { @@ -71,6 +75,25 @@ func TestInvalidDuration(t *testing.T) { require.Error(t, err) } +func TestIntPtr(t *testing.T) { + var args struct { + Foo *int + } + err := parse("--foo 123", &args) + require.NoError(t, err) + require.NotNil(t, args.Foo) + assert.Equal(t, 123, *args.Foo) +} + +func TestIntPtrNotPresent(t *testing.T) { + var args struct { + Foo *int + } + err := parse("", &args) + require.NoError(t, err) + assert.Nil(t, args.Foo) +} + func TestMixed(t *testing.T) { var args struct { Foo string `arg:"-f"` @@ -362,6 +385,14 @@ func TestUnsupportedSliceElement(t *testing.T) { var args struct { Foo []interface{} } + err := parse("--foo 3", &args) + assert.Error(t, err) +} + +func TestUnsupportedSliceElementMissingValue(t *testing.T) { + var args struct { + Foo []interface{} + } err := parse("--foo", &args) assert.Error(t, err) } @@ -452,3 +483,61 @@ func TestEnvironmentVariableRequired(t *testing.T) { MustParse(&args) assert.Equal(t, "bar", args.Foo) } + +type textUnmarshaler struct { + val int +} + +func (f *textUnmarshaler) UnmarshalText(b []byte) error { + f.val = len(b) + return nil +} + +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) +} + +type boolUnmarshaler bool + +func (p *boolUnmarshaler) UnmarshalText(b []byte) error { + *p = len(b)%2 == 0 + return nil +} + +func TestBoolUnmarhsaler(t *testing.T) { + // test that a bool type that implements TextUnmarshaler is + // handled as a TextUnmarshaler not as a bool + var args struct { + Foo *boolUnmarshaler + } + err := parse("--foo ab", &args) + require.NoError(t, err) + assert.EqualValues(t, true, *args.Foo) +} + +type sliceUnmarshaler []int + +func (p *sliceUnmarshaler) UnmarshalText(b []byte) error { + *p = sliceUnmarshaler{len(b)} + return nil +} + +func TestSliceUnmarhsaler(t *testing.T) { + // test that a slice type that implements TextUnmarshaler is + // handled as a TextUnmarshaler not as a slice + var args struct { + Foo *sliceUnmarshaler + Bar string `arg:"positional"` + } + err := parse("--foo abcde xyz", &args) + require.NoError(t, err) + require.Len(t, *args.Foo, 1) + assert.EqualValues(t, 5, (*args.Foo)[0]) + assert.Equal(t, "xyz", args.Bar) +} |
