Skip to content

Commit

Permalink
Add Raise[f] helpers.
Browse files Browse the repository at this point in the history
  • Loading branch information
chronos-tachyon committed Jun 14, 2021
1 parent 0dd6a0c commit b87db05
Showing 1 changed file with 14 additions and 5 deletions.
19 changes: 14 additions & 5 deletions assert.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,31 @@ 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.
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
Expand All @@ -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.
Expand Down

0 comments on commit b87db05

Please sign in to comment.