diff options
| author | Eugene Triguba <eugenetriguba@gmail.com> | 2024-02-11 11:13:25 -0500 |
|---|---|---|
| committer | Eugene Triguba <eugenetriguba@gmail.com> | 2024-02-11 11:13:25 -0500 |
| commit | 3e98972271b4880bf40bf9c98564fce5bf1879d0 (patch) | |
| tree | 4ca4e97accdba3a6a1e3bab7e9ce846745961ff8 | |
| parent | Release v0.3.0 (diff) | |
| download | checkmate-3e98972271b4880bf40bf9c98564fce5bf1879d0.zip | |
Add nil pointer check for Nil functions
| -rw-r--r-- | CHANGELOG.md | 6 | ||||
| -rw-r--r-- | check/check.go | 13 | ||||
| -rw-r--r-- | check/check_test.go | 11 |
3 files changed, 29 insertions, 1 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md index 128a3c4..bc95198 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## [Unreleased] + +### Fixed + +- `check.Nil` and `assert.Nil` now handle nil pointers. + ## [0.3.0] - 2024-02-01 ### Added diff --git a/check/check.go b/check/check.go index ddb9d5a..ffc4bb5 100644 --- a/check/check.go +++ b/check/check.go @@ -3,6 +3,7 @@ package check import ( "errors" "fmt" + "reflect" "strings" "github.com/eugenetriguba/checkmate" @@ -23,7 +24,17 @@ func Nil(t checkmate.TestingT, value any, msgAndArgs ...any) bool { msgAndArgs = []any{"expected value to be nil, got %v", value} } - return check(t, value == nil, msgAndArgs...) + isNil := false + val := reflect.ValueOf(value) + if val.Kind() == reflect.Ptr { + if val.IsNil() { + isNil = true + } + } else if value == nil { + isNil = true + } + + return check(t, isNil, msgAndArgs...) } // NotNil checks whether the value does not equal nil. diff --git a/check/check_test.go b/check/check_test.go index 9c73d52..8c6087e 100644 --- a/check/check_test.go +++ b/check/check_test.go @@ -343,3 +343,14 @@ func TestCheckReturnValues(t *testing.T) { } }) } + +func TestCheckNilForNilPointer(t *testing.T) { + mockT := &cmtest.MockT{} + var value *cmtest.MockT = nil + + passed := Nil(mockT, value) + + if !passed { + t.Fatal("check.Nil should have returned true for nil pointer") + } +} |