MM-9977: test config.SetDefaults leaves nothing nil (#8610)
* MM-9977: test config.SetDefaults leaves nothing nil * clarify test default config test cases * comment re: allowing nil slice * extend config SetDefaults to handle partially initialized configs
Этот коммит содержится в:
коммит произвёл
Christopher Speller
родитель
911b409936
Коммит
ae5e324be8
@@ -4,11 +4,57 @@
|
||||
package model
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"reflect"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestConfigDefaults(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
t.Run("somewhere nil when uninitialized", func(t *testing.T) {
|
||||
c := Config{}
|
||||
require.False(t, checkNowhereNil(t, "config", c))
|
||||
})
|
||||
|
||||
t.Run("nowhere nil when initialized", func(t *testing.T) {
|
||||
c := Config{}
|
||||
c.SetDefaults()
|
||||
require.True(t, checkNowhereNil(t, "config", c))
|
||||
})
|
||||
|
||||
t.Run("nowhere nil when partially initialized", func(t *testing.T) {
|
||||
var recursivelyUninitialize func(*Config, string, reflect.Value)
|
||||
recursivelyUninitialize = func(config *Config, name string, v reflect.Value) {
|
||||
if v.Type().Kind() == reflect.Ptr {
|
||||
// Set every pointer we find in the tree to nil
|
||||
v.Set(reflect.Zero(v.Type()))
|
||||
require.True(t, v.IsNil())
|
||||
|
||||
// SetDefaults on the root config should make it non-nil, otherwise
|
||||
// it means that SetDefaults isn't being called recursively in
|
||||
// all cases.
|
||||
config.SetDefaults()
|
||||
if assert.False(t, v.IsNil(), "%s should be non-nil after SetDefaults()", name) {
|
||||
recursivelyUninitialize(config, fmt.Sprintf("(*%s)", name), v.Elem())
|
||||
}
|
||||
|
||||
} else if v.Type().Kind() == reflect.Struct {
|
||||
for i := 0; i < v.NumField(); i++ {
|
||||
recursivelyUninitialize(config, fmt.Sprintf("%s.%s", name, v.Type().Field(i).Name), v.Field(i))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
c := Config{}
|
||||
c.SetDefaults()
|
||||
recursivelyUninitialize(&c, "config", reflect.ValueOf(&c).Elem())
|
||||
})
|
||||
}
|
||||
|
||||
func TestConfigDefaultFileSettingsDirectory(t *testing.T) {
|
||||
c1 := Config{}
|
||||
c1.SetDefaults()
|
||||
|
||||
Ссылка в новой задаче
Block a user