From c1e9d5db75e9765d6d8e2599c47a7f2ba796a9de Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?G=C3=B6ksel=20K=C3=BC=C3=A7=C3=BCk=C5=9Fahin?= Date: Fri, 3 Jan 2025 22:19:29 +0300 Subject: [PATCH] test: improve assert.go with better formating --- test/assert/assert.go | 28 +++++++++++++++------------- 1 file changed, 15 insertions(+), 13 deletions(-) diff --git a/test/assert/assert.go b/test/assert/assert.go index bd476fa..674bd24 100644 --- a/test/assert/assert.go +++ b/test/assert/assert.go @@ -2,7 +2,9 @@ package assert import ( "encoding/json" + "fmt" "reflect" + "strings" ) type TestingT interface { @@ -18,23 +20,23 @@ func MarshalWithoutError(t TestingT, body any) string { } func Equal(t TestingT, expected, actual any, messages ...string) { - message := getMessage(messages) + message := getMessages(messages) if !reflect.DeepEqual(expected, actual) { - t.Errorf("Expected: '%v' type: <%s>, Actual: '%v' type: <%s>. %s", + t.Errorf("\nExpected: '%v' type: <%s>\nActual: '%v' type: <%s>. %s", expected, reflect.TypeOf(expected).String(), actual, reflect.TypeOf(actual).String(), message) } } func EqualReference(t TestingT, expected, actual any, messages ...string) { - message := getMessage(messages) + message := getMessages(messages) expectedPointer, actualPointer := reflect.ValueOf(expected).Pointer(), reflect.ValueOf(actual).Pointer() if !(expectedPointer == actualPointer) { - t.Errorf("Expected: '%v', Actual: '%v'. %s", expectedPointer, actualPointer, message) + t.Errorf("\nExpected: '%v', Actual: '%v'. %s", expectedPointer, actualPointer, message) } } func NotEqualReference(t TestingT, expected, actual any, messages ...string) { - message := getMessage(messages) + message := getMessages(messages) expectedPointer, actualPointer := reflect.ValueOf(expected).Pointer(), reflect.ValueOf(actual).Pointer() if expectedPointer == actualPointer { t.Errorf("Expected and Actual have the same reference: '%v'. %s", expectedPointer, message) @@ -42,35 +44,35 @@ func NotEqualReference(t TestingT, expected, actual any, messages ...string) { } func True(t TestingT, condition bool, messages ...string) { - message := getMessage(messages) + message := getMessages(messages) if !condition { t.Errorf("Expected: true, Actual: false. %s", message) } } func False(t TestingT, condition bool, messages ...string) { - message := getMessage(messages) + message := getMessages(messages) if condition { t.Errorf("Expected: false, Actual: true. %s", message) } } func Nil(t TestingT, value any, messages ...string) { - message := getMessage(messages) + message := getMessages(messages) if value != nil { t.Errorf("Expected: nil, Actual: '%v'. %s", value, message) } } func NotNil(t TestingT, value any, messages ...string) { - message := getMessage(messages) + message := getMessages(messages) if value == nil { t.Errorf("Expected: not nil, Actual: nil. %s", message) } } func IsType(t TestingT, expected, actual any, messages ...string) { - message := getMessage(messages) + message := getMessages(messages) expectedType := reflect.TypeOf(expected) actualValue := reflect.TypeOf(actual) if expectedType != actualValue { @@ -79,16 +81,16 @@ func IsType(t TestingT, expected, actual any, messages ...string) { } func IsTypeString(t TestingT, expectedType string, actual any, messages ...string) { - message := getMessage(messages) + message := getMessages(messages) actualValue := reflect.TypeOf(actual) if expectedType != actualValue.String() { t.Errorf("Expected type '%v', but got type '%v'. %s", expectedType, actualValue, message) } } -func getMessage(messages []string) string { +func getMessages(messages []string) string { if len(messages) > 0 { - return messages[0] + return fmt.Sprintf("messages: %s", strings.Join(messages, " ")) } return "" }