forked from mmarkdown/filter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
plugins_test.go
71 lines (56 loc) · 1.34 KB
/
plugins_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
package main
import (
"bytes"
"fmt"
"io/ioutil"
"path"
"path/filepath"
"testing"
"github.com/google/go-cmp/cmp"
"github.com/mmarkdown/filter/renderer"
)
func TestPlugins(t *testing.T) {
dir := "testdata"
for name, impl := range Plugins {
r := &renderer.R{}
r.RegisterPlugin(impl)
dir := path.Join(dir, name)
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]
t.Run(path.Join(dir, f.Name()), func(t *testing.T) {
if err := doTestMarkdown(dir, base, r); err != nil {
t.Errorf("failed test for %q: %s", base, err)
}
})
}
}
}
func doTestMarkdown(dir, basename string, r *renderer.R) error {
filename := filepath.Join(dir, basename+".md")
input, err := ioutil.ReadFile(filename)
if err != nil {
return err
}
filename = filepath.Join(dir, basename+".fmt")
expected, err := ioutil.ReadFile(filename)
if err != nil {
return err
}
expected = bytes.TrimSpace(expected)
actual := r.Render(input)
actual = bytes.TrimSpace(actual)
if diff := cmp.Diff(string(actual), string(expected)); diff != "" {
return fmt.Errorf("%s: differs: (-want +got)\n%s", basename+".md", diff)
}
return nil
}