summaryrefslogtreecommitdiff
path: root/srv/src/post/renderer_test.go
blob: 5c01cd2b4d94e7f5945e4cb436796cb8979d2c68 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
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())
}