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
Этот коммит содержится в:
Jesse Hallam
2018-04-13 10:56:09 -04:00
коммит произвёл Christopher Speller
родитель 911b409936
Коммит ae5e324be8
4 изменённых файлов: 289 добавлений и 1 удалений

Просмотреть файл

@@ -7,6 +7,8 @@ import (
"net/http"
"strings"
"testing"
"github.com/stretchr/testify/require"
)
func TestNewId(t *testing.T) {
@@ -367,3 +369,184 @@ func TestIsValidId(t *testing.T) {
}
}
}
func TestNowhereNil(t *testing.T) {
t.Parallel()
var nilStringPtr *string
var nonNilStringPtr *string = new(string)
var nilSlice []string
var nilStruct *struct{}
var nilMap map[bool]bool
var nowhereNilStruct = struct {
X *string
Y *string
}{
nonNilStringPtr,
nonNilStringPtr,
}
var somewhereNilStruct = struct {
X *string
Y *string
}{
nonNilStringPtr,
nilStringPtr,
}
var privateSomewhereNilStruct = struct {
X *string
y *string
}{
nonNilStringPtr,
nilStringPtr,
}
testCases := []struct {
Description string
Value interface{}
Expected bool
}{
{
"nil",
nil,
false,
},
{
"empty string",
"",
true,
},
{
"non-empty string",
"not empty!",
true,
},
{
"nil string pointer",
nilStringPtr,
false,
},
{
"non-nil string pointer",
nonNilStringPtr,
true,
},
{
"0",
0,
true,
},
{
"1",
1,
true,
},
{
"0 (int64)",
int64(0),
true,
},
{
"1 (int64)",
int64(1),
true,
},
{
"true",
true,
true,
},
{
"false",
false,
true,
},
{
"nil slice",
nilSlice,
// A nil slice is observably the same as an empty slice, so allow it.
true,
},
{
"empty slice",
[]string{},
true,
},
{
"slice containing nils",
[]*string{nil, nil},
true,
},
{
"nil map",
nilMap,
false,
},
{
"non-nil map",
make(map[bool]bool),
true,
},
{
"non-nil map containing nil",
map[bool]*string{true: nilStringPtr, false: nonNilStringPtr},
// Map values are not checked
true,
},
{
"nil struct",
nilStruct,
false,
},
{
"empty struct",
struct{}{},
true,
},
{
"struct containing no nil",
nowhereNilStruct,
true,
},
{
"struct containing nil",
somewhereNilStruct,
false,
},
{
"struct pointer containing no nil",
&nowhereNilStruct,
true,
},
{
"struct pointer containing nil",
&somewhereNilStruct,
false,
},
{
"struct containing private nil",
privateSomewhereNilStruct,
true,
},
{
"struct pointer containing private nil",
&privateSomewhereNilStruct,
true,
},
}
for _, testCase := range testCases {
testCase := testCase
t.Run(testCase.Description, func(t *testing.T) {
defer func() {
if r := recover(); r != nil {
t.Errorf("panic: %v", r)
}
}()
t.Parallel()
require.Equal(t, testCase.Expected, checkNowhereNil(t, "value", testCase.Value))
})
}
}