-
Notifications
You must be signed in to change notification settings - Fork 7
/
renderer_test.go
206 lines (180 loc) · 4.59 KB
/
renderer_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
package wikilink
import (
"bufio"
"bytes"
"errors"
"io"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/yuin/goldmark/ast"
)
func TestRenderer(t *testing.T) {
t.Parallel()
t.Run("default resolver", func(t *testing.T) {
t.Parallel()
tests := []struct {
desc string
give *Node
wantEntering string
wantExiting string
}{
{
desc: "page",
give: &Node{
Target: []byte("foo"),
},
wantEntering: `<a href="foo.html">`,
wantExiting: `</a>`,
},
{
desc: "image link",
give: &Node{
Target: []byte("foo.png"),
},
wantEntering: `<a href="foo.png">`,
wantExiting: `</a>`,
},
{
desc: "image embed",
give: &Node{
Target: []byte("foo.png"),
Embed: true,
},
wantEntering: `<img src="foo.png">`,
wantExiting: ``,
},
{
desc: "image embed url escape",
give: &Node{
Target: []byte("my cat picture 1.jpeg"),
Embed: true,
},
wantEntering: `<img src="my%20cat%20picture%201.jpeg">`,
wantExiting: ``,
},
{
desc: "pdf link",
give: &Node{
Target: []byte("foo.pdf"),
},
wantEntering: `<a href="foo.pdf">`,
wantExiting: `</a>`,
},
{
desc: "pdf embed", // unsupported at this time
give: &Node{
Target: []byte("foo.pdf"),
Embed: true,
},
wantEntering: `<a href="foo.pdf">`,
wantExiting: `</a>`,
},
{
desc: "page fragment",
give: &Node{
Target: []byte("foo"),
Fragment: []byte("frag"),
},
wantEntering: `<a href="foo.html#frag">`,
wantExiting: `</a>`,
},
{
desc: "page fragment embed", // unsupported at this time
give: &Node{
Target: []byte("foo"),
Fragment: []byte("frag"),
Embed: true,
},
wantEntering: `<a href="foo.html#frag">`,
wantExiting: `</a>`,
},
}
for _, tt := range tests {
t.Run(tt.desc, func(t *testing.T) {
var (
r Renderer
buff bytes.Buffer
)
w := bufio.NewWriter(&buff)
_, err := r.Render(w, nil /* source */, tt.give, true /* entering */)
require.NoError(t, err, "should not fail")
require.NoError(t, w.Flush(), "flush")
assert.Equal(t, tt.wantEntering, buff.String(), "output mismatch")
buff.Reset()
_, err = r.Render(w, nil /* source */, tt.give, false /* exiting */)
require.NoError(t, err, "should not fail")
require.NoError(t, w.Flush(), "flush")
assert.Equal(t, tt.wantExiting, buff.String(), "output mismatch")
})
}
})
t.Run("custom resolver", func(t *testing.T) {
t.Parallel()
var (
buff bytes.Buffer
w = bufio.NewWriter(&buff)
resolved bool
)
defer func() {
assert.True(t, resolved, "custom resolver was never invoked")
}()
n := &Node{Target: []byte("foo")}
r := Renderer{
Resolver: resolverFunc(func(n *Node) ([]byte, error) {
assert.False(t, resolved, "resolver invoked too many times")
resolved = true
assert.Equal(t, "foo", string(n.Target), "target mismatch")
return []byte("bar.html"), nil
}),
}
_, err := r.Render(w, nil /* source */, n, true /* entering */)
require.NoError(t, err, "should not fail")
require.NoError(t, w.Flush(), "flush")
assert.Equal(t, `<a href="bar.html">`, buff.String(),
"output mismatch")
})
t.Run("no link", func(t *testing.T) {
t.Parallel()
var (
buff bytes.Buffer
w = bufio.NewWriter(&buff)
)
n := &Node{Target: []byte("foo")}
r := Renderer{
Resolver: resolverFunc(noopResolver),
}
_, err := r.Render(w, nil /* source */, n, true /* entering */)
require.NoError(t, err, "should not fail")
_, err = r.Render(w, nil /* source */, n, false /* entering */)
require.NoError(t, err, "should not fail")
require.NoError(t, w.Flush(), "flush")
assert.Empty(t, buff.String(), "output should be empty")
})
}
func TestRenderer_IncorrectNode(t *testing.T) {
t.Parallel()
var r Renderer
_, err := r.Render(bufio.NewWriter(io.Discard), nil /* src */, ast.NewText(), true /* enter */)
require.Error(t, err, "render with incorrect node must fail")
assert.Contains(t, err.Error(), "unexpected node")
}
func TestRenderer_ResolveError(t *testing.T) {
t.Parallel()
r := Renderer{
Resolver: resolverFunc(func(*Node) ([]byte, error) {
return nil, errors.New("great sadness")
}),
}
_, err := r.Render(
bufio.NewWriter(io.Discard),
nil, // source
&Node{Target: []byte("foo")},
true, // entering
)
require.Error(t, err, "render with incorrect node must fail")
assert.Contains(t, err.Error(), "great sadness")
}
func noopResolver(*Node) ([]byte, error) {
return nil, nil
}