helpers.gno

0.84 Kb ยท 51 lines
 1package uassert
 2
 3import "strings"
 4
 5func fail(t TestingT, customMsgs []string, failureMessage string, args ...any) bool {
 6	customMsg := ""
 7	if len(customMsgs) > 0 {
 8		customMsg = strings.Join(customMsgs, " ")
 9	}
10	if customMsg != "" {
11		failureMessage += " - " + customMsg
12	}
13	t.Errorf(failureMessage, args...)
14	return false
15}
16
17func checkDidPanic(f any) (didPanic bool, message string) {
18	didPanic = true
19	defer func() {
20		r := recover()
21
22		if r == nil {
23			message = "nil"
24			return
25		}
26
27		err, ok := r.(error)
28		if ok {
29			message = err.Error()
30			return
31		}
32
33		errStr, ok := r.(string)
34		if ok {
35			message = errStr
36			return
37		}
38
39		message = "recover: unsupported type"
40	}()
41
42	if fn, ok := f.(func()); ok {
43		fn()
44	} else if fn, ok := f.(func(realm)); ok {
45		fn(cross)
46	} else {
47		panic("f must be of type func() or func(realm)")
48	}
49	didPanic = false
50	return
51}