-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgolden_test.go
More file actions
80 lines (68 loc) · 1.79 KB
/
golden_test.go
File metadata and controls
80 lines (68 loc) · 1.79 KB
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
package recipemd
import (
"bytes"
"encoding/json"
"os"
"path/filepath"
"strings"
"testing"
)
func TestGolden(t *testing.T) {
suites := []struct {
name string
dir string
opts []Option
}{
{"default", "testdata/golden", nil},
{"gfm", "testdata/golden/gfm", []Option{WithGithubFormattedMarkdown()}},
{"frontmatter", "testdata/golden/frontmatter", []Option{WithFrontmatter()}},
}
for _, suite := range suites {
t.Run(suite.name, func(t *testing.T) {
files, err := filepath.Glob(filepath.Join(suite.dir, "*.md"))
if err != nil {
t.Fatal(err)
}
if len(files) == 0 {
t.Fatalf("no test files in %s", suite.dir)
}
for _, mdFile := range files {
name := strings.TrimSuffix(filepath.Base(mdFile), ".md")
isInvalid := strings.HasSuffix(name, ".invalid")
t.Run(name, func(t *testing.T) {
input, err := os.ReadFile(mdFile)
if err != nil {
t.Fatal(err)
}
recipe, parseErr := NewParser(suite.opts...).Parse(bytes.NewReader(input))
if isInvalid {
if parseErr == nil {
t.Errorf("expected parse error for invalid case")
}
return
}
if parseErr != nil {
t.Fatalf("Parse error: %v", parseErr)
}
got, err := json.MarshalIndent(recipe, "", " ")
if err != nil {
t.Fatal(err)
}
jsonFile := strings.TrimSuffix(mdFile, ".md") + ".json"
expected, err := os.ReadFile(jsonFile)
if err != nil {
t.Fatal(err)
}
var expectedMap, gotMap map[string]any
json.Unmarshal(expected, &expectedMap)
json.Unmarshal(got, &gotMap)
expectedNorm, _ := json.Marshal(expectedMap)
gotNorm, _ := json.Marshal(gotMap)
if string(expectedNorm) != string(gotNorm) {
t.Errorf("mismatch\nexpected:\n%s\ngot:\n%s", expected, got)
}
})
}
})
}
}