-
Notifications
You must be signed in to change notification settings - Fork 0
/
markdown_test.go
82 lines (76 loc) · 2.89 KB
/
markdown_test.go
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
package tg_test
import (
"os"
"strings"
"testing"
tg "github.com/skunkie/goldmark-tg"
"github.com/skunkie/goldmark-tg/extension"
"github.com/stretchr/testify/assert"
"github.com/yuin/goldmark"
)
func readfile(t *testing.T, path string) []byte {
t.Helper()
source, err := os.ReadFile(path)
if err != nil {
t.Error(err)
}
return source
}
func TestMarkdownToHTML(t *testing.T) {
tests := []struct {
name string
md goldmark.Markdown
source []byte
want string
}{
{
name: "test 01",
md: goldmark.New(tg.Markdown()...),
source: readfile(t, "_test/markdown.md"),
want: "<b>bold text</b>\n<i>italic text</i>\n<a href=\"http://www.example.com/\">inline URL</a>\n<a href=\"tg://user?id=123456789\">inline mention of a user</a>\n<code>inline fixed-width code</code><pre><code>pre-formatted fixed-width code block\n</code></pre>\n<pre><code class=\"language-python\">pre-formatted fixed-width code block written in the Python programming language\n</code></pre>\n",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var buf strings.Builder
err := tt.md.Convert(tt.source, &buf)
if err != nil {
t.Errorf("error = %v", err)
return
}
assert.Equal(t, tt.want, buf.String())
})
}
}
func TestMarkdownV2ToHTML(t *testing.T) {
tests := []struct {
name string
md goldmark.Markdown
source []byte
want string
}{
{
name: "test 01",
md: goldmark.New(tg.MarkdownV2()...),
source: readfile(t, "_test/markdownv2.md"),
want: "<b>bold *text</b>\n<i>italic *text</i>\n<u>underline</u>\n<s>strikethrough</s>\n<tg-spoiler>spoiler</tg-spoiler>\n<b>bold <i>italic bold <s>italic bold strikethrough <tg-spoiler>italic bold strikethrough spoiler</tg-spoiler></s> <u>underline italic bold</u></i> bold</b>\n<a href=\"http://www.example.com/\">inline URL</a>\n<a href=\"tg://user?id=123456789\">inline mention of a user</a>\n<tg-emoji emoji-id=\"5368324170671202286\">👍</tg-emoji>\n<code>inline fixed-width code</code><pre><code>pre-formatted fixed-width code block\n</code></pre>\n<pre><code class=\"language-python\">pre-formatted fixed-width code block written in the Python programming language\n</code></pre>\n<blockquote>\nBlock quotation started\nBlock quotation continued\nThe last line of the block quotation</blockquote>\n",
},
{
name: "test 02",
md: goldmark.New(append(tg.MarkdownV2(), goldmark.WithExtensions(extension.Template))...),
source: readfile(t, "_test/markdownv2_template.md"),
want: "<b>{{ if eq .Status \"firing\" }}2{{ else }}0{{ end }}</b>\n<i>T1</i> <u>{{ __T2__\n||\"T3\"|| }}</u>\n}} <tg-spoiler>"T4"</tg-spoiler> {{",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var buf strings.Builder
err := tt.md.Convert(tt.source, &buf)
if err != nil {
t.Errorf("error = %v", err)
return
}
assert.Equal(t, tt.want, buf.String())
})
}
}