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