Skip to content Search Apps Documentation Source Content Run Reset Eval Playground 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.

budget_test.gno

2.51 Kb · 69 lines
 1package budget
 2
 3import "testing"
 4
 5func TestLedgerConservesFunds(t *testing.T) {
 6	ledger := Ledger{}.Fund(1000000).Reserve(600000).Reserve(300000)
 7	checkLedger(t, ledger, Ledger{Funded: 1000000, Available: 100000, Reserved: 900000})
 8	ledger = ledger.Approve(300000).Pay(300000).Approve(600000).Reserve(100000)
 9	checkLedger(t, ledger, Ledger{Funded: 1000000, Reserved: 100000, Claimable: 600000, Paid: 300000})
10	ledger = ledger.Release(100000).RefundAvailable()
11	checkLedger(t, ledger, Ledger{Funded: 1000000, Claimable: 700000, Paid: 300000})
12	ledger = ledger.Pay(100000).Pay(600000)
13	checkLedger(t, ledger, Ledger{Funded: 1000000, Paid: 1000000})
14}
15
16func TestPeriodAndDeadlineBoundaries(t *testing.T) {
17	if PeriodAt(119, 100, 20) != 0 || PeriodAt(120, 100, 20) != 1 || PeriodAt(140, 100, 20) != 2 {
18		t.Fatal("period must change at its first height")
19	}
20	CheckDeadline(100, 120, 20)
21	expectPanic(t, func() { CheckDeadline(100, 100, 20) })
22	expectPanic(t, func() { CheckDeadline(100, 121, 20) })
23}
24
25func TestInvalidLedgerTransitions(t *testing.T) {
26	expectPanic(t, func() { Ledger{}.Fund(0) })
27	expectPanic(t, func() { Ledger{}.Reserve(1) })
28	expectPanic(t, func() { Ledger{}.Approve(1) })
29	expectPanic(t, func() { Ledger{}.Release(1) })
30	expectPanic(t, func() { Ledger{}.Pay(1) })
31	expectPanic(t, func() { Add(maxInt64, 1) })
32	expectPanic(t, func() { Subtract(1, 2) })
33}
34
35func TestAddressAndEvidenceValidation(t *testing.T) {
36	RequireAddress(address("g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5"))
37	RequireDigest("29b3d9914b5af7272b4a89af0a9427ff6dc8ee75cf28739414fc2a46372fd38f")
38	expectPanic(t, func() { RequireAddress(address("g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf6")) })
39	expectPanic(t, func() { RequireDigest("invalid") })
40}
41
42func TestEvidenceCommitmentBindsContext(t *testing.T) {
43	proof := "29b3d9914b5af7272b4a89af0a9427ff6dc8ee75cf28739414fc2a46372fd38f"
44	a := EvidenceCommitment(1, Maintenance, 1, proof)
45	RequireDigest(a)
46	if a == EvidenceCommitment(2, Maintenance, 1, proof) ||
47		a == EvidenceCommitment(1, Reimbursements, 1, proof) ||
48		a == EvidenceCommitment(1, Maintenance, 2, proof) {
49		t.Fatal("evidence commitment did not bind the context")
50	}
51}
52
53func checkLedger(t *testing.T, actual, expected Ledger) {
54	if actual != expected {
55		t.Fatalf("ledger got %+v, want %+v", actual, expected)
56	}
57	if actual.Funded != actual.Available+actual.Reserved+actual.Claimable+actual.Paid {
58		t.Fatal("ledger does not conserve funds")
59	}
60}
61
62func expectPanic(t *testing.T, run func()) {
63	defer func() {
64		if recover() == nil {
65			t.Error("expected panic")
66		}
67	}()
68	run()
69}