-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmarkdown_test.go
158 lines (144 loc) · 4.33 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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
package markdown
import (
"bytes"
"fmt"
"testing"
"github.com/sergi/go-diff/diffmatchpatch"
"github.com/yuin/goldmark"
"github.com/yuin/goldmark/extension"
"github.com/yuin/goldmark/renderer/html"
"github.com/yuin/goldmark/util"
"github.com/blackstork-io/goldmark-markdown/pkg/mdexamples"
)
func roundtrip(t testing.TB, source, expectedHTMLOverride []byte) {
t.Helper()
if !bytes.HasSuffix(source, []byte("\n")) {
source = append(source, '\n')
}
mdToHtml := goldmark.New(
goldmark.WithExtensions(
extension.Table,
extension.Strikethrough,
extension.TaskList,
),
goldmark.WithRendererOptions(html.WithUnsafe()),
)
mdToMd := goldmark.New(
goldmark.WithParser(mdToHtml.Parser()),
goldmark.WithExtensions(NewRenderer()),
)
expectedHtmlBuf := &bytes.Buffer{}
err := mdToHtml.Convert(source, expectedHtmlBuf)
if err != nil {
t.Fatalf("convert to html: %v", err)
}
expectedHtml := expectedHtmlBuf.Bytes()
if expectedHTMLOverride != nil {
expectedHtml = expectedHTMLOverride
}
generatedMarkdownBuf := &bytes.Buffer{}
err = mdToMd.Convert(source, generatedMarkdownBuf)
if err != nil {
t.Fatalf("convert to markdown: %v", err)
}
generatedMarkdown := generatedMarkdownBuf.Bytes()
resultingHtmlBuf := &bytes.Buffer{}
err = mdToHtml.Convert(generatedMarkdown, resultingHtmlBuf)
if err != nil {
t.Fatalf("convert generated markdown to html: %v", err)
}
resultingHtml := resultingHtmlBuf.Bytes()
if !bytes.Equal(expectedHtml, resultingHtml) {
t.Errorf("generated markdown differs")
if len(expectedHtml) < 200 {
t.Logf("---------- original -----------\n%s\n\n", util.VisualizeSpaces(source))
t.Logf("---------- roundtrip ----------\n%s\n\n", util.VisualizeSpaces(generatedMarkdown))
t.Logf("---------- original HTML-------\n%s\n\n", expectedHtml)
t.Logf("---------- roundtrip HTML------\n%s\n\n", resultingHtml)
} else {
// dmp := diffmatchpatch.New()
// diffs := dmp.DiffMain(string(util.VisualizeSpaces(source)), string(util.VisualizeSpaces(generatedMarkdown)), false)
// t.Logf("---------- md diff ------------\n%s\n\n", dmp.DiffPrettyText(diffs))
dmp := diffmatchpatch.New()
diffs := dmp.DiffMain(string(expectedHtml), string(resultingHtml), true)
t.Logf("---------- HTML diff ----------\n%s\n\n", dmp.DiffPrettyText(diffs))
}
}
}
func TestSpecExamplesRoundtrip(t *testing.T) {
for _, exFile := range mdexamples.ReadAllSpecExamples() {
for _, ex := range exFile.Examples {
t.Run(fmt.Sprintf("%s/%d", exFile.Name, ex.ID), func(t *testing.T) {
t.Parallel()
t.Logf("URL: %s", ex.Link)
roundtrip(t, ex.Markdown, nil)
})
}
}
}
func TestDocumentsRoundtrip(t *testing.T) {
for _, ex := range mdexamples.ReadAllDocumentExamples() {
t.Run(ex.Name, func(t *testing.T) {
t.Parallel()
if ex.Name == "definitions.md" {
// we don't support definitions, so definition on a new line is rendered as an empty block.
t.SkipNow()
}
if ex.Name == "text_below_table.md" {
// TestTextBelowTable takes care of this test
t.SkipNow()
}
roundtrip(t, ex.Data, nil)
})
}
}
func FuzzRenderer(f *testing.F) {
for _, exFile := range mdexamples.ReadAllSpecExamples() {
for _, ex := range exFile.Examples {
f.Add(ex.Markdown)
}
}
f.Fuzz(func(t *testing.T, data []byte) {
roundtrip(t, data, nil)
})
}
func TestFuzzFindings(t *testing.T) {
t.Skip("Known and accepted failures in empahsis parsing")
cases := [...][]byte{
[]byte("* F***00000*000*00000"), // simplified to:
[]byte("***a*b*c"),
[]byte("_______00000000000_____00_0_!_000__ 000"), // simplified to:
[]byte("___0_!_0__"),
}
for _, data := range cases {
roundtrip(t, data, nil)
}
}
func TestTextBelowTable(t *testing.T) {
// For some reason if the cell is missing it would be generated by goldmark HTML without
// alignment attribute.
// Our parser will generate an empty cell in place of the missing cell, and the alignment
// would be applied to it, so the difference is the `style="text-align:center"` in the last td.
roundtrip(
t,
mdexamples.ReadDocumentExample("text_below_table.md").Data,
[]byte(`<table>
<thead>
<tr>
<th style="text-align:left">a</th>
<th style="text-align:center">b</th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align:left">c</td>
<td style="text-align:center">d</td>
</tr>
<tr>
<td style="text-align:left">e</td>
<td style="text-align:center"></td>
</tr>
</tbody>
</table>
`))
}