common: tests for ChooseSTring
This commit is contained in:
parent
7191c1f250
commit
8395d0e97a
|
@ -45,6 +45,17 @@ func CheckUnusedConfig(md *mapstructure.Metadata) *packer.MultiError {
|
||||||
return nil
|
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
|
// DecodeConfig is a helper that handles decoding raw configuration using
|
||||||
// mapstructure. It returns the metadata and any errors that may happen.
|
// mapstructure. It returns the metadata and any errors that may happen.
|
||||||
// If you need extra configuration for mapstructure, you should configure
|
// If you need extra configuration for mapstructure, you should configure
|
||||||
|
@ -206,14 +217,3 @@ func decodeConfigHook(raws []interface{}) (mapstructure.DecodeHookFunc, error) {
|
||||||
return v, nil
|
return v, nil
|
||||||
}, 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) {
|
func TestDecodeConfig(t *testing.T) {
|
||||||
type Local struct {
|
type Local struct {
|
||||||
Foo string
|
Foo string
|
||||||
|
|
Loading…
Reference in New Issue