From a9841ab27c56b981676f1fb3b8a29591c0068008 Mon Sep 17 00:00:00 2001 From: Eugene Triguba Date: Sun, 28 Jan 2024 15:49:20 -0500 Subject: Add AssertTrue, AssertFalse, AssertNil, and AssertNotNil --- assert.go | 60 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) (limited to 'assert.go') diff --git a/assert.go b/assert.go index da05ffb..eb2544a 100644 --- a/assert.go +++ b/assert.go @@ -16,6 +16,66 @@ import ( "github.com/google/go-cmp/cmp" ) +// AssertNil checks whether the value equals nil. An optional +// message and arguments for any format placeholders in that +// message can be provided for if a failure occurs. +func AssertNil(t TestingT, value any, msgAndArgs ...any) { + if ht, ok := t.(helperT); ok { + ht.Helper() + } + + if len(msgAndArgs) == 0 { + msgAndArgs = []any{"expected value to be nil, got %v", value} + } + + Assert(t, value == nil, msgAndArgs...) +} + +// AssertNil checks whether the value does not equal nil. +// An optional message and arguments for any format placeholders +// in that message can be provided for if a failure occurs. +func AssertNotNil(t TestingT, value any, msgAndArgs ...any) { + if ht, ok := t.(helperT); ok { + ht.Helper() + } + + if len(msgAndArgs) == 0 { + msgAndArgs = []any{"expected value to not be nil, got nil", value} + } + + Assert(t, value != nil, msgAndArgs...) +} + +// AssertTrue checks whether the condition is true. +// An optional message and arguments for any format placeholders +// in that message can be provided for if a failure occurs. +func AssertTrue(t TestingT, condition bool, msgAndArgs ...any) { + if ht, ok := t.(helperT); ok { + ht.Helper() + } + + if len(msgAndArgs) == 0 { + msgAndArgs = []any{"expected condition to be true, got false"} + } + + Assert(t, condition, msgAndArgs...) +} + +// AssertFalse checks whether the condition is false. +// An optional message and arguments for any format placeholders +// in that message can be provided for if a failure occurs. +func AssertFalse(t TestingT, condition bool, msgAndArgs ...any) { + if ht, ok := t.(helperT); ok { + ht.Helper() + } + + if len(msgAndArgs) == 0 { + msgAndArgs = []any{"expected condition to be false, got true"} + } + + Assert(t, !condition, msgAndArgs...) +} + // AssertErrorIs checks whether any error in the actual error's tree // matches the expected error. An optional message and arguments for // any format placeholders in that message can be provided for if a -- cgit v1.2.3