Commit 8395d0e9 authored by Mitchell Hashimoto's avatar Mitchell Hashimoto

common: tests for ChooseSTring

parent 7191c1f2
......@@ -45,6 +45,17 @@ func CheckUnusedConfig(md *mapstructure.Metadata) *packer.MultiError {
return nil
}
// ChooseString returns the first non-empty value.
func ChooseString(vals ...string) string {
for _, el := range vals {
if el != "" {
return el
}
}
return ""
}
// DecodeConfig is a helper that handles decoding raw configuration using
// mapstructure. It returns the metadata and any errors that may happen.
// If you need extra configuration for mapstructure, you should configure
......@@ -206,14 +217,3 @@ func decodeConfigHook(raws []interface{}) (mapstructure.DecodeHookFunc, error) {
return v, nil
}, nil
}
// ChooseString returns the first non-empty value.
func ChooseString(vals ...string) string {
for _, el := range vals {
if el != "" {
return el
}
}
return ""
}
......@@ -29,6 +29,33 @@ func TestCheckUnusedConfig(t *testing.T) {
}
}
func TestChooseString(t *testing.T) {
cases := []struct {
Input []string
Output string
}{
{
[]string{"", "foo", ""},
"foo",
},
{
[]string{"", "foo", "bar"},
"foo",
},
{
[]string{"", "", ""},
"",
},
}
for _, tc := range cases {
result := ChooseString(tc.Input...)
if result != tc.Output {
t.Fatalf("bad: %#v", tc.Input)
}
}
}
func TestDecodeConfig(t *testing.T) {
type Local struct {
Foo string
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment