Search Apps Documentation Source Content File Folder Download Copy Actions Download

z_func_6_filetest.gno

1.42 Kb ยท 53 lines
 1// PKGPATH: gno.land/r/demo/test
 2package test
 3
 4import (
 5	"strings"
 6	"testing"
 7
 8	"gno.land/p/jeronimoalbi/expect"
 9)
10
11const (
12	caller = address("g16jpf0puufcpcjkph5nxueec8etpcldz7zwgydq")
13	msg    = "Boom!"
14)
15
16var (
17	output strings.Builder
18	t      = expect.MockTestingT(&output)
19)
20
21func Fail(realm) {
22	panic(msg)
23}
24
25func Success(realm) {
26	// No panic
27}
28
29func main() {
30	testing.SetRealm(testing.NewUserRealm(caller))
31
32	expect.Func(t, func() { Fail(cross) }).ToCrossPanic()
33	expect.Func(t, func() { Fail(cross) }).ToCrossPanic().WithMessage(msg)
34
35	expect.Func(t, func() error { Fail(cross); return nil }).ToCrossPanic()
36	expect.Func(t, func() error { Fail(cross); return nil }).ToCrossPanic().WithMessage(msg)
37
38	expect.Func(t, func() any { Fail(cross); return nil }).ToCrossPanic()
39	expect.Func(t, func() any { Fail(cross); return nil }).ToCrossPanic().WithMessage(msg)
40
41	expect.Func(t, func() (any, error) { Fail(cross); return nil, nil }).ToCrossPanic()
42	expect.Func(t, func() (any, error) { Fail(cross); return nil, nil }).ToCrossPanic().WithMessage(msg)
43
44	expect.Func(t, func() { Success(cross) }).Not().ToCrossPanic()
45	expect.Func(t, func() error { Success(cross); return nil }).Not().ToCrossPanic()
46	expect.Func(t, func() any { Success(cross); return nil }).Not().ToCrossPanic()
47	expect.Func(t, func() (any, error) { Success(cross); return nil, nil }).Not().ToCrossPanic()
48
49	// None should fail, output should be empty
50	print(output.String())
51}
52
53// Output: