-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmarkdown_test.go
More file actions
91 lines (84 loc) · 2.35 KB
/
Copy pathmarkdown_test.go
File metadata and controls
91 lines (84 loc) · 2.35 KB
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
package main
import (
"strings"
"testing"
)
func TestEscText(t *testing.T) {
cases := []struct{ in, want string }{
{"a & b", "a & b"},
{"<tag>", "<tag>"},
{"plain", "plain"},
// escText must NOT escape double quotes (they stay raw in body content).
{`say "hi"`, `say "hi"`},
{"a & <b> & c", "a & <b> & c"},
}
for _, c := range cases {
if got := escText(c.in); got != c.want {
t.Errorf("escText(%q) = %q, want %q", c.in, got, c.want)
}
}
}
func TestInline(t *testing.T) {
cases := []struct{ in, want string }{
{"**bold**", "<strong>bold</strong>"},
{"a `code` b", "a <code>code</code> b"},
{"**b** and `c`", "<strong>b</strong> and <code>c</code>"},
// escaping happens before markdown substitution
{"**<x>**", "<strong><x></strong>"},
{"a & b", "a & b"},
}
for _, c := range cases {
if got := inline(c.in); got != c.want {
t.Errorf("inline(%q) = %q, want %q", c.in, got, c.want)
}
}
}
func TestRenderBody(t *testing.T) {
body := "# Dropped Title\n" +
"Intro line one.\n" +
"line two.\n" +
"\n" +
"## Section\n" +
"- item a\n" +
"- item b\n" +
"\n" +
"Final **para** with `code`."
got := renderBody(body)
// The body H1 must be dropped (title comes from frontmatter).
if strings.Contains(got, "Dropped Title") {
t.Errorf("renderBody kept body H1; got:\n%s", got)
}
want := strings.Join([]string{
" <p>Intro line one. line two.</p>",
" <h2>Section</h2>",
" <ul>",
" <li>item a</li>",
" <li>item b</li>",
" </ul>",
" <p>Final <strong>para</strong> with <code>code</code>.</p>",
}, "\n")
if got != want {
t.Errorf("renderBody mismatch.\n got:\n%s\nwant:\n%s", got, want)
}
}
func TestRenderBodyIndentation(t *testing.T) {
// Paragraphs indent 6 spaces, list items 8 — matches the reference pages.
got := renderBody("para\n\n- one")
for _, line := range strings.Split(got, "\n") {
switch {
case strings.Contains(line, "<li>"):
if !strings.HasPrefix(line, " <li>") {
t.Errorf("list item not indented 8 spaces: %q", line)
}
case strings.Contains(line, "<p>"):
if !strings.HasPrefix(line, " <p>") {
t.Errorf("paragraph not indented 6 spaces: %q", line)
}
}
}
}
func TestRenderBodyEmpty(t *testing.T) {
if got := renderBody(""); got != "" {
t.Errorf("renderBody(\"\") = %q, want empty", got)
}
}