-
Notifications
You must be signed in to change notification settings - Fork 8
/
renderer_test.go
183 lines (150 loc) · 5.07 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
package bfchroma
import (
"bytes"
"fmt"
"testing"
"github.com/alecthomas/chroma/v2"
"github.com/alecthomas/chroma/v2/formatters/html"
"github.com/alecthomas/chroma/v2/styles"
bf "github.com/russross/blackfriday/v2"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestExtend(t *testing.T) {
var b bf.Renderer
var r *Renderer
b = bf.NewHTMLRenderer(bf.HTMLRendererParameters{
Flags: bf.CommonHTMLFlags,
})
r = NewRenderer(Extend(b))
assert.Equal(t, r.Base, b, "should be the same renderer")
}
func ExampleExtend() {
md := "```go\npackage main\n\nfunc main() {\n}\n```"
b := bf.NewHTMLRenderer(bf.HTMLRendererParameters{
Flags: bf.CommonHTMLFlags,
})
r := NewRenderer(Extend(b))
h := bf.Run([]byte(md), bf.WithRenderer(r))
fmt.Println(string(h))
}
func TestStyle(t *testing.T) {
var r *Renderer
for k, v := range styles.Registry {
r = NewRenderer(Style(k))
assert.Equal(t, r.Style, v, "Style should match")
}
for _, v := range []string{"♥", "inexistent", "fallback!"} {
r = NewRenderer(Style(v))
assert.Equal(t, r.Style, styles.Fallback)
}
}
func ExampleStyle() {
md := "```go\npackage main\n\nfunc main() {\n}\n```"
r := NewRenderer(Style("github"))
h := bf.Run([]byte(md), bf.WithRenderer(r))
fmt.Println(string(h))
}
func TestChromaStyle(t *testing.T) {
var r *Renderer
for _, v := range styles.Registry {
r = NewRenderer(ChromaStyle(v))
assert.Equal(t, r.Style, v, "Style should match")
}
}
func ExampleChromaStyle() {
md := "```go\npackage main\n\nfunc main() {\n}\n```"
r := NewRenderer(ChromaStyle(styles.Get("github")))
h := bf.Run([]byte(md), bf.WithRenderer(r))
fmt.Println(string(h))
}
func TestWithoutAutodetect(t *testing.T) {
r := NewRenderer(WithoutAutodetect())
assert.False(t, r.Autodetect, "Should set Autodetect to false")
r = NewRenderer()
assert.True(t, r.Autodetect, "Not using option should leave Autodetect to true")
}
func ExampleWithoutAutodetect() {
md := "```\npackage main\n\nfunc main() {\n}\n```"
r := NewRenderer(WithoutAutodetect())
h := bf.Run([]byte(md), bf.WithRenderer(r))
fmt.Println(string(h))
}
func TestChromaOptions(t *testing.T) {
NewRenderer(ChromaOptions(html.WithClasses(true)))
}
func ExampleChromaOptions() {
md := "```go\npackage main\n\nfunc main() {\n}\n```"
r := NewRenderer(ChromaOptions(html.WithLineNumbers(true)))
h := bf.Run([]byte(md), bf.WithRenderer(r))
fmt.Println(string(h))
}
func TestRenderWithChroma(t *testing.T) {
var err error
var b *bytes.Buffer
r := NewRenderer()
tests := []struct {
in []byte
cbd bf.CodeBlockData
out string
}{
{[]byte{0}, bf.CodeBlockData{}, "<pre style=\"color:#f8f8f2;background-color:#272822;\"><code><span style=\"display:flex;\"><span>\x00</span></span></code></pre>"},
{[]byte{0, 1, 2}, bf.CodeBlockData{}, "<pre style=\"color:#f8f8f2;background-color:#272822;\"><code><span style=\"display:flex;\"><span>\x00\x01\x02</span></span></code></pre>"},
{[]byte("Hello World"), bf.CodeBlockData{}, "<pre style=\"color:#f8f8f2;background-color:#272822;\"><code><span style=\"display:flex;\"><span>Hello World</span></span></code></pre>"},
}
for _, test := range tests {
b = new(bytes.Buffer)
err = r.RenderWithChroma(b, test.in, test.cbd)
assert.NoError(t, err, "Should not fail")
assert.Equal(t, test.out, b.String())
}
}
func TestRender(t *testing.T) {
md := "```go\npackage main\n\nfunc main() {\n}\n```"
r := NewRenderer()
bg := r.Style.Get(chroma.Background).Background.String()
h := bf.Run([]byte(md), bf.WithRenderer(r))
assert.Contains(t, string(h), r.Style.Get(chroma.NameFunction).Colour.String())
assert.Contains(t, string(h), bg)
assert.Contains(t, string(h), "<pre")
// Check if auto-detection works on Go example
md = "```\npackage main\n\nfunc main() {\n\tfmt.Println()\n}\n```"
h = bf.Run([]byte(md), bf.WithRenderer(r))
assert.Contains(t, string(h), r.Style.Get(chroma.NameFunction).Colour.String())
assert.Contains(t, string(h), bg)
assert.Contains(t, string(h), "<pre")
// Check if disabling the Autodetect feature works
r = NewRenderer(WithoutAutodetect())
h = bf.Run([]byte(md), bf.WithRenderer(r))
assert.NotContains(t, string(h), r.Style.Get(chroma.NameFunction).Colour.String())
assert.Contains(t, string(h), bg)
assert.Contains(t, string(h), "<pre")
}
func TestRender_EmbedCSS(t *testing.T) {
r := NewRenderer(EmbedCSS())
h := bf.Run([]byte(""), bf.WithRenderer(r))
assert.Contains(t, string(h), "<style>")
assert.Contains(t, string(h), ".chroma")
assert.Contains(t, string(h), "</style>")
}
func TestRenderer_ChromaCSS(t *testing.T) {
r := NewRenderer()
var w bytes.Buffer
err := r.ChromaCSS(&w)
require.NoError(t, err)
assert.Contains(t, w.String(), ".chroma")
}
func ExampleNewRenderer() {
// Complex example on how to initialize the renderer
md := "```go\npackage main\n\nfunc main() {\n}\n```"
r := NewRenderer(
Extend(bf.NewHTMLRenderer(bf.HTMLRendererParameters{
Flags: bf.CommonHTMLFlags,
})),
WithoutAutodetect(),
ChromaStyle(styles.Get("github")),
ChromaOptions(html.WithLineNumbers(true)),
)
h := bf.Run([]byte(md), bf.WithRenderer(r))
fmt.Println(string(h))
}