owl-blogs/test/assertions/asserts.go

103 lines
2.2 KiB
Go
Raw Permalink Normal View History

package assertions
import (
"net/http/httptest"
"strings"
"testing"
)
2022-11-02 21:12:58 +00:00
func Assert(t *testing.T, condition bool, message string) {
t.Helper()
if !condition {
t.Errorf(message)
}
}
func AssertNot(t *testing.T, condition bool, message string) {
t.Helper()
if condition {
t.Errorf(message)
}
}
func AssertContains(t *testing.T, containing string, search string) {
t.Helper()
if !strings.Contains(containing, search) {
t.Errorf("Expected '%s' to contain '%s'", containing, search)
}
}
2022-11-06 13:36:37 +00:00
func AssertArrayContains[T comparable](t *testing.T, list []T, search T) {
t.Helper()
for _, item := range list {
if item == search {
return
}
}
t.Errorf("Expected '%v' to be in '%v'", search, list)
}
2022-11-02 21:12:58 +00:00
func AssertNotContains(t *testing.T, containing string, search string) {
t.Helper()
if strings.Contains(containing, search) {
t.Errorf("Expected '%s' to not contain '%s'", containing, search)
}
}
func AssertNoError(t *testing.T, err error, message string) {
t.Helper()
if err != nil {
t.Errorf(message+": %s", err.Error())
}
}
2022-11-03 18:23:13 +00:00
func AssertError(t *testing.T, err error, message string) {
t.Helper()
if err == nil {
t.Errorf(message)
}
}
func AssertLen[T any](t *testing.T, list []T, expected int) {
t.Helper()
if len(list) != expected {
t.Errorf("Expected list to have length %d, got %d", expected, len(list))
}
}
func AssertMapLen[T any, S comparable](t *testing.T, list map[S]T, expected int) {
t.Helper()
if len(list) != expected {
t.Errorf("Expected list to have length %d, got %d", expected, len(list))
}
}
func AssertEqual[T comparable](t *testing.T, actual T, expected T) {
t.Helper()
if actual != expected {
t.Errorf("Expected '%v', got '%v'", expected, actual)
}
}
func AssertStatus(t *testing.T, rr *httptest.ResponseRecorder, expStatus int) {
if status := rr.Code; status != expStatus {
t.Errorf("handler returned wrong status code: got %v want %v",
status, expStatus)
return
}
}
2022-11-03 18:23:13 +00:00
func AssertLessThan(t *testing.T, actual int, expected int) {
t.Helper()
if actual >= expected {
t.Errorf("Expected '%d' to be less than '%d'", actual, expected)
}
}
func AssertGreaterThan(t *testing.T, actual int, expected int) {
t.Helper()
if actual <= expected {
t.Errorf("Expected '%d' to be greater than '%d'", actual, expected)
}
}