Skip to content Search Apps Documentation Source Content Run Reset Eval Playground File Folder Download Copy Actions State String Boolean Number Struct Map Slice Pointer Function Closure Reference Nil Package Type Interface Unknown

urequire.gno

5.74 Kb · 207 lines
  1// urequire is a sister package for uassert.
  2// XXX: codegen the package.
  3package urequire
  4
  5import "gno.land/p/nt/uassert/v0"
  6
  7// type TestingT = uassert.TestingT // XXX: bug, should work
  8
  9// NoError requires that a function returned no error (i.e. `nil`).
 10func NoError(t uassert.TestingT, err error, msgs ...string) {
 11	t.Helper()
 12	if uassert.NoError(t, err, msgs...) {
 13		return
 14	}
 15	t.FailNow()
 16}
 17
 18// Error requires that a function returned an error (i.e. not `nil`).
 19func Error(t uassert.TestingT, err error, msgs ...string) {
 20	t.Helper()
 21	if uassert.Error(t, err, msgs...) {
 22		return
 23	}
 24	t.FailNow()
 25}
 26
 27// ErrorContains requires that a function returned an error (i.e. not `nil`)
 28// and that the error contains the specified substring.
 29func ErrorContains(t uassert.TestingT, err error, contains string, msgs ...string) {
 30	t.Helper()
 31	if uassert.ErrorContains(t, err, contains, msgs...) {
 32		return
 33	}
 34	t.FailNow()
 35}
 36
 37// True requires that the specified value is true.
 38func True(t uassert.TestingT, value bool, msgs ...string) {
 39	t.Helper()
 40	if uassert.True(t, value, msgs...) {
 41		return
 42	}
 43	t.FailNow()
 44}
 45
 46// False requires that the specified value is false.
 47func False(t uassert.TestingT, value bool, msgs ...string) {
 48	t.Helper()
 49	if uassert.False(t, value, msgs...) {
 50		return
 51	}
 52	t.FailNow()
 53}
 54
 55// ErrorIs requires that the given error matches the target error.
 56func ErrorIs(t uassert.TestingT, err, target error, msgs ...string) {
 57	t.Helper()
 58	if uassert.ErrorIs(t, err, target, msgs...) {
 59		return
 60	}
 61	t.FailNow()
 62}
 63
 64// AbortsWithMessage requires that the code inside the specified func aborts
 65// (panics when crossing another realm).
 66// Use PanicsWithMessage for requiring local panics within the same realm.
 67// Note: This relies on gno's `revive` mechanism to catch aborts.
 68// See uassert.AbortsWithMessage for `rlm` semantics.
 69func AbortsWithMessage(t uassert.TestingT, rlm realm, msg string, f any, msgs ...string) {
 70	t.Helper()
 71	if uassert.AbortsWithMessage(t, rlm, msg, f, msgs...) {
 72		return
 73	}
 74	t.FailNow()
 75}
 76
 77// AbortsContains requires that the code inside the specified func aborts
 78// (panics when crossing another realm) and the abort message contains the specified substring.
 79// See uassert.AbortsWithMessage for `rlm` semantics.
 80func AbortsContains(t uassert.TestingT, rlm realm, substr string, f any, msgs ...string) {
 81	t.Helper()
 82	if uassert.AbortsContains(t, rlm, substr, f, msgs...) {
 83		return
 84	}
 85	t.FailNow()
 86}
 87
 88// NotAborts requires that the code inside the specified func does NOT abort
 89// when crossing an execution boundary (e.g., VM call).
 90// Use NotPanics for requiring the absence of local panics within the same realm.
 91// Note: This relies on Gno's `revive` mechanism.
 92// See uassert.AbortsWithMessage for `rlm` semantics.
 93func NotAborts(t uassert.TestingT, rlm realm, f any, msgs ...string) {
 94	t.Helper()
 95	if uassert.NotAborts(t, rlm, f, msgs...) {
 96		return
 97	}
 98	t.FailNow()
 99}
100
101// PanicsWithMessage requires that the code inside the specified func panics
102// locally within the same execution realm.
103// Use AbortsWithMessage for requiring panics that cross execution boundaries (aborts).
104// See uassert.AbortsWithMessage for `rlm` semantics.
105func PanicsWithMessage(t uassert.TestingT, rlm realm, msg string, f any, msgs ...string) {
106	t.Helper()
107	if uassert.PanicsWithMessage(t, rlm, msg, f, msgs...) {
108		return
109	}
110	t.FailNow()
111}
112
113// PanicsContains requires that the code inside the specified func panics
114// locally within the same execution realm and the panic message contains the specified substring.
115// See uassert.AbortsWithMessage for `rlm` semantics.
116func PanicsContains(t uassert.TestingT, rlm realm, substr string, f any, msgs ...string) {
117	t.Helper()
118	if uassert.PanicsContains(t, rlm, substr, f, msgs...) {
119		return
120	}
121	t.FailNow()
122}
123
124// NotPanics requires that the code inside the specified func does NOT panic
125// locally within the same execution realm.
126// Use NotAborts for requiring the absence of panics that cross execution boundaries (aborts).
127// See uassert.AbortsWithMessage for `rlm` semantics.
128func NotPanics(t uassert.TestingT, rlm realm, f any, msgs ...string) {
129	t.Helper()
130	if uassert.NotPanics(t, rlm, f, msgs...) {
131		return
132	}
133	t.FailNow()
134}
135
136// Equal requires that two objects are equal.
137func Equal(t uassert.TestingT, expected, actual any, msgs ...string) {
138	t.Helper()
139	if uassert.Equal(t, expected, actual, msgs...) {
140		return
141	}
142	t.FailNow()
143}
144
145// NotEqual requires that two objects are not equal.
146func NotEqual(t uassert.TestingT, expected, actual any, msgs ...string) {
147	t.Helper()
148	if uassert.NotEqual(t, expected, actual, msgs...) {
149		return
150	}
151	t.FailNow()
152}
153
154// Empty requires that the specified object is empty
155// (zero value, empty string/slice/map, or nil).
156func Empty(t uassert.TestingT, obj any, msgs ...string) {
157	t.Helper()
158	if uassert.Empty(t, obj, msgs...) {
159		return
160	}
161	t.FailNow()
162}
163
164// NotEmpty requires that the specified object is not empty.
165func NotEmpty(t uassert.TestingT, obj any, msgs ...string) {
166	t.Helper()
167	if uassert.NotEmpty(t, obj, msgs...) {
168		return
169	}
170	t.FailNow()
171}
172
173// Nil requires that the value is nil.
174func Nil(t uassert.TestingT, value any, msgs ...string) {
175	t.Helper()
176	if uassert.Nil(t, value, msgs...) {
177		return
178	}
179	t.FailNow()
180}
181
182// NotNil requires that the value is not nil.
183func NotNil(t uassert.TestingT, value any, msgs ...string) {
184	t.Helper()
185	if uassert.NotNil(t, value, msgs...) {
186		return
187	}
188	t.FailNow()
189}
190
191// TypedNil requires that the value is a typed-nil (nil pointer) value.
192func TypedNil(t uassert.TestingT, value any, msgs ...string) {
193	t.Helper()
194	if uassert.TypedNil(t, value, msgs...) {
195		return
196	}
197	t.FailNow()
198}
199
200// NotTypedNil requires that the value is not a typed-nil (nil pointer) value.
201func NotTypedNil(t uassert.TestingT, value any, msgs ...string) {
202	t.Helper()
203	if uassert.NotTypedNil(t, value, msgs...) {
204		return
205	}
206	t.FailNow()
207}