ApplyDefaults() now only applies defaults if the value is not already otherwise set.

This commit is contained in:
Alec Thomas
2019-06-12 13:25:33 +10:00
parent 0d256bb68a
commit 7be398e79f
6 changed files with 115 additions and 18 deletions
+19 -4
View File
@@ -12,8 +12,23 @@ func TestApplyDefaults(t *testing.T) {
Str string `default:"str"`
Duration time.Duration `default:"30s"`
}
cli := &CLI{}
err := ApplyDefaults(cli)
require.NoError(t, err)
require.Equal(t, &CLI{Str: "str", Duration: time.Second * 30}, cli)
tests := []struct {
name string
target CLI
expected CLI
}{
{name: "DefaultsWhenNotSet",
expected: CLI{Str: "str", Duration: time.Second * 30}},
{name: "PartiallySetDefaults",
target: CLI{Duration: time.Second},
expected: CLI{Str: "str", Duration: time.Second}},
}
for _, tt := range tests {
// nolint: scopelint
t.Run(tt.name, func(t *testing.T) {
err := ApplyDefaults(&tt.target)
require.NoError(t, err)
require.Equal(t, tt.expected, tt.target)
})
}
}