urequire.gno

3.18 Kb ยท 133 lines
  1// urequire is a sister package for uassert.
  2// XXX: codegen the package.
  3package urequire
  4
  5import "gno.land/p/demo/uassert"
  6
  7// type TestingT = uassert.TestingT // XXX: bug, should work
  8
  9func NoError(t uassert.TestingT, err error, msgs ...string) {
 10	t.Helper()
 11	if uassert.NoError(t, err, msgs...) {
 12		return
 13	}
 14	t.FailNow()
 15}
 16
 17func Error(t uassert.TestingT, err error, msgs ...string) {
 18	t.Helper()
 19	if uassert.Error(t, err, msgs...) {
 20		return
 21	}
 22	t.FailNow()
 23}
 24
 25func ErrorContains(t uassert.TestingT, err error, contains string, msgs ...string) {
 26	t.Helper()
 27	if uassert.ErrorContains(t, err, contains, msgs...) {
 28		return
 29	}
 30	t.FailNow()
 31}
 32
 33func True(t uassert.TestingT, value bool, msgs ...string) {
 34	t.Helper()
 35	if uassert.True(t, value, msgs...) {
 36		return
 37	}
 38	t.FailNow()
 39}
 40
 41func False(t uassert.TestingT, value bool, msgs ...string) {
 42	t.Helper()
 43	if uassert.False(t, value, msgs...) {
 44		return
 45	}
 46	t.FailNow()
 47}
 48
 49func ErrorIs(t uassert.TestingT, err, target error, msgs ...string) {
 50	t.Helper()
 51	if uassert.ErrorIs(t, err, target, msgs...) {
 52		return
 53	}
 54	t.FailNow()
 55}
 56
 57// AbortsWithMessage requires that the code inside the specified func aborts
 58// (panics when crossing another realm).
 59// Use PanicsWithMessage for requiring local panics within the same realm.
 60// Note: This relies on gno's `revive` mechanism to catch aborts.
 61func AbortsWithMessage(t uassert.TestingT, msg string, f any, msgs ...string) {
 62	t.Helper()
 63	if uassert.AbortsWithMessage(t, msg, f, msgs...) {
 64		return
 65	}
 66	t.FailNow()
 67}
 68
 69// NotAborts requires that the code inside the specified func does NOT abort
 70// when crossing an execution boundary (e.g., VM call).
 71// Use NotPanics for requiring the absence of local panics within the same realm.
 72// Note: This relies on Gno's `revive` mechanism.
 73func NotAborts(t uassert.TestingT, f any, msgs ...string) {
 74	t.Helper()
 75	if uassert.NotPanics(t, f, msgs...) {
 76		return
 77	}
 78	t.FailNow()
 79}
 80
 81// PanicsWithMessage requires that the code inside the specified func panics
 82// locally within the same execution realm.
 83// Use AbortsWithMessage for requiring panics that cross execution boundaries (aborts).
 84func PanicsWithMessage(t uassert.TestingT, msg string, f any, msgs ...string) {
 85	t.Helper()
 86	if uassert.PanicsWithMessage(t, msg, f, msgs...) {
 87		return
 88	}
 89	t.FailNow()
 90}
 91
 92// NotPanics requires that the code inside the specified func does NOT panic
 93// locally within the same execution realm.
 94// Use NotAborts for requiring the absence of panics that cross execution boundaries (aborts).
 95func NotPanics(t uassert.TestingT, f any, msgs ...string) {
 96	t.Helper()
 97	if uassert.NotPanics(t, f, msgs...) {
 98		return
 99	}
100	t.FailNow()
101}
102
103func Equal(t uassert.TestingT, expected, actual any, msgs ...string) {
104	t.Helper()
105	if uassert.Equal(t, expected, actual, msgs...) {
106		return
107	}
108	t.FailNow()
109}
110
111func NotEqual(t uassert.TestingT, expected, actual any, msgs ...string) {
112	t.Helper()
113	if uassert.NotEqual(t, expected, actual, msgs...) {
114		return
115	}
116	t.FailNow()
117}
118
119func Empty(t uassert.TestingT, obj any, msgs ...string) {
120	t.Helper()
121	if uassert.Empty(t, obj, msgs...) {
122		return
123	}
124	t.FailNow()
125}
126
127func NotEmpty(t uassert.TestingT, obj any, msgs ...string) {
128	t.Helper()
129	if uassert.NotEmpty(t, obj, msgs...) {
130		return
131	}
132	t.FailNow()
133}