fix tst showing wrong file:line, add DeepEqual

This commit is contained in:
Robin Willmann 2023-09-21 13:07:55 +02:00
parent d56a0235af
commit 6c81f7f6bc

View File

@ -2,23 +2,41 @@ package tst
import ( import (
"encoding/hex" "encoding/hex"
"reflect"
"runtime/debug" "runtime/debug"
"testing" "testing"
) )
func AssertEqual[T comparable](t *testing.T, actual T, expected T) { func AssertEqual[T comparable](t *testing.T, actual T, expected T) {
t.Helper()
if actual != expected { if actual != expected {
t.Errorf("values differ: Actual: '%v', Expected: '%v'", actual, expected) t.Errorf("values differ: Actual: '%v', Expected: '%v'", actual, expected)
} }
} }
func AssertNotEqual[T comparable](t *testing.T, actual T, expected T) { func AssertNotEqual[T comparable](t *testing.T, actual T, expected T) {
t.Helper()
if actual == expected { if actual == expected {
t.Errorf("values do not differ: Actual: '%v', Expected: '%v'", actual, expected) t.Errorf("values do not differ: Actual: '%v', Expected: '%v'", actual, expected)
} }
} }
func AssertDeepEqual[T comparable](t *testing.T, actual T, expected T) {
t.Helper()
if reflect.DeepEqual(actual, expected) {
t.Errorf("values differ: Actual: '%v', Expected: '%v'", actual, expected)
}
}
func AssertNotDeepEqual[T comparable](t *testing.T, actual T, expected T) {
t.Helper()
if !reflect.DeepEqual(actual, expected) {
t.Errorf("values do not differ: Actual: '%v', Expected: '%v'", actual, expected)
}
}
func AssertDeRefEqual[T comparable](t *testing.T, actual *T, expected T) { func AssertDeRefEqual[T comparable](t *testing.T, actual *T, expected T) {
t.Helper()
if actual == nil { if actual == nil {
t.Errorf("values differ: Actual: NIL, Expected: '%v'", expected) t.Errorf("values differ: Actual: NIL, Expected: '%v'", expected)
} }
@ -28,6 +46,7 @@ func AssertDeRefEqual[T comparable](t *testing.T, actual *T, expected T) {
} }
func AssertPtrEqual[T comparable](t *testing.T, actual *T, expected *T) { func AssertPtrEqual[T comparable](t *testing.T, actual *T, expected *T) {
t.Helper()
if actual == nil && expected == nil { if actual == nil && expected == nil {
return return
} }
@ -47,6 +66,7 @@ func AssertPtrEqual[T comparable](t *testing.T, actual *T, expected *T) {
} }
func AssertHexEqual(t *testing.T, expected string, actual []byte) { func AssertHexEqual(t *testing.T, expected string, actual []byte) {
t.Helper()
actualStr := hex.EncodeToString(actual) actualStr := hex.EncodeToString(actual)
if actualStr != expected { if actualStr != expected {
t.Errorf("values differ: Actual: '%v', Expected: '%v'", actualStr, expected) t.Errorf("values differ: Actual: '%v', Expected: '%v'", actualStr, expected)
@ -54,18 +74,21 @@ func AssertHexEqual(t *testing.T, expected string, actual []byte) {
} }
func AssertTrue(t *testing.T, value bool) { func AssertTrue(t *testing.T, value bool) {
t.Helper()
if !value { if !value {
t.Error("value should be true\n" + string(debug.Stack())) t.Error("value should be true\n" + string(debug.Stack()))
} }
} }
func AssertFalse(t *testing.T, value bool) { func AssertFalse(t *testing.T, value bool) {
t.Helper()
if value { if value {
t.Error("value should be false\n" + string(debug.Stack())) t.Error("value should be false\n" + string(debug.Stack()))
} }
} }
func AssertNoErr(t *testing.T, anerr error) { func AssertNoErr(t *testing.T, anerr error) {
t.Helper()
if anerr != nil { if anerr != nil {
t.Error("Function returned an error: " + anerr.Error() + "\n" + string(debug.Stack())) t.Error("Function returned an error: " + anerr.Error() + "\n" + string(debug.Stack()))
} }