z_func_7_filetest.gno

1.56 Kb ยท 66 lines
 1// PKGPATH: gno.land/r/demo/test
 2package test
 3
 4import (
 5	"std"
 6	"strings"
 7	"testing"
 8
 9	"gno.land/p/jeronimoalbi/expect"
10)
11
12const (
13	caller = std.Address("g16jpf0puufcpcjkph5nxueec8etpcldz7zwgydq")
14	msg    = "Boom!"
15)
16
17var (
18	output strings.Builder
19	t      = expect.MockTestingT(&output)
20)
21
22func Fail(realm) {
23	panic(msg)
24}
25
26func Success(realm) {
27	// No panic
28}
29
30func main() {
31	wantMsg := "Tick Tock"
32
33	testing.SetRealm(std.NewUserRealm(caller))
34
35	expect.Func(t, func() { Success(cross) }).ToCrossPanic()
36	expect.Func(t, func() { Fail(cross) }).ToCrossPanic().WithMessage(wantMsg)
37
38	expect.Func(t, func() error { Success(cross); return nil }).ToCrossPanic()
39	expect.Func(t, func() error { Fail(cross); return nil }).ToCrossPanic().WithMessage(wantMsg)
40
41	expect.Func(t, func() any { Success(cross); return nil }).ToCrossPanic()
42	expect.Func(t, func() any { Fail(cross); return nil }).ToCrossPanic().WithMessage(wantMsg)
43
44	expect.Func(t, func() (any, error) { Success(cross); return nil, nil }).ToCrossPanic()
45	expect.Func(t, func() (any, error) { Fail(cross); return nil, nil }).ToCrossPanic().WithMessage(wantMsg)
46
47	println(output.String())
48}
49
50// Output:
51// Expected function to cross panic
52// Expected cross panic message to match
53// Got: Boom!
54// Want: Tick Tock
55// Expected function to cross panic
56// Expected cross panic message to match
57// Got: Boom!
58// Want: Tick Tock
59// Expected function to cross panic
60// Expected cross panic message to match
61// Got: Boom!
62// Want: Tick Tock
63// Expected function to cross panic
64// Expected cross panic message to match
65// Got: Boom!
66// Want: Tick Tock