-
Notifications
You must be signed in to change notification settings - Fork 0
/
mdtoc_test.go
239 lines (208 loc) · 4.63 KB
/
mdtoc_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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
package mdtoc_test
import (
"bytes"
"errors"
"io/ioutil"
"os"
"strings"
"testing"
"github.com/madlambda/mdtoc"
)
func assertNoErr(t *testing.T, err error) {
if err != nil {
t.Fatalf("unexpected err: %s", err)
}
}
func TestTOC(t *testing.T) {
testCases := []string{
"noheaders",
"removeheaders",
"empty",
"onlyspaces",
"trimheaders",
"toconly",
"atx/ignorecode",
"atx/headerfirst",
"atx/headerlast",
"atx/trimrightspace",
"atx/leftspacenotheader",
"atx/invalidheader",
"atx/onlyheader",
"atx/headerwithhash",
"atx/headerambiguity",
"atx/onlyheaderandspaces",
"atx/multiplelevels",
"atx/specialchars",
"atx/withspaces",
"atx/updateheader",
}
for _, name := range testCases {
t.Run(name, func(t *testing.T) {
input, err := ioutil.ReadFile(
"testdata/" + name + "/input.md",
)
assertNoErr(t, err)
wantRaw, err := ioutil.ReadFile(
"testdata/" + name + "/output.md",
)
assertNoErr(t, err)
want := string(wantRaw)
var output bytes.Buffer
err = mdtoc.Generate(
bytes.NewBuffer(input),
&output,
)
if err != nil {
t.Fatalf("unexpected error: %s", err)
}
got := output.String()
if want != got {
t.Fatalf(
"\nexpected:\n%q\ngot:\n%q\n\n",
want,
got,
)
}
})
t.Run(name+"/FromFile", func(t *testing.T) {
inputfilepath := "testdata/" + name + "/input.md"
wantRaw, err := ioutil.ReadFile(
"testdata/" + name + "/output.md",
)
assertNoErr(t, err)
want := string(wantRaw)
var output bytes.Buffer
err = mdtoc.GenerateFromFile(
inputfilepath,
&output,
)
assertNoErr(t, err)
got := output.String()
if want != got {
t.Fatalf(
"\nexpected:\n%q\ngot:\n%q\n\n",
want,
got,
)
}
})
t.Run(name+"/InPlace", func(t *testing.T) {
inputdata, err := ioutil.ReadFile(
"testdata/" + name + "/input.md",
)
assertNoErr(t, err)
tmpfile, err := ioutil.TempFile("", "mdtoc.inplace.test")
assertNoErr(t, err)
defer func() {
err := os.Remove(tmpfile.Name())
assertNoErr(t, err)
}()
n, err := tmpfile.Write(inputdata)
assertNoErr(t, err)
assertNoErr(t, tmpfile.Close())
if n != len(inputdata) {
t.Fatalf(
"expected to write %d but wrote %d",
n,
inputdata,
)
}
wantRaw, err := ioutil.ReadFile(
"testdata/" + name + "/output.md",
)
assertNoErr(t, err)
want := string(wantRaw)
err = mdtoc.GenerateInPlace(tmpfile.Name())
assertNoErr(t, err)
gotRaw, err := ioutil.ReadFile(tmpfile.Name())
assertNoErr(t, err)
got := string(gotRaw)
if want != got {
t.Fatalf(
"\nexpected:\n%q\ngot:\n%q\n\n",
want,
got,
)
}
})
}
}
type fakeReadWriter struct {
t *testing.T
writeCalls int
explodeSecondWrite bool
}
func (f *fakeReadWriter) Read(b []byte) (int, error) {
return 0, errors.New("injected error")
}
func (f *fakeReadWriter) Write(p []byte) (n int, err error) {
if f.writeCalls > 0 {
if f.explodeSecondWrite {
f.t.Fatal("should not call write after error")
}
}
f.writeCalls += 1
return 0, errors.New("injected error")
}
func TestInputIoError(t *testing.T) {
var output bytes.Buffer
err := mdtoc.Generate(&fakeReadWriter{}, &output)
if err == nil {
t.Fatal("expected error, got nil")
}
if output.Len() != 0 {
t.Fatalf("should have not written to output, wrote[%d]", output.Len())
}
}
func TestOnCorruptedHeaderFails(t *testing.T) {
const corruptedheader = `
<!-- mdtocstart -->
# Table of Contents
- [Header](#header)
# Header
`
input := strings.NewReader(corruptedheader)
var output bytes.Buffer
err := mdtoc.Generate(input, &output)
if err == nil {
t.Fatal("expected error, got nil")
}
if output.Len() != 0 {
t.Fatalf("expected empty output, got: %q", output)
}
}
func TestOutputIoError(t *testing.T) {
input := strings.NewReader("whatever")
err := mdtoc.Generate(input, &fakeReadWriter{})
if err == nil {
t.Fatal("expected error, got nil")
}
}
func TestStopWritingOnOutputError(t *testing.T) {
input := strings.NewReader("# Header")
err := mdtoc.Generate(input, &fakeReadWriter{
t: t,
explodeSecondWrite: true,
})
if err == nil {
t.Fatal("expected error, got nil")
}
}
func TestGenerateFromInvalidFile(t *testing.T) {
var output bytes.Buffer
err := mdtoc.GenerateFromFile(
"notvalid.haha.xt.777",
&output,
)
if err == nil {
t.Fatal("expected error, got nil")
}
}
func TestGenerateInPlaceInvalidFile(t *testing.T) {
invalidFile := "notvalid.haha.xt.666"
defer os.Remove(invalidFile)
err := mdtoc.GenerateInPlace(invalidFile)
if err == nil {
t.Fatal("expected error, got nil")
}
}