guestbook_test.gno
2.18 Kb · 98 lines
1package guestbook
2
3import (
4 "strings"
5 "testing"
6
7 "gno.land/p/nt/avl/v0"
8)
9
10// resetState clears package globals between tests (globals persist across
11// test functions in the same package binary).
12func resetState() {
13 entries = avl.NewTree()
14 count = 0
15}
16
17func TestAddEntryAppendsAndCounts(t *testing.T) {
18 resetState()
19
20 addEntry("g1alice", "hello world", 100)
21 addEntry("g1bob", "second entry", 101)
22
23 if got := Count(); got != 2 {
24 t.Fatalf("Count() = %d, want 2", got)
25 }
26
27 out := Render("")
28 if !strings.Contains(out, "**Total signatures:** 2") {
29 t.Errorf("Render missing total; got:\n%s", out)
30 }
31 if !strings.Contains(out, "hello world") {
32 t.Errorf("Render missing first message; got:\n%s", out)
33 }
34 if !strings.Contains(out, "second entry") {
35 t.Errorf("Render missing second message; got:\n%s", out)
36 }
37 if !strings.Contains(out, "100") || !strings.Contains(out, "101") {
38 t.Errorf("Render missing heights; got:\n%s", out)
39 }
40}
41
42func TestRenderNewestFirst(t *testing.T) {
43 resetState()
44
45 addEntry("g1alice", "AAA-oldest", 1)
46 addEntry("g1alice", "BBB-newest", 2)
47
48 out := Render("")
49 iNew := strings.Index(out, "BBB-newest")
50 iOld := strings.Index(out, "AAA-oldest")
51 if iNew < 0 || iOld < 0 {
52 t.Fatalf("both messages must appear; got:\n%s", out)
53 }
54 if iNew > iOld {
55 t.Errorf("newest entry should render before oldest; got:\n%s", out)
56 }
57}
58
59func TestRenderEmpty(t *testing.T) {
60 resetState()
61
62 out := Render("")
63 if !strings.Contains(out, "No one has signed yet") {
64 t.Errorf("empty render should invite signing; got:\n%s", out)
65 }
66}
67
68func TestEmptyMessagePanics(t *testing.T) {
69 resetState()
70
71 defer func() {
72 if r := recover(); r == nil {
73 t.Errorf("signing an empty message should panic")
74 }
75 }()
76 addEntry("g1alice", " ", 1)
77}
78
79func TestEscapeCell(t *testing.T) {
80 cases := []struct {
81 in, want string
82 }{
83 {"plain", "plain"},
84 {"a|b", "a\\|b"},
85 {"line1\nline2", "line1 line2"},
86 }
87 for _, tc := range cases {
88 if got := escapeCell(tc.in); got != tc.want {
89 t.Errorf("escapeCell(%q) = %q, want %q", tc.in, got, tc.want)
90 }
91 }
92}
93
94func TestSeqKeyOrders(t *testing.T) {
95 if a, b := seqKey(2), seqKey(10); !(a < b) {
96 t.Errorf("seqKey(2)=%q should sort before seqKey(10)=%q", a, b)
97 }
98}