diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 0ffd24cb3..463eee185 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -24,7 +24,6 @@ jobs: strategy: matrix: go_version: - - "1.17" - "1.18" - "1.19" - "1.20" diff --git a/assert/assertions.go b/assert/assertions.go index 6ab0ec347..a640985e2 100644 --- a/assert/assertions.go +++ b/assert/assertions.go @@ -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++ { diff --git a/assert/assertions_test.go b/assert/assertions_test.go index 162c71801..ad3f391ce 100644 --- a/assert/assertions_test.go +++ b/assert/assertions_test.go @@ -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) diff --git a/go.mod b/go.mod index d3c4d723b..c2f23dae9 100644 --- a/go.mod +++ b/go.mod @@ -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