Created
May 27, 2020 14:53
-
-
Save takakd/d1be73e7ad237b8c97341cf3fe269075 to your computer and use it in GitHub Desktop.
Testing panic function.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Testing if testFunc calls panic. | |
// e.g. | |
// IsTestCallPanic(func(){ | |
// <place test target here.> | |
// }) | |
func IsTestCallPanic(testFunc func()) (ok bool){ | |
defer func() { | |
if err := recover(); err == nil { | |
ok = false | |
} | |
}() | |
ok = true | |
testFunc() | |
return | |
} | |
// Test Code. | |
func TestIsTestCallPanic(t *testing.T) { | |
isCalled := IsTestCallPanic(func() { | |
var i interface{} | |
if i == nil { | |
panic("Hi, panic.") | |
} | |
}) | |
if !isCalled { | |
t.Errorf("failed.") | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment