github.com/cloudflare/circl@v1.5.0/internal/test/test.go (about) 1 package test 2 3 import ( 4 "errors" 5 "fmt" 6 "strings" 7 "testing" 8 ) 9 10 // ReportError reports an error if got is different from want. 11 func ReportError(t testing.TB, got, want interface{}, inputs ...interface{}) { 12 b := &strings.Builder{} 13 fmt.Fprint(b, "\n") 14 for i, in := range inputs { 15 fmt.Fprintf(b, "in[%v]: %v\n", i, in) 16 } 17 fmt.Fprintf(b, "got: %v\nwant: %v", got, want) 18 t.Helper() 19 t.Fatal(b.String()) 20 } 21 22 // CheckOk fails the test if result == false. 23 func CheckOk(result bool, msg string, t testing.TB) { 24 t.Helper() 25 26 if !result { 27 t.Fatal(msg) 28 } 29 } 30 31 // checkErr fails on error condition. mustFail indicates whether err is expected 32 // to be nil or not. 33 func checkErr(t testing.TB, err error, mustFail bool, msg string) { 34 t.Helper() 35 if err != nil && !mustFail { 36 t.Fatalf("msg: %v\nerr: %v", msg, err) 37 } 38 39 if err == nil && mustFail { 40 t.Fatalf("msg: %v\nerr: %v", msg, err) 41 } 42 } 43 44 // CheckNoErr fails if err !=nil. Print msg as an error message. 45 func CheckNoErr(t testing.TB, err error, msg string) { t.Helper(); checkErr(t, err, false, msg) } 46 47 // CheckIsErr fails if err ==nil. Print msg as an error message. 48 func CheckIsErr(t testing.TB, err error, msg string) { t.Helper(); checkErr(t, err, true, msg) } 49 50 // CheckPanic returns true if call to function 'f' caused panic. 51 func CheckPanic(f func()) error { 52 hasPanicked := errors.New("no panic detected") 53 defer func() { 54 if r := recover(); r != nil { 55 hasPanicked = nil 56 } 57 }() 58 f() 59 return hasPanicked 60 }