forked from abhinav/goldmark-mermaid
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathintegration_test.go
100 lines (86 loc) · 2.27 KB
/
integration_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
package mermaid_test
import (
"bytes"
"os"
"regexp"
"strings"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/yuin/goldmark"
"go.abhg.dev/goldmark/mermaid"
"gopkg.in/yaml.v3"
)
func TestIntegration_Client(t *testing.T) {
t.Parallel()
testdata, err := os.ReadFile("testdata/client.yaml")
require.NoError(t, err)
var tests []struct {
Desc string `yaml:"desc"`
NoScript bool `yaml:"noscript"`
Give string `yaml:"give"`
Want string `yaml:"want"`
}
require.NoError(t, yaml.Unmarshal(testdata, &tests))
for _, tt := range tests {
tt := tt
t.Run(tt.Desc, func(t *testing.T) {
ext := mermaid.Extender{
RenderMode: mermaid.RenderModeClient,
MermaidJS: "mermaid.js",
NoScript: tt.NoScript,
}
md := goldmark.New(goldmark.WithExtensions(&ext))
var got bytes.Buffer
require.NoError(t, md.Convert([]byte(tt.Give), &got))
assert.Equal(t,
strings.TrimSuffix(tt.Want, "\n"),
strings.TrimSuffix(got.String(), "\n"),
)
})
}
}
func TestIntegration_Server(t *testing.T) {
t.Parallel()
testdata, err := os.ReadFile("testdata/server.yaml")
require.NoError(t, err)
var tests []struct {
Desc string `yaml:"desc"`
Give string `yaml:"give"`
Want string `yaml:"want"`
}
require.NoError(t, yaml.Unmarshal(testdata, &tests))
// HACK:
// For some reason,
// mmdc generates an SVG with specific numbers in the output
// deterministically on my computer,
// and for the same diagram, also deterministically,
// it generates slightly different numbers in CI.
//
// This basically 'fixes' those in a string.
numberRe := regexp.MustCompile(`\d+(\.\d+)?`)
normalize := func(s string) string {
s = numberRe.ReplaceAllString(s, `0`)
return strings.TrimSuffix(s, "\n")
}
for _, tt := range tests {
tt := tt
t.Run(tt.Desc, func(t *testing.T) {
// 'yarn install' must already have been run.
mmdc := mermaid.CLI{
Path: "node_modules/.bin/mmdc",
}
ext := mermaid.Extender{
RenderMode: mermaid.RenderModeServer,
MMDC: &mmdc,
}
md := goldmark.New(goldmark.WithExtensions(&ext))
var got bytes.Buffer
require.NoError(t, md.Convert([]byte(tt.Give), &got))
assert.Equal(t,
normalize(tt.Want),
normalize(got.String()),
)
})
}
}