96 lines
2.3 KiB
Go
96 lines
2.3 KiB
Go
package tst
|
|
|
|
import (
|
|
"encoding/hex"
|
|
"reflect"
|
|
"runtime/debug"
|
|
"testing"
|
|
)
|
|
|
|
func AssertEqual[T comparable](t *testing.T, actual T, expected T) {
|
|
t.Helper()
|
|
if actual != expected {
|
|
t.Errorf("values differ: Actual: '%v', Expected: '%v'", actual, expected)
|
|
}
|
|
}
|
|
|
|
func AssertNotEqual[T comparable](t *testing.T, actual T, expected T) {
|
|
t.Helper()
|
|
if actual == expected {
|
|
t.Errorf("values do not differ: Actual: '%v', Expected: '%v'", actual, expected)
|
|
}
|
|
}
|
|
|
|
func AssertDeepEqual[T any](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 any](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) {
|
|
t.Helper()
|
|
if actual == nil {
|
|
t.Errorf("values differ: Actual: NIL, Expected: '%v'", expected)
|
|
}
|
|
if *actual != expected {
|
|
t.Errorf("values differ: Actual: '%v', Expected: '%v'", actual, expected)
|
|
}
|
|
}
|
|
|
|
func AssertPtrEqual[T comparable](t *testing.T, actual *T, expected *T) {
|
|
t.Helper()
|
|
if actual == nil && expected == nil {
|
|
return
|
|
}
|
|
if actual != nil && expected != nil {
|
|
if *actual != *expected {
|
|
t.Errorf("values differ: Actual: '%v', Expected: '%v'", *actual, *expected)
|
|
} else {
|
|
return
|
|
}
|
|
}
|
|
if actual == nil && expected != nil {
|
|
t.Errorf("values differ: Actual: nil, Expected: not-nil")
|
|
}
|
|
if actual != nil && expected == nil {
|
|
t.Errorf("values differ: Actual: not-nil, Expected: nil")
|
|
}
|
|
}
|
|
|
|
func AssertHexEqual(t *testing.T, expected string, actual []byte) {
|
|
t.Helper()
|
|
actualStr := hex.EncodeToString(actual)
|
|
if actualStr != expected {
|
|
t.Errorf("values differ: Actual: '%v', Expected: '%v'", actualStr, expected)
|
|
}
|
|
}
|
|
|
|
func AssertTrue(t *testing.T, value bool) {
|
|
t.Helper()
|
|
if !value {
|
|
t.Error("value should be true\n" + string(debug.Stack()))
|
|
}
|
|
}
|
|
|
|
func AssertFalse(t *testing.T, value bool) {
|
|
t.Helper()
|
|
if value {
|
|
t.Error("value should be false\n" + string(debug.Stack()))
|
|
}
|
|
}
|
|
|
|
func AssertNoErr(t *testing.T, anerr error) {
|
|
t.Helper()
|
|
if anerr != nil {
|
|
t.Error("Function returned an error: " + anerr.Error() + "\n" + string(debug.Stack()))
|
|
}
|
|
}
|