diff options
| author | Eugene Triguba <eugenetriguba@gmail.com> | 2024-01-28 15:49:20 -0500 |
|---|---|---|
| committer | Eugene Triguba <eugenetriguba@gmail.com> | 2024-01-28 15:49:20 -0500 |
| commit | a9841ab27c56b981676f1fb3b8a29591c0068008 (patch) | |
| tree | 85fe823110ca5b0c1a8500541c73c09032b27728 | |
| parent | Add AssertErrorIs and AssertErrorContains (diff) | |
| download | checkmate-a9841ab27c56b981676f1fb3b8a29591c0068008.zip | |
Add AssertTrue, AssertFalse, AssertNil, and AssertNotNil
| -rw-r--r-- | assert.go | 60 | ||||
| -rw-r--r-- | assert_test.go | 44 |
2 files changed, 104 insertions, 0 deletions
@@ -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 diff --git a/assert_test.go b/assert_test.go index 84a17da..88e9e0b 100644 --- a/assert_test.go +++ b/assert_test.go @@ -121,6 +121,42 @@ func wrapAssertErrorContains(t TestingT, args []any) { } } +func wrapAssertTrue(t TestingT, args []any) { + if len(args) > 1 { + AssertTrue(t, args[0].(bool), args[1:]...) + + } else { + AssertTrue(t, args[0].(bool)) + } +} + +func wrapAssertFalse(t TestingT, args []any) { + if len(args) > 1 { + AssertFalse(t, args[0].(bool), args[1:]...) + + } else { + AssertFalse(t, args[0].(bool)) + } +} + +func wrapAssertNil(t TestingT, args []any) { + if len(args) > 1 { + AssertNil(t, args[0], args[1:]...) + + } else { + AssertNil(t, args[0]) + } +} + +func wrapAssertNotNil(t TestingT, args []any) { + if len(args) > 1 { + AssertNotNil(t, args[0], args[1:]...) + + } else { + AssertNotNil(t, args[0]) + } +} + var passingTestFns = []struct { name string assertionFn assertFn @@ -134,6 +170,10 @@ var passingTestFns = []struct { {"AssertLenEqual", wrapAssertLenEqualInt, []any{[]int{1, 2}, 2}}, {"AssertErrorIs", wrapAssertErrorIs, []any{os.ErrExist, os.ErrExist}}, {"AssertErrorContains", wrapAssertErrorContains, []any{errors.New("error 1"), "error 1"}}, + {"AssertTrue", wrapAssertTrue, []any{true}}, + {"AssertFalse", wrapAssertFalse, []any{false}}, + {"AssertNil", wrapAssertNil, []any{nil}}, + {"AssertNotNil", wrapAssertNotNil, []any{1}}, } var failingTestFns = []struct { @@ -149,6 +189,10 @@ var failingTestFns = []struct { {"AssertLenEqual", wrapAssertLenEqualInt, []any{[]int{1, 2}, 0}}, {"AssertErrorIs", wrapAssertErrorIs, []any{errors.New("error 1"), errors.New("error 2")}}, {"AssertErrorContains", wrapAssertErrorContains, []any{errors.New("error 1"), "error 2"}}, + {"AssertTrue", wrapAssertTrue, []any{false}}, + {"AssertFalse", wrapAssertFalse, []any{true}}, + {"AssertNil", wrapAssertNil, []any{1}}, + {"AssertNotNil", wrapAssertNotNil, []any{nil}}, } func TestOptionalMessageAndArgs(t *testing.T) { |