aboutsummaryrefslogtreecommitdiff
path: root/int_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 /int_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 'int_slice_test.go')
-rw-r--r--int_slice_test.go80
1 files changed, 78 insertions, 2 deletions
diff --git a/int_slice_test.go b/int_slice_test.go
index 32c7cd7..2b7f68d 100644
--- a/int_slice_test.go
+++ b/int_slice_test.go
@@ -17,6 +17,12 @@ func setUpISFlagSet(isp *[]int) *FlagSet {
return f
}
+func setUpISFlagSetWithDefault(isp *[]int) *FlagSet {
+ f := NewFlagSet("test", ContinueOnError)
+ f.IntSliceVar(isp, "is", []int{0, 1}, "Command seperated list!")
+ return f
+}
+
func TestEmptyIS(t *testing.T) {
var is []int
f := setUpISFlagSet(&is)
@@ -27,7 +33,7 @@ func TestEmptyIS(t *testing.T) {
getIS, err := f.GetIntSlice("is")
if err != nil {
- t.Fatal("got an error from GetStringSlice():", err)
+ t.Fatal("got an error from GetIntSlice():", err)
}
if len(getIS) != 0 {
t.Fatalf("got is %v with len=%d but expected length=0", getIS, len(getIS))
@@ -65,6 +71,76 @@ func TestIS(t *testing.T) {
}
}
+func TestISDefault(t *testing.T) {
+ var is []int
+ f := setUpISFlagSetWithDefault(&is)
+
+ vals := []string{"0", "1"}
+
+ err := f.Parse([]string{})
+ if err != nil {
+ t.Fatal("expected no error; got", err)
+ }
+ for i, v := range is {
+ d, err := strconv.Atoi(vals[i])
+ if err != nil {
+ t.Fatalf("got error: %v", err)
+ }
+ if d != v {
+ t.Fatalf("expected is[%d] to be %s but got: %s", i, d, v)
+ }
+ }
+
+ getIS, err := f.GetIntSlice("is")
+ if err != nil {
+ t.Fatal("got an error from GetIntSlice():", err)
+ }
+ for i, v := range getIS {
+ d, err := strconv.Atoi(vals[i])
+ if err != nil {
+ t.Fatal("got an error from GetIntSlice():", err)
+ }
+ if d != v {
+ t.Fatalf("expected is[%d] to be %s from GetIntSlice but got: %s", i, d, v)
+ }
+ }
+}
+
+func TestISWithDefault(t *testing.T) {
+ var is []int
+ f := setUpISFlagSetWithDefault(&is)
+
+ vals := []string{"1", "2"}
+ arg := fmt.Sprintf("--is=%s", strings.Join(vals, ","))
+ err := f.Parse([]string{arg})
+ if err != nil {
+ t.Fatal("expected no error; got", err)
+ }
+ for i, v := range is {
+ d, err := strconv.Atoi(vals[i])
+ if err != nil {
+ t.Fatalf("got error: %v", err)
+ }
+ if d != v {
+ t.Fatalf("expected is[%d] to be %s but got: %s", i, d, v)
+ }
+ }
+
+ getIS, err := f.GetIntSlice("is")
+ if err != nil {
+ t.Fatal("got an error from GetIntSlice():", err)
+ }
+ for i, v := range getIS {
+ d, err := strconv.Atoi(vals[i])
+ if err != nil {
+ t.Fatalf("got error: %v", err)
+ }
+ if d != v {
+ t.Fatalf("expected is[%d] to be %s from GetIntSlice but got: %s", i, d, v)
+ }
+ }
+}
+
func TestISCalledTwice(t *testing.T) {
var is []int
f := setUpISFlagSet(&is)
@@ -80,7 +156,7 @@ func TestISCalledTwice(t *testing.T) {
}
for i, v := range is {
if expected[i] != v {
- t.Fatalf("expected ss[%d] to be %s but got: %s", i, expected[i], v)
+ t.Fatalf("expected is[%d] to be %s but got: %s", i, expected[i], v)
}
}
}