-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathprinter_test.go
120 lines (106 loc) · 2.07 KB
/
printer_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 mdtt
import (
"bytes"
"io"
"os"
"runtime"
"testing"
"github.com/google/go-cmp/cmp"
)
func TestFindSegment(t *testing.T) {
tl := tableLocator{}
md := `# Title
| foo | bar |
| --- | --- |
| baz | bim |
`
b := bytes.NewBuffer([]byte(md))
tl.findLocations(b)
got := tl.ranges[0]
want := tableRange{
Start: 9,
End: 50,
}
if runtime.GOOS == "windows" {
want = tableRange{
Start: 11,
End: 54,
}
}
if diff := cmp.Diff(want, got); diff != "" {
t.Errorf("differs: (-want +got)\n%s", diff)
}
}
func TestReplaceTable(t *testing.T) {
testCases := []struct {
name string
src string
idx int
wnt string
}{
{
name: "normal",
src: "testdata/replace01.md",
idx: 0,
wnt: "testdata/replace01_want.md",
},
{
name: "thematic break",
src: "testdata/replace02.md",
idx: 0,
wnt: "testdata/replace02_want.md",
},
{
name: "fenced code block",
src: "testdata/replace03.md",
idx: 0,
wnt: "testdata/replace03_want.md",
},
{
name: "multiple tables",
src: "testdata/replace04.md",
idx: 1,
wnt: "testdata/replace04_want.md",
},
{
name: "indented code block",
src: "testdata/replace05.md",
idx: 0,
wnt: "testdata/replace05_want.md",
},
{
name: "left/center/right aligned",
src: "testdata/replace06.md",
idx: 0,
wnt: "testdata/replace06_want.md",
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
want, got := testUtilReplaceTable(tc.src, tc.wnt, tc.idx)
if diff := cmp.Diff(string(want), string(got)); diff != "" {
t.Errorf("differs: (-want +got)\n%s", diff)
}
})
}
}
func testUtilReplaceTable(src, wnt string, idx int) ([]byte, []byte) {
tw := tableWriter{}
fpSrc, _ := os.Open(src)
defer fpSrc.Close()
fp_, _ := os.Open(wnt)
defer fp_.Close()
md, _ := io.ReadAll(fp_)
m, _ := NewUI(
WithMarkdown(md),
WithFilePath(wnt),
)
m.table = m.tables[idx]
m.choose = idx
tw.render(m.table)
got := tw.replaceTable(fpSrc, idx)
fpWnt, _ := os.Open(wnt)
defer fpWnt.Close()
want, _ := io.ReadAll(fpWnt)
return got, want
}