z_func_6_filetest.gno
1.43 Kb ยท 54 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 testing.SetRealm(std.NewUserRealm(caller))
32
33 expect.Func(t, func() { Fail(cross) }).ToCrossPanic()
34 expect.Func(t, func() { Fail(cross) }).ToCrossPanic().WithMessage(msg)
35
36 expect.Func(t, func() error { Fail(cross); return nil }).ToCrossPanic()
37 expect.Func(t, func() error { Fail(cross); return nil }).ToCrossPanic().WithMessage(msg)
38
39 expect.Func(t, func() any { Fail(cross); return nil }).ToCrossPanic()
40 expect.Func(t, func() any { Fail(cross); return nil }).ToCrossPanic().WithMessage(msg)
41
42 expect.Func(t, func() (any, error) { Fail(cross); return nil, nil }).ToCrossPanic()
43 expect.Func(t, func() (any, error) { Fail(cross); return nil, nil }).ToCrossPanic().WithMessage(msg)
44
45 expect.Func(t, func() { Success(cross) }).Not().ToCrossPanic()
46 expect.Func(t, func() error { Success(cross); return nil }).Not().ToCrossPanic()
47 expect.Func(t, func() any { Success(cross); return nil }).Not().ToCrossPanic()
48 expect.Func(t, func() (any, error) { Success(cross); return nil, nil }).Not().ToCrossPanic()
49
50 // None should fail, output should be empty
51 print(output.String())
52}
53
54// Output: