Search Apps Documentation Source Content File Folder Download Copy Actions Download

z_func_7_filetest.gno

1.55 Kb ยท 65 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	wantMsg := "Tick Tock"
31
32	testing.SetRealm(testing.NewUserRealm(caller))
33
34	expect.Func(t, func() { Success(cross) }).ToCrossPanic()
35	expect.Func(t, func() { Fail(cross) }).ToCrossPanic().WithMessage(wantMsg)
36
37	expect.Func(t, func() error { Success(cross); return nil }).ToCrossPanic()
38	expect.Func(t, func() error { Fail(cross); return nil }).ToCrossPanic().WithMessage(wantMsg)
39
40	expect.Func(t, func() any { Success(cross); return nil }).ToCrossPanic()
41	expect.Func(t, func() any { Fail(cross); return nil }).ToCrossPanic().WithMessage(wantMsg)
42
43	expect.Func(t, func() (any, error) { Success(cross); return nil, nil }).ToCrossPanic()
44	expect.Func(t, func() (any, error) { Fail(cross); return nil, nil }).ToCrossPanic().WithMessage(wantMsg)
45
46	println(output.String())
47}
48
49// Output:
50// Expected function to cross panic
51// Expected cross panic message to match
52// Got: Boom!
53// Want: Tick Tock
54// Expected function to cross panic
55// Expected cross panic message to match
56// Got: Boom!
57// Want: Tick Tock
58// Expected function to cross panic
59// Expected cross panic message to match
60// Got: Boom!
61// Want: Tick Tock
62// Expected function to cross panic
63// Expected cross panic message to match
64// Got: Boom!
65// Want: Tick Tock