Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

assert: improve unsafe.Pointer tests #1505

Merged
merged 1 commit into from
Jan 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 0 additions & 8 deletions assert/assertions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import (
"strings"
"testing"
"time"
"unsafe"
)

var (
Expand Down Expand Up @@ -3138,10 +3137,3 @@ func TestErrorAs(t *testing.T) {
})
}
}

func TestIsNil(t *testing.T) {
var n unsafe.Pointer = nil
if !isNil(n) {
t.Fatal("fail")
}
}
4 changes: 4 additions & 0 deletions assert/internal/unsafetests/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// This package exists just to isolate tests that reference the [unsafe] package.
//
// The tests in this package are totally safe.
package unsafetests
34 changes: 34 additions & 0 deletions assert/internal/unsafetests/unsafetests_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package unsafetests_test

import (
"fmt"
"testing"
"unsafe"

"github.com/stretchr/testify/assert"
)

type ignoreTestingT struct{}

var _ assert.TestingT = ignoreTestingT{}

func (ignoreTestingT) Helper() {}

func (ignoreTestingT) Errorf(format string, args ...interface{}) {
// Run the formatting, but ignore the result
msg := fmt.Sprintf(format, args...)
_ = msg
}

func TestUnsafePointers(t *testing.T) {
var ignore ignoreTestingT

assert.True(t, assert.Nil(t, unsafe.Pointer(nil), "unsafe.Pointer(nil) is nil"))
assert.False(t, assert.NotNil(ignore, unsafe.Pointer(nil), "unsafe.Pointer(nil) is nil"))

assert.True(t, assert.Nil(t, unsafe.Pointer((*int)(nil)), "unsafe.Pointer((*int)(nil)) is nil"))
assert.False(t, assert.NotNil(ignore, unsafe.Pointer((*int)(nil)), "unsafe.Pointer((*int)(nil)) is nil"))

assert.False(t, assert.Nil(ignore, unsafe.Pointer(new(int)), "unsafe.Pointer(new(int)) is NOT nil"))
assert.True(t, assert.NotNil(t, unsafe.Pointer(new(int)), "unsafe.Pointer(new(int)) is NOT nil"))
}
Loading