contract_test.gno
1.27 Kb · 59 lines
1package importdemo
2
3import (
4 "strings"
5 "testing"
6
7 "gno.land/p/nt/uassert/v0"
8)
9
10func TestRender(t *testing.T) {
11 got := Render("")
12 expected := `
13# [thread] Hello!
14
15## Foo
16foo foo foo
17## Bar
18bar bar bar
19`
20 uassert.Equal(t, strings.TrimSpace(expected), strings.TrimSpace(got))
21}
22
23// Render must not depend on realm state: two calls have to agree, and the
24// second must not accumulate the first one's posts.
25func TestRenderIsPure(t *testing.T) {
26 uassert.Equal(t, Render(""), Render(""))
27 uassert.Equal(t, 2, strings.Count(Render(""), "## "))
28}
29
30// Posts come back in insertion order. The keys are zero-padded precisely so
31// avl's lexicographic ordering does not put "10" before "9" past nine posts.
32func TestOrderSurvivesPastNinePosts(t *testing.T) {
33 th := newThread("many")
34 for i := 0; i < 12; i++ {
35 th.addPost(ufmtIdx(i), "body")
36 }
37 out := th.String()
38 // post 9 must appear before post 10
39 if strings.Index(out, "## p9\n") > strings.Index(out, "## p10\n") {
40 t.Fatalf("insertion order lost past 9 posts:\n%s", out)
41 }
42 uassert.Equal(t, 12, strings.Count(out, "## "))
43}
44
45func ufmtIdx(i int) string {
46 return "p" + itoa(i)
47}
48
49func itoa(i int) string {
50 if i == 0 {
51 return "0"
52 }
53 var b []byte
54 for i > 0 {
55 b = append([]byte{byte('0' + i%10)}, b...)
56 i /= 10
57 }
58 return string(b)
59}