Skip to content

Commit

Permalink
Add unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
bwhaley committed Oct 30, 2020
1 parent 5e88f02 commit 8b94b42
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 4 deletions.
6 changes: 2 additions & 4 deletions variables/yaml_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -282,9 +282,7 @@ func ParseYamlString(str string) (interface{}, error) {
return nil, errors.WithStackTrace(err)
}

parsedValue = Convert(parsedValue)

return parsedValue, nil
return Convert(parsedValue), nil
}

// Parse a list of YAML files that define variables into a map from variable name to variable value. Along the way,
Expand Down Expand Up @@ -344,7 +342,7 @@ func ParseVars(varsList []string, varFileList []string) (map[string]interface{},
return util.MergeMaps(varsFromVarsList, varsFromVarFiles), nil
}

// Convert converts i of type map[interface{}]interface{} to a map[string]interface{} so that it may be properly
// Convert updates i of type map[interface{}]interface{} to a map[string]interface{} so that it may be properly
// marshalled in to JSON.
// See: https://github.com/go-yaml/yaml/issues/139
func Convert(i interface{}) interface{} {
Expand Down
41 changes: 41 additions & 0 deletions variables/yaml_helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,3 +76,44 @@ func TestParseVariablesFromKeyValuePairs(t *testing.T) {
}
}
}

func TestConvert(t *testing.T) {
t.Parallel()

testCases := []struct {
input interface{}
expectedType interface{}
}{
{
input: "",
expectedType: "string",
},
{
input: map[interface{}]interface{}{
"key1": "value1",
"key2": "value2",
},
expectedType: map[string]interface{}{},
},
{
input: map[string]interface{}{
"key1": "value1",
},
expectedType: map[string]interface{}{},
},
{
input: []interface{}{
map[interface{}]interface{}{
"key1": "value1",
},
"",
},
expectedType: []interface{}{},
},
}

for _, testCase := range testCases {
actual := Convert(testCase.input)
assert.IsType(t, testCase.expectedType, actual)
}
}

0 comments on commit 8b94b42

Please sign in to comment.