asserts_test.gno
1.86 Kb · 73 lines
1package gns
2
3import (
4 "testing"
5)
6
7// Local assertion helpers.
8//
9// We deliberately avoid gno.land/p/nt/uassert here: when this realm is compiled
10// as an EXTERNAL package (outside the gno examples module) the working-tree copy
11// of uassert/v0 fails to preprocess with the pinned gno binary. These helpers
12// depend only on the standard testing package.
13//
14// GNS mutations PANIC with stable error values (see the error_* set) to revert
15// state, per gno semantics. A recover() inside this same realm package cannot
16// catch a crossing-boundary abort (only a pure p/ package frame can). Rejection
17// invariants are therefore unit-tested against the internal, non-crossing
18// helpers that produce the errors (authorize, priceFor, available, ...), and a
19// few end-to-end abort behaviours are covered by filetests.
20
21func eqStr(t *testing.T, want, got, ctx string) {
22 t.Helper()
23 if want != got {
24 t.Errorf("%s: want %q, got %q", ctx, want, got)
25 }
26}
27
28func eqInt(t *testing.T, want, got int64, ctx string) {
29 t.Helper()
30 if want != got {
31 t.Errorf("%s: want %d, got %d", ctx, want, got)
32 }
33}
34
35func isTrue(t *testing.T, v bool, ctx string) {
36 t.Helper()
37 if !v {
38 t.Errorf("%s: want true", ctx)
39 }
40}
41
42func isFalse(t *testing.T, v bool, ctx string) {
43 t.Helper()
44 if v {
45 t.Errorf("%s: want false", ctx)
46 }
47}
48
49func noErr(t *testing.T, err error, ctx string) {
50 t.Helper()
51 if err != nil {
52 t.Errorf("%s: unexpected error %v", ctx, err)
53 }
54}
55
56func isErr(t *testing.T, err error, ctx string) {
57 t.Helper()
58 if err == nil {
59 t.Errorf("%s: expected error, got nil", ctx)
60 }
61}
62
63// errIs asserts err matches want by stable code (Error() string).
64func errIs(t *testing.T, err, want error, ctx string) {
65 t.Helper()
66 if err == nil {
67 t.Errorf("%s: expected error %v, got nil", ctx, want)
68 return
69 }
70 if err.Error() != want.Error() {
71 t.Errorf("%s: expected %q, got %q", ctx, want.Error(), err.Error())
72 }
73}