From b87db05698d0c6532a95d8511fbd9eefa2ff63da Mon Sep 17 00:00:00 2001 From: Donald King Date: Sun, 13 Jun 2021 19:13:08 -0700 Subject: [PATCH] Add Raise[f] helpers. --- assert.go | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/assert.go b/assert.go index bb3d860..1c0b2d8 100644 --- a/assert.go +++ b/assert.go @@ -6,12 +6,23 @@ import ( "strings" ) +// Raise panics with Error. +func Raise(text string) { + panic(Error{Text: text}) +} + +// Raisef panics with Error. +func Raisef(format string, v ...interface{}) { + text := fmt.Sprintf(format, v...) + panic(Error{Text: text}) +} + // Assert panics with Error if cond is false. func Assert(cond bool, text string) { if cond { return } - panic(Error{Text: text}) + Raise(text) } // Assertf panics with Error if cond is false. @@ -19,8 +30,7 @@ func Assertf(cond bool, format string, v ...interface{}) { if cond { return } - text := fmt.Sprintf(format, v...) - panic(Error{Text: text}) + Raisef(format, v...) } // NotNil takes a pointer to a nil-able type (pointer, interface, etc) and @@ -31,8 +41,7 @@ func NotNil(v interface{}) { if !r1.IsNil() { return } - text := fmt.Sprintf("%s is nil", r1.Type().String()) - panic(Error{Text: text}) + Raisef("%s is nil", r1.Type().String()) } // Error is the error type for Assert failure panics.