Skip to content

Commit c499e3a

Browse files
committed
urequire/uassert takes func(realm) too
1 parent a8994a9 commit c499e3a

File tree

4 files changed

+58
-26
lines changed

4 files changed

+58
-26
lines changed

examples/gno.land/p/demo/uassert/helpers.gno

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ func fail(t TestingT, customMsgs []string, failureMessage string, args ...any) b
1414
return false
1515
}
1616

17-
func checkDidPanic(f func()) (didPanic bool, message string) {
17+
func checkDidPanic(f any) (didPanic bool, message string) {
1818
didPanic = true
1919
defer func() {
2020
r := recover()
@@ -38,7 +38,14 @@ func checkDidPanic(f func()) (didPanic bool, message string) {
3838

3939
message = "recover: unsupported type"
4040
}()
41-
f()
41+
switch f := f.(type) {
42+
case func():
43+
f()
44+
case func(realm):
45+
f(cross)
46+
default:
47+
panic("f must be of type func() or func(realm)")
48+
}
4249
didPanic = false
4350
return
4451
}

examples/gno.land/p/demo/uassert/uassert.gno

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -83,14 +83,23 @@ func ErrorIs(t TestingT, err, target error, msgs ...string) bool {
8383
// AbortsWithMessage asserts that the code inside the specified func aborts
8484
// (panics when crossing another realm).
8585
// Use PanicsWithMessage for asserting local panics within the same realm.
86-
// Note: This relies on gno's `revive` mechanism to catch aborts.
87-
func AbortsWithMessage(t TestingT, msg string, f func(), msgs ...string) bool {
86+
//
87+
// NOTE: This relies on gno's `revive` mechanism to catch aborts.
88+
func AbortsWithMessage(t TestingT, msg string, f any, msgs ...string) bool {
8889
t.Helper()
8990

9091
var didAbort bool
9192
var abortValue any
93+
var r any
9294

93-
r := revive(f) // revive() captures the value passed to panic()
95+
switch f := f.(type) {
96+
case func():
97+
r = revive(f) // revive() captures the value passed to panic()
98+
case func(realm):
99+
r = revive(func() { f(cross) })
100+
default:
101+
panic("f must be of type func() or func(realm)")
102+
}
94103
if r != nil {
95104
didAbort = true
96105
abortValue = r
@@ -114,13 +123,21 @@ func AbortsWithMessage(t TestingT, msg string, f func(), msgs ...string) bool {
114123
// NotAborts asserts that the code inside the specified func does NOT abort
115124
// when crossing an execution boundary.
116125
// Note: Consider using NotPanics which checks for both panics and aborts.
117-
func NotAborts(t TestingT, f func(), msgs ...string) bool {
126+
func NotAborts(t TestingT, f any, msgs ...string) bool {
118127
t.Helper()
119128

120129
var didAbort bool
121130
var abortValue any
131+
var r any
122132

123-
r := revive(f) // revive() captures the value passed to panic()
133+
switch f := f.(type) {
134+
case func():
135+
r = revive(f) // revive() captures the value passed to panic()
136+
case func(realm):
137+
r = revive(func() { f(cross) })
138+
default:
139+
panic("f must be of type func() or func(realm)")
140+
}
124141
if r != nil {
125142
didAbort = true
126143
abortValue = r
@@ -139,7 +156,7 @@ func NotAborts(t TestingT, f func(), msgs ...string) bool {
139156
// PanicsWithMessage asserts that the code inside the specified func panics
140157
// locally within the same execution realm.
141158
// Use AbortsWithMessage for asserting panics that cross execution boundaries (aborts).
142-
func PanicsWithMessage(t TestingT, msg string, f func(), msgs ...string) bool {
159+
func PanicsWithMessage(t TestingT, msg string, f any, msgs ...string) bool {
143160
t.Helper()
144161

145162
didPanic, panicValue := checkDidPanic(f)
@@ -157,7 +174,7 @@ func PanicsWithMessage(t TestingT, msg string, f func(), msgs ...string) bool {
157174

158175
// NotPanics asserts that the code inside the specified func does NOT panic
159176
// (within the same realm) or abort (due to a cross-realm panic).
160-
func NotPanics(t TestingT, f func(), msgs ...string) bool {
177+
func NotPanics(t TestingT, f any, msgs ...string) bool {
161178
t.Helper()
162179

163180
var panicVal any
@@ -174,7 +191,14 @@ func NotPanics(t TestingT, f func(), msgs ...string) bool {
174191
}
175192
}()
176193
// Execute the function
177-
f()
194+
switch f := f.(type) {
195+
case func():
196+
f()
197+
case func(realm):
198+
f(cross)
199+
default:
200+
panic("f must be of type func() or func(realm)")
201+
}
178202
})
179203

180204
// Check if revive caught an abort

examples/gno.land/p/demo/urequire/urequire.gno

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ func ErrorIs(t uassert.TestingT, err, target error, msgs ...string) {
5858
// (panics when crossing another realm).
5959
// Use PanicsWithMessage for requiring local panics within the same realm.
6060
// Note: This relies on gno's `revive` mechanism to catch aborts.
61-
func AbortsWithMessage(t uassert.TestingT, msg string, f func(), msgs ...string) {
61+
func AbortsWithMessage(t uassert.TestingT, msg string, f any, msgs ...string) {
6262
t.Helper()
6363
if uassert.AbortsWithMessage(t, msg, f, msgs...) {
6464
return
@@ -70,7 +70,7 @@ func AbortsWithMessage(t uassert.TestingT, msg string, f func(), msgs ...string)
7070
// when crossing an execution boundary (e.g., VM call).
7171
// Use NotPanics for requiring the absence of local panics within the same realm.
7272
// Note: This relies on Gno's `revive` mechanism.
73-
func NotAborts(t uassert.TestingT, f func(), msgs ...string) {
73+
func NotAborts(t uassert.TestingT, f any, msgs ...string) {
7474
t.Helper()
7575
if uassert.NotPanics(t, f, msgs...) {
7676
return
@@ -81,7 +81,7 @@ func NotAborts(t uassert.TestingT, f func(), msgs ...string) {
8181
// PanicsWithMessage requires that the code inside the specified func panics
8282
// locally within the same execution realm.
8383
// Use AbortsWithMessage for requiring panics that cross execution boundaries (aborts).
84-
func PanicsWithMessage(t uassert.TestingT, msg string, f func(), msgs ...string) {
84+
func PanicsWithMessage(t uassert.TestingT, msg string, f any, msgs ...string) {
8585
t.Helper()
8686
if uassert.PanicsWithMessage(t, msg, f, msgs...) {
8787
return
@@ -92,7 +92,7 @@ func PanicsWithMessage(t uassert.TestingT, msg string, f func(), msgs ...string)
9292
// NotPanics requires that the code inside the specified func does NOT panic
9393
// locally within the same execution realm.
9494
// Use NotAborts for requiring the absence of panics that cross execution boundaries (aborts).
95-
func NotPanics(t uassert.TestingT, f func(), msgs ...string) {
95+
func NotPanics(t uassert.TestingT, f any, msgs ...string) {
9696
t.Helper()
9797
if uassert.NotPanics(t, f, msgs...) {
9898
return

gnovm/pkg/gnolang/transpile_gno0p9.go

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ import (
4747
func PrepareGno0p9(gofset *token.FileSet, gofs []*ast.File, mpkg *std.MemPackage) (errs error) {
4848
for _, gof := range gofs {
4949
// AST transform for Gno 0.9.
50-
err := prepareGno0p9_part1(gof)
50+
err := prepareGno0p9(gof)
5151
if err != nil {
5252
errs = multierr.Append(errs, err)
5353
continue
@@ -61,35 +61,36 @@ func PrepareGno0p9(gofset *token.FileSet, gofs []*ast.File, mpkg *std.MemPackage
6161
if err != nil {
6262
errs = multierr.Append(errs, err)
6363
}
64-
// NOTE: If there was a need to preserve the gofs,
65-
// a reversal can happen here as prepareGno0p2_part2().
6664
return errs
6765
}
6866

6967
// Minimal AST mutation(s) for Gno 0.9.
70-
func prepareGno0p9_part1(f *ast.File) (err error) {
68+
func prepareGno0p9(f *ast.File) (err error) {
7169
astutil.Apply(f, func(c *astutil.Cursor) bool {
7270
switch gon := c.Node().(type) {
7371
case *ast.Ident:
7472
// XXX: optimistic.
7573
switch gon.Name {
7674
case "cross":
77-
gon.Name = "_cross_gno0p0" // only exists in .gnobuiltins.gno for gno 0.0
75+
// only exists in .gnobuiltins.gno for gno 0.0
76+
gon.Name = "_cross_gno0p0"
7877
case "realm":
7978
gon.Name = "realm_XXX"
80-
case "realm_gno0p9": // not used
81-
gon.Name = "realm"
8279
case "address":
8380
gon.Name = "address_XXX"
84-
case "address_gno0p9": // not used
85-
gon.Name = "address"
8681
case "gnocoin":
8782
gon.Name = "gnocoin_XXX"
88-
case "gnocoin_gno0p9": // not used
89-
gon.Name = "gnocoin"
9083
case "gnocoins":
9184
gon.Name = "gnocoins_XXX"
92-
case "gnocoins_gno0p9": // not used
85+
case "cross_gno0p9":
86+
gon.Name = "cross"
87+
case "realm_gno0p9": // doesn't work, prepare in pkg/test/import.
88+
gon.Name = "realm"
89+
case "address_gno0p9":
90+
gon.Name = "address"
91+
case "gnocoin_gno0p9":
92+
gon.Name = "gnocoin"
93+
case "gnocoins_gno0p9":
9394
gon.Name = "gnocoins"
9495
}
9596
}

0 commit comments

Comments
 (0)