From 25026541a207621f132b5aa3ded61620dfa1cff9 Mon Sep 17 00:00:00 2001 From: Fahim Bagar Date: Wed, 1 Feb 2023 22:43:55 +0700 Subject: [PATCH 1/3] Add PanicAssertionFunc --- assert/assertions.go | 4 ++++ assert/assertions_test.go | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+) diff --git a/assert/assertions.go b/assert/assertions.go index 0b7570f21..979138c0a 100644 --- a/assert/assertions.go +++ b/assert/assertions.go @@ -45,6 +45,10 @@ type BoolAssertionFunc func(TestingT, bool, ...interface{}) bool // for table driven tests. type ErrorAssertionFunc func(TestingT, error, ...interface{}) bool +// PanicAssertionFunc is a common function prototype when validating a panic value. Can be useful +// for table driven tests. +type PanicAssertionFunc func(t TestingT, f PanicTestFunc, msgAndArgs ...interface{}) bool + // Comparison is a custom function that returns true on success and false on failure type Comparison func() (success bool) diff --git a/assert/assertions_test.go b/assert/assertions_test.go index 2a6e47234..66c6e441b 100644 --- a/assert/assertions_test.go +++ b/assert/assertions_test.go @@ -2789,6 +2789,42 @@ func TestErrorAssertionFunc(t *testing.T) { } } +func ExamplePanicAssertionFunc() { + t := &testing.T{} // provided by test + + tests := []struct { + name string + panicFn func() + assertion PanicAssertionFunc + }{ + {"with panic", func() { panic(nil) }, Panics}, + {"without panic", func() {}, NotPanics}, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + tt.assertion(t, tt.panicFn) + }) + } +} + +func TestPanicAssertionFunc(t *testing.T) { + tests := []struct { + name string + panicFn func() + assertion PanicAssertionFunc + }{ + {"not panic", func() {}, NotPanics}, + {"panic", func() { panic(nil) }, Panics}, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + tt.assertion(t, tt.panicFn) + }) + } +} + func TestEventuallyFalse(t *testing.T) { mockT := new(testing.T) From 0e5fddb29a8734f2b3afec17ff9f14c1599297cc Mon Sep 17 00:00:00 2001 From: Fahim Bagar Date: Thu, 6 Jul 2023 16:24:36 +0700 Subject: [PATCH 2/3] Use type alias --- assert/assertions.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/assert/assertions.go b/assert/assertions.go index 979138c0a..ccbd525d8 100644 --- a/assert/assertions.go +++ b/assert/assertions.go @@ -47,7 +47,7 @@ type ErrorAssertionFunc func(TestingT, error, ...interface{}) bool // PanicAssertionFunc is a common function prototype when validating a panic value. Can be useful // for table driven tests. -type PanicAssertionFunc func(t TestingT, f PanicTestFunc, msgAndArgs ...interface{}) bool +type PanicAssertionFunc = func(t TestingT, f PanicTestFunc, msgAndArgs ...interface{}) bool // Comparison is a custom function that returns true on success and false on failure type Comparison func() (success bool) From 2ab33d6f3babd0a8428a085e9d5c2112370aabc6 Mon Sep 17 00:00:00 2001 From: Fahim Bagar Date: Tue, 5 Mar 2024 12:12:02 +0700 Subject: [PATCH 3/3] Change type --- assert/assertions_test.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/assert/assertions_test.go b/assert/assertions_test.go index 66c6e441b..0ae36cd92 100644 --- a/assert/assertions_test.go +++ b/assert/assertions_test.go @@ -2794,7 +2794,7 @@ func ExamplePanicAssertionFunc() { tests := []struct { name string - panicFn func() + panicFn PanicTestFunc assertion PanicAssertionFunc }{ {"with panic", func() { panic(nil) }, Panics}, @@ -2811,7 +2811,7 @@ func ExamplePanicAssertionFunc() { func TestPanicAssertionFunc(t *testing.T) { tests := []struct { name string - panicFn func() + panicFn PanicTestFunc assertion PanicAssertionFunc }{ {"not panic", func() {}, NotPanics},