Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions examples_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -990,6 +990,16 @@ func ExampleUnreachable() {
// Output:
}

func ExamplePanic() {
Panic(t, func() { panic("hi mom") })
// Output:
}

func ExampleNotPanic() {
NotPanic(t, func() {})
// Output:
}

func ExampleValidJSON() {
js := `{"key": ["v1", "v2"]}`
ValidJSON(t, js)
Expand Down
20 changes: 20 additions & 0 deletions internal/assertions/assertions.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,26 @@ func Unreachable() (s string) {
return
}

func Panic(f func()) (s string) {
defer func() {
if r := recover(); r == nil {
s = "expected panic; did not panic"
}
}()
f()
return
}

func NotPanic(f func()) (s string) {
defer func() {
if r := recover(); r != nil {
s = fmt.Sprintf("expected not to panic; panicked: %v", r)
}
}()
f()
return
}

func Error(err error) (s string) {
if err == nil {
s = "expected non-nil error; got nil\n"
Expand Down
10 changes: 10 additions & 0 deletions must/examples_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions must/must.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,18 @@ func Unreachable(t T, settings ...Setting) {
invoke(t, assertions.Unreachable(), settings...)
}

// Panic asserts func f panics.
func Panic(t T, f func(), settings ...Setting) {
t.Helper()
invoke(t, assertions.Panic(f), settings...)
}

// NotPanic asserts func f does not panic.
func NotPanic(t T, f func(), settings ...Setting) {
t.Helper()
invoke(t, assertions.NotPanic(f), settings...)
}

// Error asserts err is a non-nil error.
func Error(t T, err error, settings ...Setting) {
t.Helper()
Expand Down
Loading