common: tests for ChooseSTring

This commit is contained in:
Mitchell Hashimoto 2014-04-21 21:30:49 -07:00
parent 7191c1f250
commit 8395d0e97a
2 changed files with 38 additions and 11 deletions

View File

@ -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 ""
}

View File

@ -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