summaryrefslogtreecommitdiffstats
path: root/model/utils_test.go
diff options
context:
space:
mode:
authorGeorge Goldberg <george@gberg.me>2018-08-29 19:08:03 +0100
committerChristopher Speller <crspeller@gmail.com>2018-08-29 11:08:03 -0700
commite742ba7d517c38534fbd4bb964accfb8f18c7841 (patch)
treef148186b98ae9075417c0ca780ae84655ff06df9 /model/utils_test.go
parent9c76d9ba0031ee4175db6960024d61c23cc98659 (diff)
downloadchat-e742ba7d517c38534fbd4bb964accfb8f18c7841.tar.gz
chat-e742ba7d517c38534fbd4bb964accfb8f18c7841.tar.bz2
chat-e742ba7d517c38534fbd4bb964accfb8f18c7841.zip
Remove testing imports from non-test code. (#9324)
Diffstat (limited to 'model/utils_test.go')
-rw-r--r--model/utils_test.go58
1 files changed, 58 insertions, 0 deletions
diff --git a/model/utils_test.go b/model/utils_test.go
index f004fc216..1db91d6e6 100644
--- a/model/utils_test.go
+++ b/model/utils_test.go
@@ -6,6 +6,7 @@ package model
import (
"fmt"
"net/http"
+ "reflect"
"strings"
"testing"
"time"
@@ -665,3 +666,60 @@ func TestNowhereNil(t *testing.T) {
})
}
}
+
+// checkNowhereNil checks that the given interface value is not nil, and if a struct, that all of
+// its public fields are also nowhere nil
+func checkNowhereNil(t *testing.T, name string, value interface{}) bool {
+ if value == nil {
+ return false
+ }
+
+ v := reflect.ValueOf(value)
+ switch v.Type().Kind() {
+ case reflect.Ptr:
+ if v.IsNil() {
+ t.Logf("%s was nil", name)
+ return false
+ }
+
+ return checkNowhereNil(t, fmt.Sprintf("(*%s)", name), v.Elem().Interface())
+
+ case reflect.Map:
+ if v.IsNil() {
+ t.Logf("%s was nil", name)
+ return false
+ }
+
+ // Don't check map values
+ return true
+
+ case reflect.Struct:
+ nowhereNil := true
+ for i := 0; i < v.NumField(); i++ {
+ f := v.Field(i)
+ // Ignore unexported fields
+ if v.Type().Field(i).PkgPath != "" {
+ continue
+ }
+
+ nowhereNil = nowhereNil && checkNowhereNil(t, fmt.Sprintf("%s.%s", name, v.Type().Field(i).Name), f.Interface())
+ }
+
+ return nowhereNil
+
+ case reflect.Array:
+ fallthrough
+ case reflect.Chan:
+ fallthrough
+ case reflect.Func:
+ fallthrough
+ case reflect.Interface:
+ fallthrough
+ case reflect.UnsafePointer:
+ t.Logf("unhandled field %s, type: %s", name, v.Type().Kind())
+ return false
+
+ default:
+ return true
+ }
+}