-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathread_test.go
120 lines (107 loc) · 3.55 KB
/
read_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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package testmark_test
import (
"bytes"
"fmt"
"io/ioutil"
"path/filepath"
"regexp"
"testing"
"github.com/warpfork/go-testmark"
)
func TestRead(t *testing.T) {
testdata, err := filepath.Abs("testdata")
if err != nil {
t.Fatal(err)
}
doc, err := testmark.ReadFile(filepath.Join(testdata, "example.md"))
if err != nil {
t.Fatal(err)
}
if len(doc.DataHunks) != len(doc.HunksByName) {
t.Errorf("document hunk list has different length than hunks-by-name: %d != %d", len(doc.DataHunks), len(doc.HunksByName))
}
readFixturesExample(t, doc)
}
func readFixturesExample(t *testing.T, doc *testmark.Document) {
assert(t, doc.DataHunks[0].Name, "this-is-the-data-name")
assert(t, doc.DataHunks[0].LineStart+1, "13")
assert(t, doc.DataHunks[0].LineEnd+1, "17")
assert(t, doc.DataHunks[0].Body, "the content of this code block is data which can be read,\nand *replaced*, by testmark.\n")
assert(t, doc.DataHunks[1].Name, "more-data")
assert(t, doc.DataHunks[1].LineStart+1, "36")
assert(t, doc.DataHunks[1].LineEnd+1, "41")
assert(t, doc.DataHunks[1].Body, "func OtherMarkdownParsers() (shouldHighlight bool) {\n\treturn true\n}\n")
assert(t, doc.DataHunks[2].Name, "cannot-describe-no-linebreak")
assert(t, doc.DataHunks[2].LineStart+1, "70")
assert(t, doc.DataHunks[2].LineEnd+1, "73")
assert(t, doc.DataHunks[2].Body, "A markdown codeblock always has a trailing linebreak before its close indicator, you see.\n")
}
func TestParseCRLF(t *testing.T) {
input, err := ioutil.ReadFile(filepath.Join("testdata", "example.md"))
if err != nil {
t.Fatal(err)
}
input = bytes.ReplaceAll(input, []byte("\n"), []byte("\r\n"))
doc, err := testmark.Parse(input)
if err != nil {
t.Fatal(err)
}
readFixturesExample(t, doc)
}
func TestParseSimple(t *testing.T) {
buf := bytes.NewBuffer([]byte{})
fmt.Fprintln(buf, "[testmark]:# (simple)")
fmt.Fprintln(buf, "```")
fmt.Fprintln(buf, "Hello, World!")
fmt.Fprintln(buf, "```")
doc, err := testmark.Parse(buf.Bytes())
if err != nil {
t.Fatal(err)
}
hunk := doc.HunksByName["simple"]
assert(t, hunk.Body, "Hello, World!\n")
}
func TestParseTrailingWhitespace(t *testing.T) {
buf := bytes.NewBuffer([]byte{})
fmt.Fprintln(buf, "[testmark]:# (trailing/whitespace) ")
fmt.Fprintln(buf, "```")
fmt.Fprintln(buf, "foo")
fmt.Fprintln(buf, "```")
doc, err := testmark.Parse(buf.Bytes())
assert(t, err.Error(), `invalid markdown comment on line 1 (should look like "[testmark]:# (data-name-here)"; remove trailing whitespace)`)
_, exists := doc.HunksByName["trailing/whitespace"]
if exists {
t.Errorf("hunk should not exist")
}
}
func TestParseTrailingExtraLineBreak(t *testing.T) {
buf := bytes.NewBuffer([]byte("[testmark]:# (extra/newline)\n\n"))
fmt.Fprintln(buf, "```")
fmt.Fprintln(buf, "foo")
fmt.Fprintln(buf, "```")
doc, err := testmark.Parse(buf.Bytes())
assert(t, err.Error(), `invalid markdown comment on line 2. Missing code block for hunk extra/newline`)
hunk, exists := doc.HunksByName["extra/newline"]
if exists {
t.Errorf("testmark should ignore these")
}
assert(t, hunk.Body, "")
}
func TestDuplicateHunk(t *testing.T) {
testdata, err := filepath.Abs("testdata")
if err != nil {
t.Fatal(err)
}
mdPath := filepath.Join(testdata, "exampleWithDuplicateHunks.md")
_, err = testmark.ReadFile(mdPath)
assertRegex(t, err.Error(), "repeated testmark hunk name \"one/two/three\".*")
}
func assertRegex(t testing.TB, actual string, pattern string) {
matched, err := regexp.MatchString(pattern, actual)
if err != nil {
t.Fatal(err)
}
if !matched {
t.Errorf("expected %q to match pattern %q", actual, pattern)
}
}