github.com/gnolang/gno@v0.0.0-20240520182011-228e9d0192ce/examples/gno.land/r/gnoland/blog/gnoblog_test.gno (about) 1 package gnoblog 2 3 import ( 4 "std" 5 "strings" 6 "testing" 7 ) 8 9 func TestPackage(t *testing.T) { 10 std.TestSetOrigCaller(std.Address("g1u7y667z64x2h7vc6fmpcprgey4ck233jaww9zq")) 11 12 author := std.GetOrigCaller() 13 14 // by default, no posts. 15 { 16 got := Render("") 17 expected := ` 18 # Gnoland's Blog 19 20 No 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 # Gnoland'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 # title2 51 52 body2 53 54 --- 55 56 Tags: [#tag1](/r/gnoland/blog:t/tag1) [#tag3](/r/gnoland/blog:t/tag3) 57 58 Written by moul on 20 May 2022 59 60 Published by g1u7y667z64x2h7vc6fmpcprgey4ck233jaww9zq to Gnoland's Blog 61 62 --- 63 <details><summary>Comment section</summary> 64 65 </details> 66 67 ` 68 assertMDEquals(t, got, expected) 69 } 70 71 // list by tags. 72 { 73 got := Render("t/invalid") 74 expected := "# [Gnoland's Blog](/r/gnoland/blog:) / t / invalid\n\nNo posts." 75 assertMDEquals(t, got, expected) 76 77 got = Render("t/tag2") 78 expected = ` 79 # [Gnoland'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 := ` 99 # title2 100 101 body2 102 103 --- 104 105 Tags: [#tag1](/r/gnoland/blog:t/tag1) [#tag3](/r/gnoland/blog:t/tag3) 106 107 Written by moul on 20 May 2022 108 109 Published by g1u7y667z64x2h7vc6fmpcprgey4ck233jaww9zq to Gnoland's Blog 110 111 --- 112 <details><summary>Comment section</summary> 113 114 <h5>comment4 115 116 </h5><h6>by g1u7y667z64x2h7vc6fmpcprgey4ck233jaww9zq on 13 Feb 09 23:31 UTC</h6> 117 118 --- 119 120 <h5>comment2 121 122 </h5><h6>by g1u7y667z64x2h7vc6fmpcprgey4ck233jaww9zq on 13 Feb 09 23:31 UTC</h6> 123 124 --- 125 126 </details> 127 ` 128 assertMDEquals(t, got, expected) 129 } 130 131 // edit post. 132 { 133 ModEditPost("slug2", "title2++", "body2++", "2009-11-10T23:00:00Z", "manfred", "tag1,tag4") 134 got := Render("p/slug2") 135 expected := ` 136 # title2++ 137 138 body2++ 139 140 --- 141 142 Tags: [#tag1](/r/gnoland/blog:t/tag1) [#tag4](/r/gnoland/blog:t/tag4) 143 144 Written by manfred on 10 Nov 2009 145 146 Published by g1u7y667z64x2h7vc6fmpcprgey4ck233jaww9zq to Gnoland's Blog 147 148 --- 149 <details><summary>Comment section</summary> 150 151 <h5>comment4 152 153 </h5><h6>by g1u7y667z64x2h7vc6fmpcprgey4ck233jaww9zq on 13 Feb 09 23:31 UTC</h6> 154 155 --- 156 157 <h5>comment2 158 159 </h5><h6>by g1u7y667z64x2h7vc6fmpcprgey4ck233jaww9zq on 13 Feb 09 23:31 UTC</h6> 160 161 --- 162 163 </details> 164 ` 165 assertMDEquals(t, got, expected) 166 } 167 168 { // Test remove functionality 169 ModAddPost("testSlug1", "testTitle1", "body1", "2022-05-22T13:17:22Z", "moul", "tag1,tag2") 170 171 got := Render("p/testSlug1") 172 173 if got == "404" { 174 t.Errorf("did not expect 404") 175 } 176 177 ModRemovePost("testSlug1") 178 got = Render("p/testSlug1") 179 180 assertMDEquals(t, got, "404") 181 } 182 183 // TODO: pagination. 184 // TODO: ?format=... 185 186 // all 404s 187 { 188 notFoundPaths := []string{ 189 "p/slug3", 190 "p", 191 "p/", 192 "x/x", 193 "t", 194 "t/", 195 "/", 196 "p/slug1/", 197 } 198 for _, notFoundPath := range notFoundPaths { 199 got := Render(notFoundPath) 200 expected := "404" 201 if got != expected { 202 t.Errorf("path %q: expected %q, got %q.", notFoundPath, expected, got) 203 } 204 } 205 } 206 } 207 208 func assertMDEquals(t *testing.T, got, expected string) { 209 t.Helper() 210 expected = strings.TrimSpace(expected) 211 got = strings.TrimSpace(got) 212 if expected != got { 213 t.Errorf("invalid render output.\nexpected %q.\ngot %q.", expected, got) 214 } 215 }