Ensure IntSlice & StringSlice serialization works as expected

This commit is contained in:
Dan Buch 2016-04-29 02:53:58 -04:00
parent d1b0c49a98
commit 1a91f3dce5
No known key found for this signature in database
GPG Key ID: FAEF12936DD3E3EC
2 changed files with 55 additions and 16 deletions

20
flag.go
View File

@ -141,9 +141,9 @@ func (f *StringSlice) Set(value string) error {
}
if strings.HasPrefix(value, slPfx) {
v := []string{}
_ = json.Unmarshal([]byte(strings.Replace(value, slPfx, "", 1)), v)
f.slice = append(f.slice, v...)
// Deserializing assumes overwrite
_ = json.Unmarshal([]byte(strings.Replace(value, slPfx, "", 1)), &f.slice)
f.hasBeenSet = true
return nil
}
@ -242,10 +242,10 @@ func (i *IntSlice) Set(value string) error {
i.hasBeenSet = true
}
if strings.HasPrefix(slPfx, value) {
v := []int{}
_ = json.Unmarshal([]byte(strings.Replace(value, slPfx, "", 1)), v)
i.slice = append(i.slice, v...)
if strings.HasPrefix(value, slPfx) {
// Deserializing assumes overwrite
_ = json.Unmarshal([]byte(strings.Replace(value, slPfx, "", 1)), &i.slice)
i.hasBeenSet = true
return nil
}
@ -263,6 +263,12 @@ func (i *IntSlice) String() string {
return fmt.Sprintf("%v", i.slice)
}
// Serialized allows IntSlice to fulfill Serializeder
func (i *IntSlice) Serialized() string {
jsonBytes, _ := json.Marshal(i.slice)
return fmt.Sprintf("%s%s", slPfx, string(jsonBytes))
}
// Value returns the slice of ints set by this flag
func (i *IntSlice) Value() []int {
return i.slice

View File

@ -43,7 +43,6 @@ var stringFlagTests = []struct {
}
func TestStringFlagHelpOutput(t *testing.T) {
for _, test := range stringFlagTests {
flag := StringFlag{Name: test.name, Usage: test.usage, Value: test.value}
output := flag.String()
@ -376,11 +375,12 @@ func TestParseMultiStringSlice(t *testing.T) {
StringSliceFlag{Name: "serve, s", Value: NewStringSlice()},
},
Action: func(ctx *Context) {
if !reflect.DeepEqual(ctx.StringSlice("serve"), []string{"10", "20"}) {
t.Errorf("main name not set")
expected := []string{"10", "20"}
if !reflect.DeepEqual(ctx.StringSlice("serve"), expected) {
t.Errorf("main name not set: %v != %v", expected, ctx.StringSlice("serve"))
}
if !reflect.DeepEqual(ctx.StringSlice("s"), []string{"10", "20"}) {
t.Errorf("short name not set")
if !reflect.DeepEqual(ctx.StringSlice("s"), expected) {
t.Errorf("short name not set: %v != %v", expected, ctx.StringSlice("s"))
}
},
}).Run([]string{"run", "-s", "10", "-s", "20"})
@ -392,11 +392,12 @@ func TestParseMultiStringSliceWithDefaults(t *testing.T) {
StringSliceFlag{Name: "serve, s", Value: NewStringSlice("9", "2")},
},
Action: func(ctx *Context) {
if !reflect.DeepEqual(ctx.StringSlice("serve"), []string{"10", "20"}) {
t.Errorf("main name not set: %v", ctx.StringSlice("serve"))
expected := []string{"10", "20"}
if !reflect.DeepEqual(ctx.StringSlice("serve"), expected) {
t.Errorf("main name not set: %v != %v", expected, ctx.StringSlice("serve"))
}
if !reflect.DeepEqual(ctx.StringSlice("s"), []string{"10", "20"}) {
t.Errorf("short name not set: %v", ctx.StringSlice("s"))
if !reflect.DeepEqual(ctx.StringSlice("s"), expected) {
t.Errorf("short name not set: %v != %v", expected, ctx.StringSlice("s"))
}
},
}).Run([]string{"run", "-s", "10", "-s", "20"})
@ -960,3 +961,35 @@ func TestParseGenericFromEnvCascade(t *testing.T) {
}
a.Run([]string{"run"})
}
func TestStringSlice_Serialized_Set(t *testing.T) {
sl0 := NewStringSlice("a", "b")
ser0 := sl0.Serialized()
if len(ser0) < len(slPfx) {
t.Fatalf("serialized shorter than expected: %q", ser0)
}
sl1 := NewStringSlice("c", "d")
sl1.Set(ser0)
if sl0.String() != sl1.String() {
t.Fatalf("pre and post serialization do not match: %v != %v", sl0, sl1)
}
}
func TestIntSlice_Serialized_Set(t *testing.T) {
sl0 := NewIntSlice(1, 2)
ser0 := sl0.Serialized()
if len(ser0) < len(slPfx) {
t.Fatalf("serialized shorter than expected: %q", ser0)
}
sl1 := NewIntSlice(3, 4)
sl1.Set(ser0)
if sl0.String() != sl1.String() {
t.Fatalf("pre and post serialization do not match: %v != %v", sl0, sl1)
}
}