crowdfund_test.gno
2.19 Kb · 54 lines
1package crowdfund
2
3import (
4 "testing"
5
6 "gno.land/p/moul/kit/store/v0"
7 "gno.land/p/nt/uassert/v0"
8)
9
10func TestProgressBarAndPct(t *testing.T) {
11 uassert.Equal(t, "░░░░░░░░░░░░░░░░░░░░", progressBar(0, 100))
12 uassert.Equal(t, "▓▓▓▓▓▓▓▓▓▓░░░░░░░░░░", progressBar(50, 100))
13 uassert.Equal(t, "▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓", progressBar(100, 100))
14 // over-funded is capped at full width
15 uassert.Equal(t, "▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓", progressBar(250, 100))
16 // zero goal never divides by zero
17 uassert.Equal(t, "░░░░░░░░░░░░░░░░░░░░", progressBar(10, 0))
18
19 uassert.Equal(t, 0, pct(0, 100))
20 uassert.Equal(t, 25, pct(25, 100))
21 uassert.Equal(t, 100, pct(100, 100))
22 uassert.Equal(t, 100, pct(500, 100)) // capped
23 uassert.Equal(t, 0, pct(5, 0)) // no goal
24}
25
26func TestState(t *testing.T) {
27 active := &Campaign{Goal: 100, Pledged: 10, Deadline: 100}
28 uassert.Equal(t, "Active", state(active, 50))
29 uassert.Equal(t, "Active", state(active, 100)) // deadline block still active
30
31 funded := &Campaign{Goal: 100, Pledged: 150, Deadline: 100}
32 uassert.Equal(t, "Funded", state(funded, 101))
33
34 claimed := &Campaign{Goal: 100, Pledged: 150, Deadline: 100, Claimed: true}
35 uassert.Equal(t, "Funded (claimed)", state(claimed, 101))
36
37 failed := &Campaign{Goal: 100, Pledged: 50, Deadline: 100}
38 uassert.Equal(t, "Failed", state(failed, 101))
39
40 // exactly meeting the goal counts as funded
41 exact := &Campaign{Goal: 100, Pledged: 100, Deadline: 100}
42 uassert.Equal(t, "Funded", state(exact, 200))
43}
44
45// v0 asserted that its own key() padded to width 12. That rule held only below
46// the width: key(10^12) is 13 characters and sorts before key(10^12-1), so the
47// campaign list would have rendered out of order from that id on. The store key
48// has no width to outgrow.
49func TestKeyOrdering(t *testing.T) {
50 uassert.True(t, store.ID(2).Key() < store.ID(10).Key())
51 uassert.True(t, store.ID(9).Key() < store.ID(100).Key())
52 uassert.True(t, store.ID(999999999999).Key() < store.ID(1000000000000).Key(),
53 "and it keeps holding one past v0's width-12 ceiling")
54}