summaryrefslogtreecommitdiff
path: root/srv/src/post/renderer_test.go
diff options
context:
space:
mode:
authorBrian Picciano <mediocregopher@gmail.com>2022-05-13 11:47:29 -0600
committerBrian Picciano <mediocregopher@gmail.com>2022-05-14 15:22:10 -0600
commit2929b4279c7a8128bd305290cc4187b6afb11cde (patch)
treed5ee6a82a21cc3c74c5f5359479b67291b9dbb27 /srv/src/post/renderer_test.go
parentd284fe2d2518c43097c0fea436d2073de14f3ada (diff)
Implement rendering Posts to html
Diffstat (limited to 'srv/src/post/renderer_test.go')
-rw-r--r--srv/src/post/renderer_test.go92
1 files changed, 92 insertions, 0 deletions
diff --git a/srv/src/post/renderer_test.go b/srv/src/post/renderer_test.go
new file mode 100644
index 0000000..5c01cd2
--- /dev/null
+++ b/srv/src/post/renderer_test.go
@@ -0,0 +1,92 @@
+package post
+
+import (
+ "bytes"
+ "strconv"
+ "strings"
+ "testing"
+ "time"
+
+ "github.com/stretchr/testify/assert"
+)
+
+func TestMarkdownBodyToHTML(t *testing.T) {
+
+ tests := []struct {
+ body string
+ exp string
+ }{
+ {
+ body: `
+# Foo
+`,
+ exp: `<h1 id="foo">Foo</h1>`,
+ },
+ {
+ body: `
+this is a body
+
+this is another
+`,
+ exp: `
+<p>this is a body</p>
+
+<p>this is another</p>`,
+ },
+ {
+ body: `this is a [link](somewhere.html)`,
+ exp: `<p>this is a <a href="somewhere.html" target="_blank">link</a></p>`,
+ },
+ }
+
+ for i, test := range tests {
+ t.Run(strconv.Itoa(i), func(t *testing.T) {
+
+ outB := mdBodyToHTML([]byte(test.body))
+ out := string(outB)
+
+ // just to make the tests nicer
+ out = strings.TrimSpace(out)
+ test.exp = strings.TrimSpace(test.exp)
+
+ assert.Equal(t, test.exp, out)
+ })
+ }
+}
+
+func TestMarkdownToHTMLRenderer(t *testing.T) {
+
+ r := NewMarkdownToHTMLRenderer()
+
+ post := RenderablePost{
+ StoredPost: StoredPost{
+ Post: Post{
+ ID: "foo",
+ Title: "Foo",
+ Description: "Bar.",
+ Body: "This is the body.",
+ Series: "baz",
+ },
+ PublishedAt: time.Now(),
+ },
+
+ SeriesPrevious: &StoredPost{
+ Post: Post{
+ ID: "foo-prev",
+ Title: "Foo Prev",
+ },
+ },
+
+ SeriesNext: &StoredPost{
+ Post: Post{
+ ID: "foo-next",
+ Title: "Foo Next",
+ },
+ },
+ }
+
+ buf := new(bytes.Buffer)
+ err := r.Render(buf, post)
+ assert.NoError(t, err)
+ t.Log(buf.String())
+}