summaryrefslogtreecommitdiffstats
path: root/model/utils_test.go
diff options
context:
space:
mode:
authorJesse Hallam <jesse.hallam@gmail.com>2018-04-13 10:56:09 -0400
committerChristopher Speller <crspeller@gmail.com>2018-04-13 07:56:09 -0700
commitae5e324be8078927bf088bc9dae37189a6ecea6f (patch)
treee8f0a0c2a4c6fc2156ed7af58ed91260a1b147d6 /model/utils_test.go
parent911b409936521827152b0a491d3e02ed81202694 (diff)
downloadchat-ae5e324be8078927bf088bc9dae37189a6ecea6f.tar.gz
chat-ae5e324be8078927bf088bc9dae37189a6ecea6f.tar.bz2
chat-ae5e324be8078927bf088bc9dae37189a6ecea6f.zip
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
Diffstat (limited to 'model/utils_test.go')
-rw-r--r--model/utils_test.go183
1 files changed, 183 insertions, 0 deletions
diff --git a/model/utils_test.go b/model/utils_test.go
index 411d7bf50..92354c0a1 100644
--- a/model/utils_test.go
+++ b/model/utils_test.go
@@ -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))
+ })
+ }
+}