From 9b6ca94623a34c6fb8414bf4048b02594d3a9e18 Mon Sep 17 00:00:00 2001 From: jackspirou Date: Fri, 31 Jul 2015 13:44:36 -0500 Subject: [PATCH] adding unit tests for ToStringMapString and ToStringMapStringSlice methods --- cast_test.go | 35 ++++++++++++++++++++++++ caste.go | 77 ++++++++++++++++++++++++++++++++++------------------ 2 files changed, 85 insertions(+), 27 deletions(-) diff --git a/cast_test.go b/cast_test.go index 3fa717f..bde6dbb 100644 --- a/cast_test.go +++ b/cast_test.go @@ -73,8 +73,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) { diff --git a/caste.go b/caste.go index 36bb3ab..cf54ccc 100644 --- a/caste.go +++ b/caste.go @@ -211,18 +211,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) } @@ -235,7 +240,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) } @@ -245,27 +271,20 @@ 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) + 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 - 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) - return m, fmt.Errorf("Unable to Cast %#v to map[string][]string", i) } - return m, fmt.Errorf("Unable to Cast %#v to map[string][]string", i) + return m, nil } func ToStringMapBoolE(i interface{}) (map[string]bool, error) { @@ -351,7 +370,11 @@ func ToStringSliceE(i interface{}) ([]string, error) { case string: return strings.Fields(v), nil default: - return a, fmt.Errorf("Unable to Cast %#v to []string", i) + 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)