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

Community realm. Read the code first. Community realm, deployed by its author. Read the code before you interact or send coins.

splitter_test.gno

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