blog_test.gno
2.09 Kb · 58 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 // Ids start at 1 in v1: the store never assigns 0, so the zero id stays
17 // usable as "absent". v0 handed out 0 for the first post.
18 id0 := Publish(cross(cur), "First Post", "Hello from alice.")
19 uassert.Equal(t, 1, id0)
20
21 testing.SetRealm(testing.NewUserRealm(bob))
22 id1 := Publish(cross(cur), "Second Post", "A longer body\nwith a newline from bob.")
23 uassert.Equal(t, 2, id1)
24
25 uassert.Equal(t, 2, PostCount())
26
27 // Root render lists both, newest (bob's) first.
28 list := Render("")
29 uassert.True(t, strings.Contains(list, "First Post"), "list has first post")
30 uassert.True(t, strings.Contains(list, "Second Post"), "list has second post")
31 uassert.True(t, strings.Index(list, "Second Post") < strings.Index(list, "First Post"),
32 "newest post appears first")
33 uassert.True(t, strings.Contains(list, alice.String()), "list shows alice author")
34
35 // Single-post render shows full body + back link.
36 single := Render("/2")
37 uassert.True(t, strings.Contains(single, "newline from bob"), "full body shown")
38 uassert.True(t, strings.Contains(single, "← back"), "back link present")
39
40 // Unknown id.
41 uassert.True(t, strings.Contains(Render("/99"), "Not found"), "missing post handled")
42
43 // Id 0 is never assigned, so the path is rejected rather than looked up.
44 uassert.True(t, strings.Contains(Render("/0"), "Invalid post path"), "id 0 is not a post")
45 uassert.True(t, strings.Contains(Render("/abc"), "Invalid post path"), "a non-numeric path is rejected")
46}
47
48func TestPublishEmptyTitleAborts(cur realm, t *testing.T) {
49 carol := testutils.TestAddress("carol")
50 testing.SetRealm(testing.NewUserRealm(carol))
51
52 uassert.AbortsWithMessage(t, cur, "blog: title must not be empty", func() {
53 Publish(cross(cur), " ", "some body")
54 })
55 uassert.AbortsWithMessage(t, cur, "blog: body must not be empty", func() {
56 Publish(cross(cur), "a title", "")
57 })
58}