posts_test.gno
1.91 Kb ยท 64 lines
1package minisocial
2
3import (
4 "std"
5 "strings"
6 "testing"
7
8 "gno.land/p/demo/testutils" // Provides testing utilities
9 "gno.land/p/demo/uassert"
10)
11
12func TestCreatePostSingle(cur realm, t *testing.T) {
13 // Get a test address for alice
14 aliceAddr := testutils.TestAddress("alice")
15 // TestSetRealm sets the realm caller, in this case Alice
16 testing.SetRealm(std.NewUserRealm(aliceAddr))
17
18 text1 := "Hello World!"
19 err := CreatePost(cross, text1)
20 uassert.True(t, err == nil, "expected no error")
21
22 // Get the rendered page
23 got := Render("")
24
25 // Content should have the text and alice's address in it
26 condition := strings.Contains(got, text1) && strings.Contains(got, aliceAddr.String())
27 uassert.True(t, condition, "expected render to contain text & alice's address")
28}
29
30func TestCreatePostMultiple(cur realm, t *testing.T) {
31 testing.SetRealm(std.NewUserRealm(testutils.TestAddress("alice")))
32
33 // Initialize a slice to hold the test posts and their authors
34 posts := []struct {
35 text string
36 author string
37 }{
38 {"Hello World!", "alice"},
39 {"This is some new text!", "bob"},
40 {"Another post by alice", "alice"},
41 {"A post by charlie!", "charlie"},
42 }
43
44 for _, p := range posts {
45 // Set the appropriate caller realm based on the author
46 authorAddr := testutils.TestAddress(p.author)
47 testing.SetRealm(std.NewUserRealm(authorAddr))
48
49 // Create the post
50 err := CreatePost(cross, p.text)
51 uassert.True(t, err == nil, "expected no error for post "+p.text)
52 }
53
54 // Get the rendered page
55 got := Render("")
56
57 // Check that all posts and their authors are present in the rendered output
58 for _, p := range posts {
59 expectedText := p.text
60 expectedAuthor := testutils.TestAddress(p.author).String() // Get the address for the author
61 condition := strings.Contains(got, expectedText) && strings.Contains(got, expectedAuthor)
62 uassert.True(t, condition, "expected render to contain text '"+expectedText+"' and address '"+expectedAuthor+"'")
63 }
64}