From c7e601033d68658bb8d86e8ced528f787f080a19 Mon Sep 17 00:00:00 2001 From: Adam Luzsi Date: Wed, 14 Jun 2023 20:58:32 +0200 Subject: [PATCH] assert.NotEmpty behaves the same as assert.NotNil --- assert/Asserter.go | 2 +- assert/Asserter_test.go | 49 ++++++++++++++++++++++++++++------------- 2 files changed, 35 insertions(+), 16 deletions(-) diff --git a/assert/Asserter.go b/assert/Asserter.go index 02cccb3..22d9b1f 100644 --- a/assert/Asserter.go +++ b/assert/Asserter.go @@ -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{ diff --git a/assert/Asserter_test.go b/assert/Asserter_test.go index 22ec89b..c329eaf 100644 --- a/assert/Asserter_test.go +++ b/assert/Asserter_test.go @@ -7,7 +7,6 @@ import ( "github.com/adamluzsi/testcase" "io" "math/big" - "math/rand" "net" "reflect" "strings" @@ -172,6 +171,7 @@ 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 { @@ -179,20 +179,7 @@ func TestAsserter_NotNil(t *testing.T) { 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) } @@ -200,6 +187,22 @@ func TestAsserter_NotNil(t *testing.T) { }) } +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{} @@ -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) {