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

splitter_test.gno

2.21 Kb · 77 lines
 1package splitter
 2
 3import (
 4	"strings"
 5	"testing"
 6
 7	"gno.land/p/moul/kit/store/v0"
 8	"gno.land/p/nt/testutils/v0"
 9	"gno.land/p/nt/uassert/v0"
10)
11
12func reset() {
13	groups = store.Named("splitter: group")
14}
15
16func TestRegisterAndOwed(cur realm, t *testing.T) {
17	// reset package state for a deterministic run
18	reset()
19
20	owner := testutils.TestAddress("owner")
21	testing.SetRealm(testing.NewUserRealm(owner))
22
23	a := testutils.TestAddress("alice").String()
24	b := testutils.TestAddress("bob").String()
25
26	id := Register(cross(cur), a+","+b, "3,1")
27	// v1 ids start at 1: the store never assigns 0, so the zero id stays
28	// usable as "absent". v0 handed out 0 for the first group.
29	uassert.Equal(t, 1, id, "first group id should be 1")
30
31	// pool 100, shares 3:1 -> alice 75, bob 25
32	RecordIncome(cross(cur), id, 100)
33
34	out := Render("")
35	uassert.True(t, strings.Contains(out, "Group #1"), "render shows group")
36	uassert.True(t, strings.Contains(out, "| 3 | 75 |"), "alice owed 75")
37	uassert.True(t, strings.Contains(out, "| 1 | 25 |"), "bob owed 25")
38	uassert.True(t, strings.Contains(out, "Pool: 100"), "pool shown")
39}
40
41func TestRoundingRemainder(cur realm, t *testing.T) {
42	reset()
43
44	owner := testutils.TestAddress("owner2")
45	testing.SetRealm(testing.NewUserRealm(owner))
46
47	a := testutils.TestAddress("a").String()
48	b := testutils.TestAddress("b").String()
49	c := testutils.TestAddress("c").String()
50
51	id := Register(cross(cur), a+","+b+","+c, "1,1,1")
52	RecordIncome(cross(cur), id, 100) // 100/3 = 33 each, remainder 1
53
54	out := Render("")
55	uassert.True(t, strings.Contains(out, "| 1 | 33 |"), "each owed 33")
56	uassert.True(t, strings.Contains(out, "remainder"), "remainder row shown")
57	uassert.True(t, strings.Contains(out, "| 1 |\n"), "remainder value 1")
58}
59
60func TestBadInputAborts(cur realm, t *testing.T) {
61	reset()
62
63	owner := testutils.TestAddress("owner3")
64	testing.SetRealm(testing.NewUserRealm(owner))
65
66	a := testutils.TestAddress("alice").String()
67
68	// mismatched payees/shares count aborts
69	uassert.AbortsWithMessage(t, cur, "splitter: payees and shares count mismatch", func() {
70		Register(cross(cur), a, "1,2")
71	})
72
73	// non-positive share aborts
74	uassert.AbortsWithMessage(t, cur, "splitter: share must be positive", func() {
75		Register(cross(cur), a, "0")
76	})
77}