-
Notifications
You must be signed in to change notification settings - Fork 45
/
mmark_test.go
87 lines (76 loc) · 2.07 KB
/
mmark_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
package main
import (
"bytes"
"io/ioutil"
"path/filepath"
"strings"
"testing"
"github.com/gomarkdown/markdown"
"github.com/gomarkdown/markdown/parser"
"github.com/mmarkdown/mmark/v2/lang"
"github.com/mmarkdown/mmark/v2/mparser"
"github.com/mmarkdown/mmark/v2/render/xml"
)
func TestMmarkXML(t *testing.T) {
dir := "testdata"
testFiles, err := ioutil.ReadDir(dir)
if err != nil {
t.Fatalf("could not read %s: %q", dir, err)
}
for _, f := range testFiles {
if f.IsDir() {
continue
}
if filepath.Ext(f.Name()) != ".md" {
continue
}
base := f.Name()[:len(f.Name())-3]
opts := xml.RendererOptions{
Flags: xml.CommonFlags | xml.XMLFragment,
Comments: [][]byte{[]byte("//"), []byte("#")},
}
// if the file name has a prefix ending in a underscore that prefix is taken is the language
// for this particular file and used.
// except for `u_` then this is a cue to enable xml.AllowUnicode.
us := strings.Index(f.Name(), "_")
l := "en"
if us >= 0 {
lang := f.Name()[:us]
switch lang {
case "u":
opts.Flags |= xml.AllowUnicode
default:
l = f.Name()[:us]
}
}
opts.Language = lang.New(l)
renderer := xml.NewRenderer(opts)
doTest(t, dir, base, renderer)
}
}
func doTest(t *testing.T, dir, basename string, renderer markdown.Renderer) {
filename := filepath.Join(dir, basename+".md")
input, err := ioutil.ReadFile(filename)
if err != nil {
t.Errorf("couldn't open '%s', error: %v\n", filename, err)
return
}
filename = filepath.Join(dir, basename+".xml")
expected, err := ioutil.ReadFile(filename)
if err != nil {
t.Errorf("couldn't open '%s', error: %v\n", filename, err)
}
expected = bytes.TrimSpace(expected)
p := parser.NewWithExtensions(mparser.Extensions)
init := mparser.NewInitial(filename)
p.Opts = parser.Options{
ParserHook: mparser.TitleHook,
ReadIncludeFn: init.ReadInclude,
}
actual := markdown.ToHTML(input, p, renderer)
actual = bytes.TrimSpace(actual)
if bytes.Compare(actual, expected) != 0 {
t.Errorf("\n [%#v]\nExpected[%s]\nActual [%s]",
basename+".md", expected, actual)
}
}