aboutsummaryrefslogtreecommitdiff
path: root/string_slice_test.go
diff options
context:
space:
mode:
authorEric Paris <[email protected]>2015-08-12 10:54:26 -0400
committerEric Paris <[email protected]>2015-08-12 11:06:13 -0400
commit95a6a40798df11df014298af8a8250af515fa753 (patch)
treea06e3ca530e046d0b72d55ffa06db19d75a34bb0 /string_slice_test.go
parent41e9136667ee4f9ffd03da380e24629d7eccace4 (diff)
Do not append to default values in {String,Int}Slice
I added the ability to do: `-s=bob -s=john` and get `[]string{"bob", "john"}` But if a default value was set to say `[]string{"eric"}` the above operation was mistakenly resulting in: `[]string{"eric", "bob", "john"} which was obviously not what was intended. This is fixed by tracking if a value was parsed and overwriting the default on the first time -s is found, but we append the 2+ time.
Diffstat (limited to 'string_slice_test.go')
-rw-r--r--string_slice_test.go60
1 files changed, 60 insertions, 0 deletions
diff --git a/string_slice_test.go b/string_slice_test.go
index 63eb9de..e8847d5 100644
--- a/string_slice_test.go
+++ b/string_slice_test.go
@@ -16,6 +16,12 @@ func setUpSSFlagSet(ssp *[]string) *FlagSet {
return f
}
+func setUpSSFlagSetWithDefault(ssp *[]string) *FlagSet {
+ f := NewFlagSet("test", ContinueOnError)
+ f.StringSliceVar(ssp, "ss", []string{"default", "values"}, "Command seperated list!")
+ return f
+}
+
func TestEmptySS(t *testing.T) {
var ss []string
f := setUpSSFlagSet(&ss)
@@ -60,6 +66,60 @@ func TestSS(t *testing.T) {
}
}
+func TestSSDefault(t *testing.T) {
+ var ss []string
+ f := setUpSSFlagSetWithDefault(&ss)
+
+ vals := []string{"default", "values"}
+
+ err := f.Parse([]string{})
+ if err != nil {
+ t.Fatal("expected no error; got", err)
+ }
+ for i, v := range ss {
+ if vals[i] != v {
+ t.Fatalf("expected ss[%d] to be %s but got: %s", i, vals[i], v)
+ }
+ }
+
+ getSS, err := f.GetStringSlice("ss")
+ if err != nil {
+ t.Fatal("got an error from GetStringSlice():", err)
+ }
+ for i, v := range getSS {
+ if vals[i] != v {
+ t.Fatalf("expected ss[%d] to be %s from GetStringSlice but got: %s", i, vals[i], v)
+ }
+ }
+}
+
+func TestSSWithDefault(t *testing.T) {
+ var ss []string
+ f := setUpSSFlagSetWithDefault(&ss)
+
+ vals := []string{"one", "two", "4", "3"}
+ arg := fmt.Sprintf("--ss=%s", strings.Join(vals, ","))
+ err := f.Parse([]string{arg})
+ if err != nil {
+ t.Fatal("expected no error; got", err)
+ }
+ for i, v := range ss {
+ if vals[i] != v {
+ t.Fatalf("expected ss[%d] to be %s but got: %s", i, vals[i], v)
+ }
+ }
+
+ getSS, err := f.GetStringSlice("ss")
+ if err != nil {
+ t.Fatal("got an error from GetStringSlice():", err)
+ }
+ for i, v := range getSS {
+ if vals[i] != v {
+ t.Fatalf("expected ss[%d] to be %s from GetStringSlice but got: %s", i, vals[i], v)
+ }
+ }
+}
+
func TestSSCalledTwice(t *testing.T) {
var ss []string
f := setUpSSFlagSet(&ss)