Skip to content

Commit 858080f

Browse files
committed
assert: more unsafe.Pointer tests
1. Isolate tests that use the "unsafe" package in a separate package assert/internal/unsafetests. That way the assert package is not tainted with unsafe. 2. Remove one reference to the private assert.isNil() in assert tests. 3. Add more tests of assert.Nil and assert.NotNil with unsafe.Pointer.
1 parent db8608e commit 858080f

File tree

3 files changed

+38
-8
lines changed

3 files changed

+38
-8
lines changed

assert/assertions_test.go

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ import (
1616
"strings"
1717
"testing"
1818
"time"
19-
"unsafe"
2019
)
2120

2221
var (
@@ -3138,10 +3137,3 @@ func TestErrorAs(t *testing.T) {
31383137
})
31393138
}
31403139
}
3141-
3142-
func TestIsNil(t *testing.T) {
3143-
var n unsafe.Pointer = nil
3144-
if !isNil(n) {
3145-
t.Fatal("fail")
3146-
}
3147-
}

assert/internal/unsafetests/doc.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
// This package exists just to isolate tests that reference the [unsafe] package.
2+
//
3+
// The tests in this package are totally safe.
4+
package unsafetests
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package unsafetests_test
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
"unsafe"
7+
8+
"github.com/stretchr/testify/assert"
9+
)
10+
11+
type ignoreTestingT struct{}
12+
13+
var _ assert.TestingT = ignoreTestingT{}
14+
15+
func (ignoreTestingT) Helper() {}
16+
17+
func (ignoreTestingT) Errorf(format string, args ...interface{}) {
18+
// Run the formatting, but ignore the result
19+
msg := fmt.Sprintf(format, args...)
20+
_ = msg
21+
}
22+
23+
func TestUnsafePointers(t *testing.T) {
24+
var ignore ignoreTestingT
25+
26+
assert.True(t, assert.Nil(t, unsafe.Pointer(nil), "unsafe.Pointer(nil) is nil"))
27+
assert.False(t, assert.NotNil(ignore, unsafe.Pointer(nil), "unsafe.Pointer(nil) is nil"))
28+
29+
assert.True(t, assert.Nil(t, unsafe.Pointer((*int)(nil)), "unsafe.Pointer((*int)(nil)) is nil"))
30+
assert.False(t, assert.NotNil(ignore, unsafe.Pointer((*int)(nil)), "unsafe.Pointer((*int)(nil)) is nil"))
31+
32+
assert.False(t, assert.Nil(ignore, unsafe.Pointer(new(int)), "unsafe.Pointer(new(int)) is NOT nil"))
33+
assert.True(t, assert.NotNil(t, unsafe.Pointer(new(int)), "unsafe.Pointer(new(int)) is NOT nil"))
34+
}

0 commit comments

Comments
 (0)