Search Apps Documentation Source Content File Folder Download Copy Actions Download State String Boolean Number Struct Map Slice Pointer Function Closure Reference Nil Package Type Interface Unknown

envelope_test.gno

2.22 Kb · 70 lines
 1package envelope
 2
 3import (
 4	"chain"
 5	"testing"
 6
 7	"gno.land/p/nt/uassert/v0"
 8)
 9
10const (
11	coin  = "/gno.land/r/moul/x/moultest/v0:moultest"
12	other = "ugnot"
13)
14
15func pay(coins ...chain.Coin) {
16	testing.SetOriginSend(chain.NewCoins(coins...))
17}
18
19func TestReadsAnEmptyEnvelope(cur realm, t *testing.T) {
20	pay()
21	uassert.True(t, IsEmpty())
22	uassert.Equal(t, int64(0), Amount(coin))
23	uassert.Equal(t, 0, len(All()))
24	uassert.PanicsContains(t, cur, "must be paid", func() { Require(coin) })
25}
26
27func TestReadsAPaidEnvelope(cur realm, t *testing.T) {
28	pay(chain.NewCoin(coin, 250))
29	uassert.False(t, IsEmpty())
30	uassert.Equal(t, int64(250), Amount(coin))
31	uassert.Equal(t, int64(250), Require(coin))
32	uassert.Equal(t, int64(0), Amount(other), "a denom that did not arrive reads zero")
33	pay()
34}
35
36func TestFloorsAndPrices(cur realm, t *testing.T) {
37	pay(chain.NewCoin(coin, 100))
38
39	uassert.Equal(t, int64(100), RequireAtLeast(coin, 100), "the floor is inclusive")
40	uassert.Equal(t, int64(100), RequireAtLeast(coin, 99))
41	uassert.PanicsContains(t, cur, "costs at least 101", func() { RequireAtLeast(coin, 101) })
42
43	uassert.Equal(t, int64(100), RequireExactly(coin, 100))
44	uassert.PanicsContains(t, cur, "costs exactly 99", func() { RequireExactly(coin, 99) },
45		"being overpaid is refused too, not quietly kept")
46	uassert.PanicsContains(t, cur, "costs exactly 101", func() { RequireExactly(coin, 101) })
47	pay()
48}
49
50func TestOnlyRefusesAMixedEnvelope(cur realm, t *testing.T) {
51	pay(chain.NewCoin(coin, 100))
52	uassert.Equal(t, int64(100), Only(coin))
53
54	pay(chain.NewCoin(coin, 100), chain.NewCoin(other, 7))
55	uassert.Equal(t, int64(100), Amount(coin), "the mixed envelope still reads fine")
56	uassert.Equal(t, int64(7), Amount(other))
57	uassert.PanicsContains(t, cur, "also carried 7ugnot", func() { Only(coin) })
58
59	// Only still requires the denom it was asked about, mixed or not.
60	pay(chain.NewCoin(other, 7))
61	uassert.PanicsContains(t, cur, "must be paid", func() { Only(coin) })
62	pay()
63}
64
65func TestRequireNamesTheFlagToFix(cur realm, t *testing.T) {
66	// The abort is the realm's user interface at the moment somebody got it
67	// wrong, so it carries the flag and the denom, not a status code.
68	pay()
69	uassert.PanicsContains(t, cur, "-send <amount>"+coin, func() { Require(coin) })
70}