-
Notifications
You must be signed in to change notification settings - Fork 4
/
extender_test.go
67 lines (58 loc) · 1.38 KB
/
extender_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
package d2
import (
"bytes"
"encoding/json"
"os"
"path/filepath"
"strings"
"testing"
"github.com/google/go-cmp/cmp"
"github.com/yuin/goldmark"
)
func TestExtenderTestExtender(t *testing.T) {
entries, err := os.ReadDir("testdata")
if err != nil {
t.Fatal(err)
}
for _, entry := range entries {
if filepath.Ext(entry.Name()) != ".md" {
continue
}
name := strings.TrimSuffix(entry.Name(), ".md")
t.Run(entry.Name(), func(t *testing.T) {
in, err := os.ReadFile(filepath.Join("testdata", entry.Name()))
if err != nil {
t.Fatal(err)
}
want, wantErr := os.ReadFile(filepath.Join("testdata", name+".html"))
if wantErr != nil {
t.Error(wantErr)
}
extender := &Extender{}
cfgfile, err := os.ReadFile(filepath.Join("testdata", name+".json"))
if !os.IsNotExist(err) {
err = json.Unmarshal(cfgfile, &extender)
if err != nil {
t.Fatal(err)
}
}
got := bytes.Buffer{}
err = goldmark.New(goldmark.WithExtensions(extender)).Convert(in, &got)
if err != nil {
t.Fatal(err)
}
if os.IsNotExist(wantErr) {
if err := os.WriteFile(
filepath.Join("testdata", name+".html"),
got.Bytes(),
0666,
); err != nil {
t.Fatal(err)
}
}
if diff := cmp.Diff(want, got.Bytes()); diff != "" {
t.Fatalf("%s:\n\nwant:\n%s\n\ngot:\n%s\n\ndiff:\n%s\n", entry.Name(), want, got.String(), diff)
}
})
}
}