1package gnoblog
2
3import (
4 "std"
5 "strings"
6 "testing"
7)
8
9func TestPackage(t *testing.T) {
10 std.TestSetOrigCaller(std.Address("g1manfred47kzduec920z88wfr64ylksmdcedlf5"))
11
12 author := std.GetOrigCaller()
13
14 // by default, no posts.
15 {
16 got := Render("")
17 expected := `
18# gno.land's blog
19
20No posts.
21`
22 assertMDEquals(t, got, expected)
23 }
24
25 // create two posts, list post.
26 {
27 ModAddPost("slug1", "title1", "body1", "2022-05-20T13:17:22Z", "moul", "tag1,tag2")
28 ModAddPost("slug2", "title2", "body2", "2022-05-20T13:17:23Z", "moul", "tag1,tag3")
29 got := Render("")
30 expected := `
31 # gno.land's blog
32
33<div class='columns-3'><div>
34
35### [title2](/r/gnoland/blog:p/slug2)
36 20 May 2022
37</div><div>
38
39### [title1](/r/gnoland/blog:p/slug1)
40 20 May 2022
41</div></div>
42 `
43 assertMDEquals(t, got, expected)
44 }
45
46 // view post.
47 {
48 got := Render("p/slug2")
49 expected := `
50<main class='gno-tmpl-page'>
51
52# title2
53
54body2
55
56---
57
58Tags: [#tag1](/r/gnoland/blog:t/tag1) [#tag3](/r/gnoland/blog:t/tag3)
59
60Written by moul on 20 May 2022
61
62Published by g1manfred47kzduec920z88wfr64ylksmdcedlf5 to gno.land's blog
63
64---
65<details><summary>Comment section</summary>
66
67</details>
68</main>
69
70 `
71 assertMDEquals(t, got, expected)
72 }
73
74 // list by tags.
75 {
76 got := Render("t/invalid")
77 expected := "# [gno.land's blog](/r/gnoland/blog:) / t / invalid\n\nNo posts."
78 assertMDEquals(t, got, expected)
79
80 got = Render("t/tag2")
81 expected = `
82# [gno.land's blog](/r/gnoland/blog:) / t / tag2
83
84<div>
85
86### [title1](/r/gnoland/blog:p/slug1)
87 20 May 2022
88</div>
89 `
90 assertMDEquals(t, got, expected)
91 }
92
93 // add comments.
94 {
95 AddComment("slug1", "comment1")
96 AddComment("slug2", "comment2")
97 AddComment("slug1", "comment3")
98 AddComment("slug2", "comment4")
99 AddComment("slug1", "comment5")
100 got := Render("p/slug2")
101 expected := `<main class='gno-tmpl-page'>
102
103# title2
104
105body2
106
107---
108
109Tags: [#tag1](/r/gnoland/blog:t/tag1) [#tag3](/r/gnoland/blog:t/tag3)
110
111Written by moul on 20 May 2022
112
113Published by g1manfred47kzduec920z88wfr64ylksmdcedlf5 to gno.land's blog
114
115---
116<details><summary>Comment section</summary>
117
118<h5>comment4
119
120</h5><h6>by g1manfred47kzduec920z88wfr64ylksmdcedlf5 on 13 Feb 09 23:31 UTC</h6>
121
122---
123
124<h5>comment2
125
126</h5><h6>by g1manfred47kzduec920z88wfr64ylksmdcedlf5 on 13 Feb 09 23:31 UTC</h6>
127
128---
129
130</details>
131</main>
132 `
133 assertMDEquals(t, got, expected)
134 }
135
136 // edit post.
137 {
138 oldTitle := "title2"
139 oldDate := "2022-05-20T13:17:23Z"
140
141 ModEditPost("slug2", oldTitle, "body2++", oldDate, "manfred", "tag1,tag4")
142 got := Render("p/slug2")
143 expected := `<main class='gno-tmpl-page'>
144
145# title2
146
147body2++
148
149---
150
151Tags: [#tag1](/r/gnoland/blog:t/tag1) [#tag4](/r/gnoland/blog:t/tag4)
152
153Written by manfred on 20 May 2022
154
155Published by g1manfred47kzduec920z88wfr64ylksmdcedlf5 to gno.land's blog
156
157---
158<details><summary>Comment section</summary>
159
160<h5>comment4
161
162</h5><h6>by g1manfred47kzduec920z88wfr64ylksmdcedlf5 on 13 Feb 09 23:31 UTC</h6>
163
164---
165
166<h5>comment2
167
168</h5><h6>by g1manfred47kzduec920z88wfr64ylksmdcedlf5 on 13 Feb 09 23:31 UTC</h6>
169
170---
171
172</details>
173</main>
174 `
175 assertMDEquals(t, got, expected)
176
177 home := Render("")
178
179 if strings.Count(home, oldTitle) != 1 {
180 t.Errorf("post not edited properly")
181 }
182 // Edits work everything except title, slug, and publicationDate
183 // Edits to the above will cause duplication on the blog home page
184 }
185
186 { // Test remove functionality
187 title := "example title"
188 slug := "testSlug1"
189 ModAddPost(slug, title, "body1", "2022-05-25T13:17:22Z", "moul", "tag1,tag2")
190
191 got := Render("")
192
193 if !strings.Contains(got, title) {
194 t.Errorf("post was not added properly")
195 }
196
197 postRender := Render("p/" + slug)
198
199 if !strings.Contains(postRender, title) {
200 t.Errorf("post not rendered properly")
201 }
202
203 ModRemovePost(slug)
204 got = Render("")
205
206 if strings.Contains(got, title) {
207 t.Errorf("post was not removed")
208 }
209
210 postRender = Render("p/" + slug)
211
212 assertMDEquals(t, postRender, "404")
213 }
214
215 // TODO: pagination.
216 // TODO: ?format=...
217
218 // all 404s
219 {
220 notFoundPaths := []string{
221 "p/slug3",
222 "p",
223 "p/",
224 "x/x",
225 "t",
226 "t/",
227 "/",
228 "p/slug1/",
229 }
230 for _, notFoundPath := range notFoundPaths {
231 got := Render(notFoundPath)
232 expected := "404"
233 if got != expected {
234 t.Errorf("path %q: expected %q, got %q.", notFoundPath, expected, got)
235 }
236 }
237 }
238}
239
240func assertMDEquals(t *testing.T, got, expected string) {
241 t.Helper()
242 expected = strings.TrimSpace(expected)
243 got = strings.TrimSpace(got)
244 if expected != got {
245 t.Errorf("invalid render output.\nexpected %q.\ngot %q.", expected, got)
246 }
247}
gnoblog_test.gno
4.45 Kb ยท 247 lines