Skip to content

Commit 705e0df

Browse files
committed
chore: revert uassert Panics->Aborts in tests by default
Signed-off-by: moul <94029+moul@users.noreply.github.com>
1 parent 0ae9d4d commit 705e0df

File tree

17 files changed

+186
-154
lines changed

17 files changed

+186
-154
lines changed

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

Lines changed: 42 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -117,9 +117,8 @@ func AbortsWithMessage(t TestingT, msg string, f func(), msgs ...string) bool {
117117
}
118118

119119
// NotAborts asserts that the code inside the specified func does NOT abort
120-
// when crossing an execution boundary (e.g., VM call).
121-
// Use NotPanics for asserting the absence of local panics within the same realm.
122-
// Note: This relies on Gno's `revive` mechanism.
120+
// when crossing an execution boundary.
121+
// Note: Consider using NotPanics which checks for both panics and aborts.
123122
func NotAborts(t TestingT, f func(), msgs ...string) bool {
124123
t.Helper()
125124

@@ -135,7 +134,7 @@ func NotAborts(t TestingT, f func(), msgs ...string) bool {
135134
if didAbort {
136135
// Fail if the function aborted when it shouldn't have
137136
// Attempt to format the abort value in the error message
138-
return fail(t, msgs, "func should not abort\n\tAbort value:\t%v", abortValue)
137+
return fail(t, msgs, "func should not abort\\n\\tAbort value:\\t%v", abortValue)
139138
}
140139

141140
// Success: function did not abort
@@ -160,17 +159,50 @@ func PanicsWithMessage(t TestingT, msg string, f func(), msgs ...string) bool {
160159
}
161160

162161
// NotPanics asserts that the code inside the specified func does NOT panic
163-
// locally within the same execution realm.
164-
// Use NotAborts for asserting the absence of panics that cross execution boundaries (aborts).
162+
// (within the same realm) or abort (due to a cross-realm panic).
165163
func NotPanics(t TestingT, f func(), msgs ...string) bool {
166164
t.Helper()
167165

168-
didPanic, panicValue := checkDidPanic(f)
166+
var panicVal any
167+
var didPanic bool
168+
var abortVal any
169+
170+
// Use revive to catch cross-realm aborts
171+
abortVal = revive(func() {
172+
// Use defer+recover to catch same-realm panics
173+
defer func() {
174+
if r := recover(); r != nil {
175+
didPanic = true
176+
panicVal = r
177+
}
178+
}()
179+
// Execute the function
180+
f()
181+
})
169182

170-
if didPanic {
171-
return fail(t, msgs, "func should not panic\n\tPanic value:\t%s", panicValue)
183+
// Check if revive caught an abort
184+
if abortVal != nil {
185+
return fail(t, msgs, "func should not abort\n\tAbort value:\t%+v", abortVal)
172186
}
173-
return true
187+
188+
// Check if recover caught a panic
189+
if didPanic {
190+
// Format panic value for message
191+
panicMsg := ""
192+
if panicVal == nil {
193+
panicMsg = "nil"
194+
} else if err, ok := panicVal.(error); ok {
195+
panicMsg = err.Error()
196+
} else if str, ok := panicVal.(string); ok {
197+
panicMsg = str
198+
} else {
199+
// Fallback for other types
200+
panicMsg = "panic: unsupported type"
201+
}
202+
return fail(t, msgs, "func should not panic\n\tPanic value:\t%s", panicMsg)
203+
}
204+
205+
return true // No panic or abort occurred
174206
}
175207

176208
// Equal asserts that two objects are equal.

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -157,14 +157,14 @@ func TestAbortsWithMessage(t *testing.T) {
157157
func TestNotAborts(t *testing.T) {
158158
mockT := new(mockTestingT)
159159

160-
if !uassert.NotAborts(mockT, func() {
160+
if !uassert.NotPanics(mockT, func() {
161161
// noop
162162
}) {
163163
t.Error("NotAborts should return true")
164164
}
165165
mockT.empty(t)
166166

167-
if uassert.NotAborts(mockT, func() {
167+
if uassert.NotPanics(mockT, func() {
168168
cross(tests.ExecSwitch)(func() {
169169
panic("Abort!")
170170
})

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ func AbortsWithMessage(t uassert.TestingT, msg string, f func(), msgs ...string)
7272
// Note: This relies on Gno's `revive` mechanism.
7373
func NotAborts(t uassert.TestingT, f func(), msgs ...string) {
7474
t.Helper()
75-
if uassert.NotAborts(t, f, msgs...) {
75+
if uassert.NotPanics(t, f, msgs...) {
7676
return
7777
}
7878
t.FailNow()

examples/gno.land/r/agherasie/forms/forms_test.gno

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ func TestGetFormByID(t *testing.T) {
1717
closeAt := "2021-01-02T00:00:00Z"
1818
data := `[{"label":"Name","fieldType":"string","required":true},{"label":"Age","fieldType":"number","required":false},{"label":"Is this a test?","fieldType":"boolean","required":false},{"label":"Favorite Food","fieldType":"['Pizza', 'Schnitzel', 'Burger']","required":true},{"label":"Favorite Foods","fieldType":"{'Pizza', 'Schnitzel', 'Burger'}","required":true}]`
1919

20-
urequire.NotAborts(t, func() {
20+
urequire.NotPanics(t, func() {
2121
id := CreateForm(title, description, openAt, closeAt, data)
2222

2323
form := GetFormByID(id)
@@ -33,7 +33,7 @@ func TestGetForms(t *testing.T) {
3333
openAt := "2021-01-01T00:00:00Z"
3434
closeAt := "2021-01-02T00:00:00Z"
3535

36-
urequire.NotAborts(t, func() {
36+
urequire.NotPanics(t, func() {
3737
data1 := `[{"label":"Name","fieldType":"string","required":true}]`
3838
CreateForm("NameForm", description, openAt, closeAt, data1)
3939
data2 := `[{"label":"Age","fieldType":"number","required":false}]`

examples/gno.land/r/demo/foo20/foo20_test.gno

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ func TestErrConditions(t *testing.T) {
8484
{"Approve(empty, 1))", "invalid address", true, func() { cross(Approve)(empty, 1) }},
8585
}
8686
for _, tc := range tests {
87-
t.Run(tc.name, func(t *testing.T) {
87+
tuassert.PanicsWithMessageting.T) {
8888
if tc.isCross {
8989
uassert.AbortsWithMessage(t, tc.msg, tc.fn)
9090
} else {
@@ -102,7 +102,7 @@ func TestErrConditions(t *testing.T) {
102102
// fn func()
103103
// }{
104104
// // Test AbortsWithMessage
105-
// {"supply cannot be zero", func() { NewFoo20("foo", "f", 0) }},
105+
// uassert.PanicsWithMessage", func() { NewFoo20("foo", "f", 0) }},
106106
// {"symbol cannot be empty", func() { NewFoo20("foo", "", 1) }},
107107
// {"name cannot be empty", func() { NewFoo20("", "f", 1) }},
108108
// }

examples/gno.land/r/gnoland/users/v1/users_test.gno

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ func TestRegister_Valid(t *testing.T) {
2222
testing.SetOriginSend(std.NewCoins(std.NewCoin("ugnot", 1_000_000)))
2323
testing.SetRealm(std.NewUserRealm(aliceAddr))
2424

25-
uassert.NotAborts(t, func() {
25+
uassert.NotPanics(t, func() {
2626
cross(Register)(alice)
2727
})
2828

@@ -41,44 +41,44 @@ func TestRegister_Invalid(t *testing.T) {
4141
testing.SetRealm(std.NewUserRealm(bobAddr))
4242

4343
// Invalid usernames
44-
uassert.AbortsWithMessage(t, ErrInvalidUsername.Error(), func() {
44+
uassert.PanicsWithMessage(t, ErrInvalidUsername.Error(), func() {
4545
cross(Register)("alice") // vanity
4646
})
4747

48-
uassert.AbortsWithMessage(t, ErrInvalidUsername.Error(), func() {
48+
uassert.PanicsWithMessage(t, ErrInvalidUsername.Error(), func() {
4949
cross(Register)("") // empty
5050
})
5151

52-
uassert.AbortsWithMessage(t, ErrInvalidUsername.Error(), func() {
52+
uassert.PanicsWithMessage(t, ErrInvalidUsername.Error(), func() {
5353
cross(Register)(" ") // empty
5454
})
5555

56-
uassert.AbortsWithMessage(t, ErrInvalidUsername.Error(), func() {
56+
uassert.PanicsWithMessage(t, ErrInvalidUsername.Error(), func() {
5757
cross(Register)("123") // empty
5858
})
5959

60-
uassert.AbortsWithMessage(t, ErrInvalidUsername.Error(), func() {
60+
uassert.PanicsWithMessage(t, ErrInvalidUsername.Error(), func() {
6161
cross(Register)("123") // only numbers
6262
})
6363

64-
uassert.AbortsWithMessage(t, ErrInvalidUsername.Error(), func() {
64+
uassert.PanicsWithMessage(t, ErrInvalidUsername.Error(), func() {
6565
cross(Register)("alice&#($)") // non-allowed chars
6666
})
6767

68-
uassert.AbortsWithMessage(t, ErrInvalidUsername.Error(), func() {
68+
uassert.PanicsWithMessage(t, ErrInvalidUsername.Error(), func() {
6969
cross(Register)("Alice123") // upper-case
7070
})
7171

72-
uassert.AbortsWithMessage(t, ErrInvalidUsername.Error(), func() {
72+
uassert.PanicsWithMessage(t, ErrInvalidUsername.Error(), func() {
7373
cross(Register)("toolongusernametoolongusernametoolongusername123") // too long
7474
})
7575

7676
// Name taken
77-
urequire.NotAborts(t, func() {
77+
urequire.NotPanics(t, func() {
7878
cross(Register)(bob)
7979
})
8080

81-
uassert.AbortsWithMessage(t, susers.ErrNameTaken.Error(), func() {
81+
uassert.PanicsWithMessage(t, susers.ErrNameTaken.Error(), func() {
8282
cross(Register)(bob) // already registered
8383
})
8484
}
@@ -89,7 +89,7 @@ func TestRegister_InvalidPayment(t *testing.T) {
8989

9090
testing.SetOriginSend(std.NewCoins(std.NewCoin("ugnot", 12))) // invalid payment amount
9191

92-
uassert.AbortsWithMessage(t, ErrInvalidPayment.Error(), func() {
92+
uassert.PanicsWithMessage(t, ErrInvalidPayment.Error(), func() {
9393
cross(Register)(alice)
9494
})
9595
}
@@ -100,7 +100,7 @@ func TestRegister_InvalidPayment(t *testing.T) {
100100

101101
testing.SetOriginSend(std.NewCoins(std.NewCoin("ugnot", 12))) // invalid payment amount
102102

103-
uassert.AbortsWithMessage(t, ErrInvalidPayment.Error(), func() {
103+
uassert.PanicsWithMessage(t, ErrInvalidPayment.Error(), func() {
104104
cross(Register)(alice)
105105
})
106106
}
@@ -110,7 +110,7 @@ func TestUsers(t *testing.T) {
110110
fee := "1000ugnot"
111111

112112
// Check initial state
113-
uassert.NotAborts(t, func() {
113+
uassert.NotPanics(t, func() {
114114
MustGetByName(susers.DefaultMinFee)
115115
})
116116
}

examples/gno.land/r/gnoland/valopers/admin_test.gno

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,13 @@ func TestUpdateInstructions(t *testing.T) {
2020

2121
newInstructions := "new instructions"
2222

23-
uassert.AbortsWithMessage(t, "action can only be executed by the contract", func() {
23+
uassert.PanicsWithMessage(t, "action can only be executed by the contract", func() {
2424
updateInstructions(newInstructions)
2525
})
2626

2727
testing.SetOriginCaller(std.DerivePkgAddr("gno.land/r/gov/dao"))
2828

29-
uassert.NotAborts(t, func() {
29+
uassert.NotPanics(t, func() {
3030
updateInstructions(newInstructions)
3131
})
3232

@@ -45,13 +45,13 @@ func TestUpdateMinFee(t *testing.T) {
4545

4646
newMinFee := int64(100)
4747

48-
uassert.AbortsWithMessage(t, "action can only be executed by the contract", func() {
48+
uassert.PanicsWithMessage(t, "action can only be executed by the contract", func() {
4949
updateMinFee(newMinFee)
5050
})
5151

5252
testing.SetOriginCaller(std.DerivePkgAddr("gno.land/r/gov/dao"))
5353

54-
uassert.NotAborts(t, func() {
54+
uassert.NotPanics(t, func() {
5555
updateMinFee(newMinFee)
5656
})
5757

0 commit comments

Comments
 (0)