diff options
| -rw-r--r-- | int_slice.go | 4 | ||||
| -rw-r--r-- | int_slice_test.go | 17 | ||||
| -rw-r--r-- | string_slice.go | 4 | ||||
| -rw-r--r-- | string_slice_test.go | 17 |
4 files changed, 42 insertions, 0 deletions
diff --git a/int_slice.go b/int_slice.go index 69e7e7c..b28353d 100644 --- a/int_slice.go +++ b/int_slice.go @@ -43,6 +43,10 @@ func (s *intSliceValue) String() string { func intSliceConv(val string) (interface{}, error) { val = strings.Trim(val, "[]") + // Empty string would cause a slice with one (empty) entry + if len(val) == 0 { + return []int{}, nil + } ss := strings.Split(val, ",") out := make([]int, len(ss)) for i, d := range ss { diff --git a/int_slice_test.go b/int_slice_test.go index 927e7f4..32c7cd7 100644 --- a/int_slice_test.go +++ b/int_slice_test.go @@ -17,6 +17,23 @@ func setUpISFlagSet(isp *[]int) *FlagSet { return f } +func TestEmptyIS(t *testing.T) { + var is []int + f := setUpISFlagSet(&is) + err := f.Parse([]string{}) + if err != nil { + t.Fatal("expected no error; got", err) + } + + getIS, err := f.GetIntSlice("is") + if err != nil { + t.Fatal("got an error from GetStringSlice():", err) + } + if len(getIS) != 0 { + t.Fatalf("got is %v with len=%d but expected length=0", getIS, len(getIS)) + } +} + func TestIS(t *testing.T) { var is []int f := setUpISFlagSet(&is) diff --git a/string_slice.go b/string_slice.go index 20d94ba..2037bd8 100644 --- a/string_slice.go +++ b/string_slice.go @@ -25,6 +25,10 @@ func (s *stringSliceValue) String() string { return "[" + strings.Join(*s, ",") func stringSliceConv(sval string) (interface{}, error) { sval = strings.Trim(sval, "[]") + // An empty string would cause a slice with one (empty) string + if len(sval) == 0 { + return []string{}, nil + } v := strings.Split(sval, ",") return v, nil } diff --git a/string_slice_test.go b/string_slice_test.go index d5c0d03..63eb9de 100644 --- a/string_slice_test.go +++ b/string_slice_test.go @@ -16,6 +16,23 @@ func setUpSSFlagSet(ssp *[]string) *FlagSet { return f } +func TestEmptySS(t *testing.T) { + var ss []string + f := setUpSSFlagSet(&ss) + err := f.Parse([]string{}) + if err != nil { + t.Fatal("expected no error; got", err) + } + + getSS, err := f.GetStringSlice("ss") + if err != nil { + t.Fatal("got an error from GetStringSlice():", err) + } + if len(getSS) != 0 { + t.Fatalf("got ss %v with len=%d but expected length=0", getSS, len(getSS)) + } +} + func TestSS(t *testing.T) { var ss []string f := setUpSSFlagSet(&ss) |
