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.

blog_test.gno

1.69 Kb · 52 lines
 1package blog
 2
 3import (
 4	"strings"
 5	"testing"
 6
 7	"gno.land/p/nt/testutils/v0"
 8	"gno.land/p/nt/uassert/v0"
 9)
10
11func TestPublishAndRender(cur realm, t *testing.T) {
12	alice := testutils.TestAddress("alice")
13	bob := testutils.TestAddress("bob")
14
15	testing.SetRealm(testing.NewUserRealm(alice))
16	id0 := Publish(cross(cur), "First Post", "Hello from alice.")
17	uassert.Equal(t, 0, id0)
18
19	testing.SetRealm(testing.NewUserRealm(bob))
20	id1 := Publish(cross(cur), "Second Post", "A longer body\nwith a newline from bob.")
21	uassert.Equal(t, 1, id1)
22
23	uassert.Equal(t, 2, PostCount())
24
25	// Root render lists both, newest (bob's) first.
26	list := Render("")
27	uassert.True(t, strings.Contains(list, "First Post"), "list has first post")
28	uassert.True(t, strings.Contains(list, "Second Post"), "list has second post")
29	uassert.True(t, strings.Index(list, "Second Post") < strings.Index(list, "First Post"),
30		"newest post appears first")
31	uassert.True(t, strings.Contains(list, alice.String()), "list shows alice author")
32
33	// Single-post render shows full body + back link.
34	single := Render("/1")
35	uassert.True(t, strings.Contains(single, "newline from bob"), "full body shown")
36	uassert.True(t, strings.Contains(single, "← back"), "back link present")
37
38	// Unknown id.
39	uassert.True(t, strings.Contains(Render("/99"), "Not found"), "missing post handled")
40}
41
42func TestPublishEmptyTitleAborts(cur realm, t *testing.T) {
43	carol := testutils.TestAddress("carol")
44	testing.SetRealm(testing.NewUserRealm(carol))
45
46	uassert.AbortsWithMessage(t, cur, "blog: title must not be empty", func() {
47		Publish(cross(cur), "   ", "some body")
48	})
49	uassert.AbortsWithMessage(t, cur, "blog: body must not be empty", func() {
50		Publish(cross(cur), "a title", "")
51	})
52}