Search Apps Documentation Source Content File Folder Download Copy Actions Download

comment_test.gno

2.97 Kb · 115 lines
  1package hub_test
  2
  3import (
  4	"testing"
  5
  6	"gno.land/p/gnoland/boards"
  7	"gno.land/p/nt/urequire/v0"
  8
  9	"gno.land/r/gnoland/boards2/v1/hub"
 10)
 11
 12func TestCommentIterateFlags(t *testing.T) {
 13	tests := []struct {
 14		name  string
 15		flags []boards.Flag
 16	}{
 17		{
 18			name: "no flags",
 19		},
 20		{
 21			name: "one flag",
 22			flags: []boards.Flag{
 23				{User: "g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5", Reason: "Reason 1"},
 24			},
 25		},
 26		{
 27			name: "multiple flags",
 28			flags: []boards.Flag{
 29				{User: "g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5", Reason: "Reason 1"},
 30				{User: "g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh", Reason: "Reason 2"},
 31				{User: "g1us8428u2a5satrlxzagqqa5m6vmuze025anjlj", Reason: "Reason 3"},
 32			},
 33		},
 34	}
 35
 36	for _, tt := range tests {
 37		t.Run(tt.name, func(t *testing.T) {
 38			var i int
 39			ref := createComment(t, "g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5", "Comment")
 40			for _, f := range tt.flags {
 41				ref.Flags.Add(f)
 42			}
 43
 44			comment := hub.NewSafeComment(ref)
 45
 46			urequire.Equal(t, len(tt.flags), comment.FlagCount, "expect number of flags to match")
 47			comment.IterateFlags(0, comment.FlagCount, func(f boards.Flag) bool {
 48				urequire.Equal(t, tt.flags[i].User, f.User, "expect user to match")
 49				urequire.Equal(t, tt.flags[i].Reason, f.Reason, "expect reason to match")
 50				i++
 51				return false
 52			})
 53		})
 54	}
 55}
 56
 57func TestCommentIterateReplies(t *testing.T) {
 58	tests := []struct {
 59		name  string
 60		setup func(*boards.Post)
 61	}{
 62		{
 63			name: "no replies",
 64		},
 65		{
 66			name: "one reply",
 67			setup: func(comment *boards.Post) {
 68				r := boards.MustNewReply(comment, "g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5", "Body 1")
 69				comment.Replies.Add(r)
 70			},
 71		},
 72		{
 73			name: "multiple replies",
 74			setup: func(comment *boards.Post) {
 75				replies := []*boards.Post{
 76					boards.MustNewReply(comment, "g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5", "Body 1"),
 77					boards.MustNewReply(comment, "g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh", "Body 2"),
 78					boards.MustNewReply(comment, "g1us8428u2a5satrlxzagqqa5m6vmuze025anjlj", "Body 3"),
 79				}
 80				for _, t := range replies {
 81					comment.Replies.Add(t)
 82				}
 83			},
 84		},
 85	}
 86
 87	for _, tt := range tests {
 88		t.Run(tt.name, func(t *testing.T) {
 89			ref := createComment(t, "g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5", "Comment")
 90			if tt.setup != nil {
 91				tt.setup(ref)
 92			}
 93
 94			comment := hub.NewSafeComment(ref)
 95
 96			urequire.Equal(t, ref.Replies.Size(), comment.ReplyCount, "expect number of replies to match")
 97			comment.IterateReplies(0, comment.ReplyCount, func(reply hub.Comment) bool {
 98				id := boards.ID(reply.ID)
 99				expected, found := ref.Replies.Get(id)
100
101				urequire.True(t, found, "expect reply to be found")
102				urequire.Equal(t, expected.Creator, reply.Creator, "expect creator to match")
103				urequire.Equal(t, expected.Body, reply.Body, "expect body to match")
104				return false
105			})
106		})
107	}
108}
109
110func createComment(t *testing.T, user address, body string) *boards.Post {
111	t.Helper()
112
113	thread := createThread(t, user, "Title", "Body")
114	return boards.MustNewReply(thread, user, body)
115}