adding unit tests for ToStringMapString and ToStringMapStringSlice methods

This commit is contained in:
jackspirou 2015-07-31 13:44:36 -05:00 committed by spf13
parent 7ed57e5c41
commit d7ba796a5f
2 changed files with 85 additions and 27 deletions

View File

@ -74,8 +74,43 @@ func TestErrorToString(t *testing.T) {
func TestMaps(t *testing.T) {
var taxonomies = map[interface{}]interface{}{"tag": "tags", "group": "groups"}
var stringMapBool = map[interface{}]interface{}{"v1": true, "v2": false}
// ToStringMapString inputs/outputs
var stringMapString = map[string]string{"key 1": "value 1", "key 2": "value 2", "key 3": "value 3"}
var stringMapInterface = map[string]interface{}{"key 1": "value 1", "key 2": "value 2", "key 3": "value 3"}
var interfaceMapString = map[interface{}]string{"key 1": "value 1", "key 2": "value 2", "key 3": "value 3"}
var interfaceMapInterface = map[interface{}]interface{}{"key 1": "value 1", "key 2": "value 2", "key 3": "value 3"}
// ToStringMapStringSlice inputs/outputs
var stringMapStringSlice = map[string][]string{"key 1": []string{"value 1", "value 2", "value 3"}, "key 2": []string{"value 1", "value 2", "value 3"}, "key 3": []string{"value 1", "value 2", "value 3"}}
var stringMapInterfaceSlice = map[string][]interface{}{"key 1": []interface{}{"value 1", "value 2", "value 3"}, "key 2": []interface{}{"value 1", "value 2", "value 3"}, "key 3": []interface{}{"value 1", "value 2", "value 3"}}
var stringMapStringSingleSliceFieldsResult = map[string][]string{"key 1": []string{"value", "1"}, "key 2": []string{"value", "2"}, "key 3": []string{"value", "3"}}
var interfaceMapStringSlice = map[interface{}][]string{"key 1": []string{"value 1", "value 2", "value 3"}, "key 2": []string{"value 1", "value 2", "value 3"}, "key 3": []string{"value 1", "value 2", "value 3"}}
var interfaceMapInterfaceSlice = map[interface{}][]interface{}{"key 1": []interface{}{"value 1", "value 2", "value 3"}, "key 2": []interface{}{"value 1", "value 2", "value 3"}, "key 3": []interface{}{"value 1", "value 2", "value 3"}}
var stringMapStringSliceMultiple = map[string][]string{"key 1": []string{"value 1", "value 2", "value 3"}, "key 2": []string{"value 1", "value 2", "value 3"}, "key 3": []string{"value 1", "value 2", "value 3"}}
var stringMapStringSliceSingle = map[string][]string{"key 1": []string{"value 1"}, "key 2": []string{"value 2"}, "key 3": []string{"value 3"}}
assert.Equal(t, ToStringMap(taxonomies), map[string]interface{}{"tag": "tags", "group": "groups"})
assert.Equal(t, ToStringMapBool(stringMapBool), map[string]bool{"v1": true, "v2": false})
// ToStringMapString tests
assert.Equal(t, ToStringMapString(stringMapString), stringMapString)
assert.Equal(t, ToStringMapString(stringMapInterface), stringMapString)
assert.Equal(t, ToStringMapString(interfaceMapString), stringMapString)
assert.Equal(t, ToStringMapString(interfaceMapInterface), stringMapString)
// ToStringMapStringSlice tests
assert.Equal(t, ToStringMapStringSlice(stringMapStringSlice), stringMapStringSlice)
assert.Equal(t, ToStringMapStringSlice(stringMapInterfaceSlice), stringMapStringSlice)
assert.Equal(t, ToStringMapStringSlice(stringMapStringSliceMultiple), stringMapStringSlice)
assert.Equal(t, ToStringMapStringSlice(stringMapStringSliceMultiple), stringMapStringSlice)
assert.Equal(t, ToStringMapStringSlice(stringMapString), stringMapStringSliceSingle)
assert.Equal(t, ToStringMapStringSlice(stringMapInterface), stringMapStringSliceSingle)
assert.Equal(t, ToStringMapStringSlice(interfaceMapStringSlice), stringMapStringSlice)
assert.Equal(t, ToStringMapStringSlice(interfaceMapInterfaceSlice), stringMapStringSlice)
assert.Equal(t, ToStringMapStringSlice(interfaceMapString), stringMapStringSingleSliceFieldsResult)
assert.Equal(t, ToStringMapStringSlice(interfaceMapInterface), stringMapStringSingleSliceFieldsResult)
}
func TestSlices(t *testing.T) {

View File

@ -213,18 +213,23 @@ func ToStringMapStringE(i interface{}) (map[string]string, error) {
var m = map[string]string{}
switch v := i.(type) {
case map[interface{}]interface{}:
for k, val := range v {
m[ToString(k)] = ToString(val)
}
return m, nil
case map[string]string:
return v, nil
case map[string]interface{}:
for k, val := range v {
m[ToString(k)] = ToString(val)
}
return m, nil
case map[string]string:
return v, nil
case map[interface{}]string:
for k, val := range v {
m[ToString(k)] = ToString(val)
}
return m, nil
case map[interface{}]interface{}:
for k, val := range v {
m[ToString(k)] = ToString(val)
}
return m, nil
default:
return m, fmt.Errorf("Unable to Cast %#v to map[string]string", i)
}
@ -237,7 +242,28 @@ func ToStringMapStringSliceE(i interface{}) (map[string][]string, error) {
var m = map[string][]string{}
switch v := i.(type) {
case map[interface{}]interface{}:
case map[string][]string:
return v, nil
case map[string][]interface{}:
for k, val := range v {
m[ToString(k)] = ToStringSlice(val)
}
return m, nil
case map[string]string:
for k, val := range v {
m[ToString(k)] = []string{val}
}
case map[string]interface{}:
for k, val := range v {
m[ToString(k)] = []string{ToString(val)}
}
return m, nil
case map[interface{}][]string:
for k, val := range v {
m[ToString(k)] = ToStringSlice(val)
}
return m, nil
case map[interface{}]string:
for k, val := range v {
m[ToString(k)] = ToStringSlice(val)
}
@ -247,28 +273,21 @@ func ToStringMapStringSliceE(i interface{}) (map[string][]string, error) {
m[ToString(k)] = ToStringSlice(val)
}
return m, nil
case map[string][]interface{}:
case map[interface{}]interface{}:
for k, val := range v {
m[ToString(k)] = ToStringSlice(val)
}
return m, nil
case map[string]interface{}:
for k, val := range v {
m[ToString(k)] = []string{ToString(val)}
}
return m, nil
case map[string][]string:
return v, nil
case map[string]string:
for k, val := range v {
m[ToString(k)] = []string{val}
}
default:
fmt.Printf("unexpected type %T", v)
key, err := ToStringE(k)
if err != nil {
return m, fmt.Errorf("Unable to Cast %#v to map[string][]string", i)
}
value, err := ToStringSliceE(val)
if err != nil {
return m, fmt.Errorf("Unable to Cast %#v to map[string][]string", i)
}
m[key] = value
}
}
return m, nil
}
func ToStringMapBoolE(i interface{}) (map[string]bool, error) {
jww.DEBUG.Println("ToStringMapBoolE called on type:", reflect.TypeOf(i))
@ -353,8 +372,12 @@ func ToStringSliceE(i interface{}) ([]string, error) {
case string:
return strings.Fields(v), nil
default:
str, err := ToStringE(v)
if err != nil {
return a, fmt.Errorf("Unable to Cast %#v to []string", i)
}
return []string{str}, nil
}
return a, fmt.Errorf("Unable to Cast %#v to []string", i)
}