gnoblog_test.gno

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