Skip to content

Commit

Permalink
Create functions 'NotZeroThenSetZero' and 'NotNilThenSetNil'
Browse files Browse the repository at this point in the history
  • Loading branch information
HaraldNordgren committed Jul 30, 2023
1 parent 862e410 commit bfaa780
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 2 deletions.
1 change: 0 additions & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ jobs:
strategy:
matrix:
go_version:
- "1.17"
- "1.18"
- "1.19"
- "1.20"
Expand Down
33 changes: 33 additions & 0 deletions assert/assertions.go
Original file line number Diff line number Diff line change
Expand Up @@ -620,6 +620,39 @@ func NotNil(t TestingT, object interface{}, msgAndArgs ...interface{}) bool {
return Fail(t, "Expected value not to be nil.", msgAndArgs...)
}

// NotNilThenSetNil asserts that the specified object is not zero and then sets it to zero.
//
// assert.NotZeroThenSetZero(t, &x)
func NotZeroThenSetZero[X any](t TestingT, x *X) bool {
if h, ok := t.(tHelper); ok {
h.Helper()
}

if ok := NotZero(t, *x); !ok {
return false
}

var zeroValue X
*x = zeroValue
return true
}

// NotNilThenSetNil asserts that the specified pointer is not nil and then sets it to nil.
//
// assert.NotNilThenSetNil(t, &x)
func NotNilThenSetNil[X any](t TestingT, x **X) bool {
if h, ok := t.(tHelper); ok {
h.Helper()
}

if ok := NotNil(t, *x); !ok {
return false
}

*x = nil
return true
}

// containsKind checks if a specified kind in the slice of kinds.
func containsKind(kinds []reflect.Kind, kind reflect.Kind) bool {
for i := 0; i < len(kinds); i++ {
Expand Down
55 changes: 55 additions & 0 deletions assert/assertions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -712,6 +712,61 @@ func TestNil(t *testing.T) {

}

func TestNotZeroThenSetZero(t *testing.T) {

mockT := new(testing.T)

cases := []struct {
beforeValue string
result bool
afterValue interface{}
}{
{"", false, ""},
{"hello", true, ""},
}

for _, c := range cases {
t.Run(fmt.Sprintf("NotZeroThenSetZero(%#v)", c.beforeValue), func(t *testing.T) {
valueToBeChanged := c.beforeValue
res := NotZeroThenSetZero(mockT, &valueToBeChanged)
if res != c.result {
t.Errorf("NotZeroThenSetZero(%#v) should return %#v", valueToBeChanged, c.result)
}
if valueToBeChanged != c.afterValue {
t.Errorf("Value should be changed to %#v, instead was %#v", c.afterValue, valueToBeChanged)
}
})
}
}

func TestNotNilThenSetNil(t *testing.T) {

mockT := new(testing.T)

a := "hello"
cases := []struct {
beforeValue *string
result bool
afterValue *string
}{
{nil, false, nil},
{&a, true, nil},
}

for _, c := range cases {
t.Run(fmt.Sprintf("NotNilThenSetNil(%#v)", c.beforeValue), func(t *testing.T) {
valueToBeChanged := c.beforeValue
res := NotNilThenSetNil(mockT, &valueToBeChanged)
if res != c.result {
t.Errorf("NotNilThenSetNil(%#v) should return %#v", valueToBeChanged, c.result)
}
if valueToBeChanged != c.afterValue {
t.Errorf("Value should be changed to %#v, instead was %#v", c.afterValue, valueToBeChanged)
}
})
}
}

func TestTrue(t *testing.T) {

mockT := new(testing.T)
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module github.com/stretchr/testify

// This should match the minimum supported version that is tested in
// .github/workflows/main.yml
go 1.17
go 1.18

require (
github.com/davecgh/go-spew v1.1.1
Expand Down

0 comments on commit bfaa780

Please sign in to comment.