Skip to content

Commit 49f24de

Browse files
committed
style: merge assertion.go and errors.go
1 parent e9281d8 commit 49f24de

File tree

7 files changed

+131
-147
lines changed

7 files changed

+131
-147
lines changed

README.md

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -326,9 +326,6 @@ Error handling:
326326
- [TryWithErrorValue](#trywitherrorvalue)
327327
- [TryCatchWithErrorValue](#trycatchwitherrorvalue)
328328
- [ErrorsAs](#errorsas)
329-
330-
Assertions:
331-
332329
- [Assert](#assert)
333330
- [Assertf](#assertf)
334331

@@ -4102,9 +4099,7 @@ if rateLimitErr, ok := lo.ErrorsAs[*RateLimitError](err); ok {
41024099

41034100
### Assert
41044101

4105-
C/C++ style assertion.
4106-
4107-
It does nothing when the condition is `true`, otherwise it panics with an optional message.
4102+
Does nothing when the condition is `true`, otherwise it panics with an optional message.
41084103

41094104
Think twice before using it, given that [Go intentionally omits assertions from its standard library](https://go.dev/doc/faq#assertions).
41104105

assertions.go

Lines changed: 0 additions & 28 deletions
This file was deleted.

assertions_example_test.go

Lines changed: 0 additions & 45 deletions
This file was deleted.

assertions_test.go

Lines changed: 0 additions & 68 deletions
This file was deleted.

errors.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import (
66
"reflect"
77
)
88

9+
const defaultAssertionFailureMessage = "assertion failed"
10+
911
// Validate is a helper that creates an error when a condition is not met.
1012
// Play: https://go.dev/play/p/vPyh51XpCBt
1113
func Validate(ok bool, format string, args ...any) error {
@@ -352,3 +354,28 @@ func ErrorsAs[T error](err error) (T, bool) {
352354
ok := errors.As(err, &t)
353355
return t, ok
354356
}
357+
358+
// Assert does nothing when the condition is true, otherwise it panics with an optional message.
359+
// Play: https://go.dev/play/p/Xv8LLKBMNwI
360+
func Assert(condition bool, message ...string) {
361+
if condition {
362+
return
363+
}
364+
365+
panicMessage := defaultAssertionFailureMessage
366+
if len(message) > 0 {
367+
panicMessage = fmt.Sprintf("%s: %s", defaultAssertionFailureMessage, message[0])
368+
}
369+
panic(panicMessage)
370+
}
371+
372+
// Assertf does nothing when the condition is true, otherwise it panics with a formatted message.
373+
// Play: https://go.dev/play/p/TVPEmVcyrdY
374+
func Assertf(condition bool, format string, args ...any) {
375+
if condition {
376+
return
377+
}
378+
379+
panicMessage := fmt.Sprintf("%s: %s", defaultAssertionFailureMessage, fmt.Sprintf(format, args...))
380+
panic(panicMessage)
381+
}

errors_example_test.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -427,3 +427,45 @@ func ExampleErrorsAs() {
427427

428428
// Output: is type myError, err: my error
429429
}
430+
431+
func ExampleAssert() {
432+
defer func() {
433+
if r := recover(); r != nil {
434+
fmt.Println(r)
435+
}
436+
}()
437+
438+
age := 20
439+
440+
// won't panic
441+
Assert(age >= 18)
442+
443+
// won't panic
444+
Assert(age >= 18, "age must be at least 18")
445+
446+
// will panic
447+
Assert(age < 18)
448+
449+
// will panic
450+
Assert(age < 18, "age must be less than 18")
451+
452+
// Output: assertion failed
453+
}
454+
455+
func ExampleAssertf() {
456+
defer func() {
457+
if r := recover(); r != nil {
458+
fmt.Println(r)
459+
}
460+
}()
461+
462+
age := 20
463+
464+
// won't panic
465+
Assertf(age >= 18, "age must be at least 18, got %d", age)
466+
467+
// will panic
468+
Assertf(age < 18, "age must be less than 18, got %d", age)
469+
470+
// Output: assertion failed: age must be less than 18, got 20
471+
}

errors_test.go

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -597,3 +597,64 @@ func TestErrorsAs(t *testing.T) {
597597
is.False(ok)
598598
is.Nil(nil, err)
599599
}
600+
601+
func TestAssert(t *testing.T) {
602+
t.Parallel()
603+
is := assert.New(t)
604+
605+
is.NotPanics(func() {
606+
Assert(true)
607+
})
608+
609+
is.NotPanics(func() {
610+
Assert(true, "user defined message")
611+
})
612+
613+
is.PanicsWithValue("assertion failed", func() {
614+
Assert(false)
615+
})
616+
617+
is.PanicsWithValue("assertion failed: user defined message", func() {
618+
Assert(false, "user defined message")
619+
})
620+
621+
//checks that the examples in `README.md` compile
622+
{
623+
age := 20
624+
is.NotPanics(func() {
625+
Assert(age >= 15)
626+
})
627+
is.NotPanics(func() {
628+
Assert(age >= 15, "user age must be >= 15")
629+
})
630+
}
631+
}
632+
633+
func TestAssertf(t *testing.T) {
634+
t.Parallel()
635+
is := assert.New(t)
636+
637+
is.NotPanics(func() {
638+
Assertf(true, "user defined message")
639+
})
640+
641+
is.NotPanics(func() {
642+
Assertf(true, "user defined message %d %d", 1, 2)
643+
})
644+
645+
is.PanicsWithValue("assertion failed: user defined message", func() {
646+
Assertf(false, "user defined message")
647+
})
648+
649+
is.PanicsWithValue("assertion failed: user defined message 1 2", func() {
650+
Assertf(false, "user defined message %d %d", 1, 2)
651+
})
652+
653+
//checks that the example in `README.md` compiles
654+
{
655+
age := 7
656+
is.PanicsWithValue("assertion failed: user age must be >= 15, got 7", func() {
657+
Assertf(age >= 15, "user age must be >= 15, got %d", age)
658+
})
659+
}
660+
}

0 commit comments

Comments
 (0)