Skip to content

Commit

Permalink
assert.NotEmpty behaves the same as assert.NotNil
Browse files Browse the repository at this point in the history
  • Loading branch information
adamluzsi committed Jun 14, 2023
1 parent fcb3b04 commit c7e6010
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 16 deletions.
2 changes: 1 addition & 1 deletion assert/Asserter.go
Original file line number Diff line number Diff line change
Expand Up @@ -772,7 +772,7 @@ func (a Asserter) Empty(v any, msg ...any) {
// NotEmpty gets whether the specified value is considered empty.
func (a Asserter) NotEmpty(v any, msg ...any) {
a.TB.Helper()
if !a.try(func(a Asserter) { a.Empty(v) }) {
if !a.isEmpty(v) {
return
}
a.fn(fmterror.Message{
Expand Down
49 changes: 34 additions & 15 deletions assert/Asserter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"github.com/adamluzsi/testcase"
"io"
"math/big"
"math/rand"
"net"
"reflect"
"strings"
Expand Down Expand Up @@ -172,34 +171,38 @@ func TestAsserter_NotNil(t *testing.T) {
Equal(t, dtb.IsFailed, false)
})
t.Run("race", func(t *testing.T) {
rnd := random.New(random.CryptoSeed{})
type T struct{ V *int }

var v = T{V: func() *int {
n := 42
return &n
}()}

done := make(chan struct{})
defer close(done)
go func() {
for {
select {
case <-done:
return
default:
*v.V = rand.Int()
}
}
}()

time.Sleep(time.Microsecond)
doConcurrently(t, func() { *v.V = rnd.Int() })

blk := func() { assert.NotNil(t, &v) }

testcase.Race(blk, blk, blk)
})
}

func doConcurrently(tb testing.TB, do func()) {
done := make(chan struct{})
tb.Cleanup(func() { close(done) })
go func() {
for {
select {
case <-done:
return
default:
do()
}
}
}()
time.Sleep(time.Microsecond)
}

func TestAsserter_Panics(t *testing.T) {
t.Run(`when no panic, fails`, func(t *testing.T) {
dtb := &doubles.TB{}
Expand Down Expand Up @@ -1669,6 +1672,22 @@ func TestAsserter_NotEmpty(t *testing.T) {
}
})
}

t.Run("race", func(t *testing.T) {
type T struct{ V *int }
rnd := random.New(random.CryptoSeed{})

var v = T{V: func() *int {
n := 42
return &n
}()}

doConcurrently(t, func() { *v.V = rnd.Int() })

blk := func() { assert.NotEmpty(t, &v) }

testcase.Race(blk, blk, blk)
})
}

func TestAsserter_ErrorIs(t *testing.T) {
Expand Down

0 comments on commit c7e6010

Please sign in to comment.