style: merge assertion.go and errors.go

This commit is contained in:
Samuel Berthe
2025-06-01 16:19:29 +02:00
parent e9281d8a66
commit 49f24de919
7 changed files with 131 additions and 147 deletions

View File

@@ -326,9 +326,6 @@ Error handling:
- [TryWithErrorValue](#trywitherrorvalue)
- [TryCatchWithErrorValue](#trycatchwitherrorvalue)
- [ErrorsAs](#errorsas)
Assertions:
- [Assert](#assert)
- [Assertf](#assertf)
@@ -4102,9 +4099,7 @@ if rateLimitErr, ok := lo.ErrorsAs[*RateLimitError](err); ok {
### Assert
C/C++ style assertion.
It does nothing when the condition is `true`, otherwise it panics with an optional message.
Does nothing when the condition is `true`, otherwise it panics with an optional message.
Think twice before using it, given that [Go intentionally omits assertions from its standard library](https://go.dev/doc/faq#assertions).

View File

@@ -1,28 +0,0 @@
package lo
import "fmt"
const defaultAssertionFailureMessage = "assertion failed"
// Assert does nothing when the condition is true, otherwise it panics with an optional message.
func Assert(condition bool, message ...string) {
if condition {
return
}
panicMessage := defaultAssertionFailureMessage
if len(message) > 0 {
panicMessage = fmt.Sprintf("%s: %s", defaultAssertionFailureMessage, message[0])
}
panic(panicMessage)
}
// Assertf does nothing when the condition is true, otherwise it panics with a formatted message.
func Assertf(condition bool, format string, args ...any) {
if condition {
return
}
panicMessage := fmt.Sprintf("%s: %s", defaultAssertionFailureMessage, fmt.Sprintf(format, args...))
panic(panicMessage)
}

View File

@@ -1,45 +0,0 @@
package lo
import "fmt"
func ExampleAssert() {
defer func() {
if r := recover(); r != nil {
fmt.Println(r)
}
}()
age := 20
// won't panic
Assert(age >= 18)
// won't panic
Assert(age >= 18, "age must be at least 18")
// will panic
Assert(age < 18)
// will panic
Assert(age < 18, "age must be less than 18")
// Output: assertion failed
}
func ExampleAssertf() {
defer func() {
if r := recover(); r != nil {
fmt.Println(r)
}
}()
age := 20
// won't panic
Assertf(age >= 18, "age must be at least 18, got %d", age)
// will panic
Assertf(age < 18, "age must be less than 18, got %d", age)
// Output: assertion failed: age must be less than 18, got 20
}

View File

@@ -1,68 +0,0 @@
package lo
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestAssert(t *testing.T) {
t.Parallel()
is := assert.New(t)
is.NotPanics(func() {
Assert(true)
})
is.NotPanics(func() {
Assert(true, "user defined message")
})
is.PanicsWithValue("assertion failed", func() {
Assert(false)
})
is.PanicsWithValue("assertion failed: user defined message", func() {
Assert(false, "user defined message")
})
//checks that the examples in `README.md` compile
{
age := 20
is.NotPanics(func() {
Assert(age >= 15)
})
is.NotPanics(func() {
Assert(age >= 15, "user age must be >= 15")
})
}
}
func TestAssertf(t *testing.T) {
t.Parallel()
is := assert.New(t)
is.NotPanics(func() {
Assertf(true, "user defined message")
})
is.NotPanics(func() {
Assertf(true, "user defined message %d %d", 1, 2)
})
is.PanicsWithValue("assertion failed: user defined message", func() {
Assertf(false, "user defined message")
})
is.PanicsWithValue("assertion failed: user defined message 1 2", func() {
Assertf(false, "user defined message %d %d", 1, 2)
})
//checks that the example in `README.md` compiles
{
age := 7
is.PanicsWithValue("assertion failed: user age must be >= 15, got 7", func() {
Assertf(age >= 15, "user age must be >= 15, got %d", age)
})
}
}

View File

@@ -6,6 +6,8 @@ import (
"reflect"
)
const defaultAssertionFailureMessage = "assertion failed"
// Validate is a helper that creates an error when a condition is not met.
// Play: https://go.dev/play/p/vPyh51XpCBt
func Validate(ok bool, format string, args ...any) error {
@@ -352,3 +354,28 @@ func ErrorsAs[T error](err error) (T, bool) {
ok := errors.As(err, &t)
return t, ok
}
// Assert does nothing when the condition is true, otherwise it panics with an optional message.
// Play: https://go.dev/play/p/Xv8LLKBMNwI
func Assert(condition bool, message ...string) {
if condition {
return
}
panicMessage := defaultAssertionFailureMessage
if len(message) > 0 {
panicMessage = fmt.Sprintf("%s: %s", defaultAssertionFailureMessage, message[0])
}
panic(panicMessage)
}
// Assertf does nothing when the condition is true, otherwise it panics with a formatted message.
// Play: https://go.dev/play/p/TVPEmVcyrdY
func Assertf(condition bool, format string, args ...any) {
if condition {
return
}
panicMessage := fmt.Sprintf("%s: %s", defaultAssertionFailureMessage, fmt.Sprintf(format, args...))
panic(panicMessage)
}

View File

@@ -427,3 +427,45 @@ func ExampleErrorsAs() {
// Output: is type myError, err: my error
}
func ExampleAssert() {
defer func() {
if r := recover(); r != nil {
fmt.Println(r)
}
}()
age := 20
// won't panic
Assert(age >= 18)
// won't panic
Assert(age >= 18, "age must be at least 18")
// will panic
Assert(age < 18)
// will panic
Assert(age < 18, "age must be less than 18")
// Output: assertion failed
}
func ExampleAssertf() {
defer func() {
if r := recover(); r != nil {
fmt.Println(r)
}
}()
age := 20
// won't panic
Assertf(age >= 18, "age must be at least 18, got %d", age)
// will panic
Assertf(age < 18, "age must be less than 18, got %d", age)
// Output: assertion failed: age must be less than 18, got 20
}

View File

@@ -597,3 +597,64 @@ func TestErrorsAs(t *testing.T) {
is.False(ok)
is.Nil(nil, err)
}
func TestAssert(t *testing.T) {
t.Parallel()
is := assert.New(t)
is.NotPanics(func() {
Assert(true)
})
is.NotPanics(func() {
Assert(true, "user defined message")
})
is.PanicsWithValue("assertion failed", func() {
Assert(false)
})
is.PanicsWithValue("assertion failed: user defined message", func() {
Assert(false, "user defined message")
})
//checks that the examples in `README.md` compile
{
age := 20
is.NotPanics(func() {
Assert(age >= 15)
})
is.NotPanics(func() {
Assert(age >= 15, "user age must be >= 15")
})
}
}
func TestAssertf(t *testing.T) {
t.Parallel()
is := assert.New(t)
is.NotPanics(func() {
Assertf(true, "user defined message")
})
is.NotPanics(func() {
Assertf(true, "user defined message %d %d", 1, 2)
})
is.PanicsWithValue("assertion failed: user defined message", func() {
Assertf(false, "user defined message")
})
is.PanicsWithValue("assertion failed: user defined message 1 2", func() {
Assertf(false, "user defined message %d %d", 1, 2)
})
//checks that the example in `README.md` compiles
{
age := 7
is.PanicsWithValue("assertion failed: user age must be >= 15, got 7", func() {
Assertf(age >= 15, "user age must be >= 15, got %d", age)
})
}
}