summaryrefslogtreecommitdiff
path: root/parse_test.go
diff options
context:
space:
mode:
authorAlex Flint <[email protected]>2022-06-09 11:21:29 -0400
committerAlex Flint <[email protected]>2022-06-09 11:21:29 -0400
commit23b2b67fe299b63a072a3541f34d57757d0b8df0 (patch)
treeb5abb6cece5d2829bb134cf7e7c0d58216595035 /parse_test.go
parentf0f44b65d1179ccedb4c56f493f97ec569a6654e (diff)
fix issue #184
Diffstat (limited to 'parse_test.go')
-rw-r--r--parse_test.go66
1 files changed, 66 insertions, 0 deletions
diff --git a/parse_test.go b/parse_test.go
index 2d0ef7a..0d58598 100644
--- a/parse_test.go
+++ b/parse_test.go
@@ -2,6 +2,7 @@ package arg
import (
"bytes"
+ "encoding/json"
"fmt"
"net"
"net/mail"
@@ -1456,3 +1457,68 @@ func TestMustParsePrintsVersion(t *testing.T) {
assert.Equal(t, 0, *exitCode)
assert.Equal(t, "example 3.2.1\n", b.String())
}
+
+type jsonMap struct {
+ val map[string]string
+}
+
+func (v *jsonMap) UnmarshalText(data []byte) error {
+ return json.Unmarshal(data, &v.val)
+}
+
+func TestTextUnmarshallerEmpty(t *testing.T) {
+ // based on https://github.com/alexflint/go-arg/issues/184
+ var args struct {
+ Config jsonMap `arg:"--config"`
+ }
+
+ err := parse("", &args)
+ require.NoError(t, err)
+ assert.Empty(t, args.Config)
+}
+
+func TestTextUnmarshallerEmptyPointer(t *testing.T) {
+ // a slight variant on https://github.com/alexflint/go-arg/issues/184
+ var args struct {
+ Config *jsonMap `arg:"--config"`
+ }
+
+ err := parse("", &args)
+ require.NoError(t, err)
+ assert.Nil(t, args.Config)
+}
+
+// similar to the above but also implements MarshalText
+type jsonMap2[T any] struct {
+ val T
+}
+
+func (v *jsonMap2[T]) MarshalText(data []byte) error {
+ return json.Unmarshal(data, &v.val)
+}
+
+func (v *jsonMap2[T]) UnmarshalText(data []byte) error {
+ return json.Unmarshal(data, &v.val)
+}
+
+func TestTextMarshallerUnmarshallerEmpty(t *testing.T) {
+ // based on https://github.com/alexflint/go-arg/issues/184
+ var args struct {
+ Config jsonMap2[map[string]string] `arg:"--config"`
+ }
+
+ err := parse("", &args)
+ require.NoError(t, err)
+ assert.Empty(t, args.Config)
+}
+
+func TestTextMarshallerUnmarshallerEmptyPointer(t *testing.T) {
+ // a slight variant on https://github.com/alexflint/go-arg/issues/184
+ var args struct {
+ Config *jsonMap2[map[string]string] `arg:"--config"`
+ }
+
+ err := parse("", &args)
+ require.NoError(t, err)
+ assert.Nil(t, args.Config)
+}