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.

zz_bounds_test.gno

1.84 Kb · 46 lines
 1package polls
 2
 3import (
 4	"strings"
 5	"testing"
 6
 7	"gno.land/p/nt/testutils/v0"
 8	"gno.land/p/nt/uassert/v0"
 9)
10
11// What a creator may store is bounded, and the root page renders a fixed
12// window of the newest polls whatever the count.
13func TestPollsAreBounded(cur realm, t *testing.T) {
14	testing.SetRealm(testing.NewUserRealm(testutils.TestAddress("bounded")))
15	uassert.AbortsContains(t, cur, "question too long", func() { CreatePoll(cross(cur), strings.Repeat("q", MaxQuestionLen+1), "a,b") })
16	uassert.AbortsContains(t, cur, "at most", func() { CreatePoll(cross(cur), "many", strings.Repeat("o,", MaxOptions)+"o") })
17	for len(polls) < IndexRecent+3 {
18		CreatePoll(cross(cur), "filler", "a,b")
19	}
20	uassert.Equal(t, IndexRecent, strings.Count(Render(""), "## Poll #"))
21}
22
23// The poll count is bounded globally.
24func TestPollCountIsBounded(cur realm, t *testing.T) {
25	before := len(polls)
26	for len(polls) < MaxPolls {
27		polls = append(polls, &Poll{ID: len(polls), Question: "q", Options: []string{"a", "b"}, Tallies: []int{0, 0}})
28	}
29	defer func() { polls = polls[:before] }()
30	testing.SetRealm(testing.NewUserRealm(testutils.TestAddress("latecomer")))
31	uassert.AbortsContains(t, cur, "polls is full", func() { CreatePoll(cross(cur), "one more", "a,b") })
32}
33
34// Polls past the newest page stay reachable: the index links to the next
35// page, and a page past the end says so instead of wrapping.
36func TestOlderPollsArePaged(cur realm, t *testing.T) {
37	testing.SetRealm(testing.NewUserRealm(testutils.TestAddress("pager")))
38	for len(polls) < IndexRecent+1 {
39		CreatePoll(cross(cur), "filler", "a,b")
40	}
41	idx := Render("")
42	uassert.True(t, strings.Contains(idx, "(/r/nym-moultest123/x/daily/polls/v1:page/2)"), idx)
43	p2 := Render("page/2")
44	uassert.True(t, strings.Contains(p2, "## Poll #0:"), p2)
45	uassert.True(t, strings.Contains(Render("page/9223372036854775807"), "No such page"))
46}