SimpleCloudNotifier/server/test/util/common.go

164 lines
3.8 KiB
Go
Raw Normal View History

package util
import (
"fmt"
2022-11-30 20:39:04 +01:00
"reflect"
"runtime/debug"
"strings"
"testing"
)
func AssertJsonMapEqual(t *testing.T, key string, expected map[string]any, actual map[string]any) {
mkeys := make(map[string]string)
for k := range expected {
mkeys[k] = k
}
for k := range actual {
mkeys[k] = k
}
for mapkey := range mkeys {
if _, ok := expected[mapkey]; !ok {
TestFailFmt(t, "Missing Key expected['%s'] ( assertJsonMapEqual[%s] )", mapkey, key)
}
if _, ok := actual[mapkey]; !ok {
TestFailFmt(t, "Missing Key actual['%s'] ( assertJsonMapEqual[%s] )", mapkey, key)
}
AssertEqual(t, key+"."+mapkey, expected[mapkey], actual[mapkey])
}
}
func AssertEqual(t *testing.T, key string, expected any, actual any) {
if expected != actual {
t.Errorf("Value [%s] differs (%T <-> %T):\n", key, expected, actual)
2022-11-30 20:39:04 +01:00
strExp := fmt.Sprintf("%v", expected)
strAct := fmt.Sprintf("%v", actual)
2022-11-30 20:39:04 +01:00
if strings.Contains(strAct, "\n") {
t.Errorf("Actual:\n~~~~~~~~~~~~~~~~\n%v\n~~~~~~~~~~~~~~~~\n\n", actual)
} else {
2022-11-30 20:39:04 +01:00
t.Errorf("Actual := \"%v\"\n", actual)
}
2022-11-30 20:39:04 +01:00
if strings.Contains(strExp, "\n") {
t.Errorf("Expected:\n~~~~~~~~~~~~~~~~\n%v\n~~~~~~~~~~~~~~~~\n\n", expected)
} else {
2022-11-30 20:39:04 +01:00
t.Errorf("Expected := \"%v\"\n", expected)
}
t.Error(string(debug.Stack()))
2022-11-30 17:58:04 +01:00
t.FailNow()
}
}
2022-11-30 21:17:29 +01:00
func AssertNotEqual(t *testing.T, key string, expected any, actual any) {
if expected == actual {
t.Errorf("Value [%s] does not differ (%T <-> %T):\n", key, expected, actual)
str1 := fmt.Sprintf("%v", expected)
str2 := fmt.Sprintf("%v", actual)
if strings.Contains(str1, "\n") {
t.Errorf("Actual:\n~~~~~~~~~~~~~~~~\n%v\n~~~~~~~~~~~~~~~~\n\n", expected)
} else {
t.Errorf("Actual := \"%v\"\n", expected)
}
if strings.Contains(str2, "\n") {
t.Errorf("Not Expected:\n~~~~~~~~~~~~~~~~\n%v\n~~~~~~~~~~~~~~~~\n\n", actual)
} else {
t.Errorf("Not Expected := \"%v\"\n", actual)
}
t.Error(string(debug.Stack()))
t.FailNow()
}
}
2022-11-30 17:58:04 +01:00
func AssertStrRepEqual(t *testing.T, key string, expected any, actual any) {
2022-11-30 20:39:04 +01:00
strExp := fmt.Sprintf("%v", unpointer(expected))
strAct := fmt.Sprintf("%v", unpointer(actual))
2022-11-30 17:58:04 +01:00
2022-11-30 20:39:04 +01:00
if strAct != strExp {
2022-11-30 17:58:04 +01:00
t.Errorf("Value [%s] differs (%T <-> %T):\n", key, expected, actual)
2022-11-30 20:39:04 +01:00
if strings.Contains(strAct, "\n") {
t.Errorf("Actual:\n~~~~~~~~~~~~~~~~\n%v\n~~~~~~~~~~~~~~~~\n\n", strAct)
2022-11-30 17:58:04 +01:00
} else {
2022-11-30 20:39:04 +01:00
t.Errorf("Actual := \"%v\"\n", strAct)
2022-11-30 17:58:04 +01:00
}
2022-11-30 20:39:04 +01:00
if strings.Contains(strExp, "\n") {
t.Errorf("Expected:\n~~~~~~~~~~~~~~~~\n%v\n~~~~~~~~~~~~~~~~\n\n", strExp)
2022-11-30 17:58:04 +01:00
} else {
2022-11-30 20:39:04 +01:00
t.Errorf("Expected := \"%v\"\n", strExp)
2022-11-30 17:58:04 +01:00
}
t.Error(string(debug.Stack()))
t.FailNow()
}
}
2022-11-30 21:17:29 +01:00
func AssertNotStrRepEqual(t *testing.T, key string, expected any, actual any) {
strExp := fmt.Sprintf("%v", unpointer(expected))
strAct := fmt.Sprintf("%v", unpointer(actual))
2022-11-30 21:17:29 +01:00
if strAct == strExp {
t.Errorf("Value [%s] does not differ (%T <-> %T):\n", key, expected, actual)
2022-11-30 21:17:29 +01:00
if strings.Contains(strAct, "\n") {
t.Errorf("Actual:\n~~~~~~~~~~~~~~~~\n%v\n~~~~~~~~~~~~~~~~\n\n", strAct)
} else {
2022-11-30 21:17:29 +01:00
t.Errorf("Actual := \"%v\"\n", strAct)
}
2022-11-30 21:17:29 +01:00
if strings.Contains(strExp, "\n") {
t.Errorf("Expected:\n~~~~~~~~~~~~~~~~\n%v\n~~~~~~~~~~~~~~~~\n\n", strExp)
} else {
2022-11-30 21:17:29 +01:00
t.Errorf("Expected := \"%v\"\n", strExp)
}
t.Error(string(debug.Stack()))
t.FailNow()
}
}
func TestFail(t *testing.T, msg string) {
t.Error(msg)
t.FailNow()
}
func TestFailFmt(t *testing.T, format string, args ...any) {
t.Errorf(format, args...)
t.FailNow()
}
func TestFailErr(t *testing.T, e error) {
t.Error(fmt.Sprintf("Failed with error:\n%s\n\nError:\n%+v\n\nTrace:\n%s", e.Error(), e, string(debug.Stack())))
t.FailNow()
}
2022-11-30 20:39:04 +01:00
func unpointer(v any) any {
2022-11-30 21:39:14 +01:00
if v == nil {
return v
}
2022-11-30 20:39:04 +01:00
val := reflect.ValueOf(v)
if val.Kind() == reflect.Ptr {
2022-11-30 21:39:14 +01:00
if val.IsNil() {
return v
}
2022-11-30 20:39:04 +01:00
val = val.Elem()
return unpointer(val.Interface())
}
return v
}