Skip to content

Commit c271197

Browse files
committed
test: add test for custom styles
1 parent ef3b2d1 commit c271197

File tree

4 files changed

+230
-3
lines changed

4 files changed

+230
-3
lines changed

glamour.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,10 @@ const (
2828
PinkStyle = "pink"
2929
)
3030

31-
const defaultWidth = 80
32-
const highPriority = 1000
31+
const (
32+
defaultWidth = 80
33+
highPriority = 1000
34+
)
3335

3436
// A TermRendererOption sets an option on a TermRenderer.
3537
type TermRendererOption func(*TermRenderer) error

glamour_test.go

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package glamour
22

33
import (
44
"bytes"
5+
"errors"
56
"io"
67
"os"
78
"strings"
@@ -43,7 +44,7 @@ func TestTermRendererWriter(t *testing.T) {
4344

4445
// generate
4546
if generate {
46-
err := os.WriteFile(testFile, b, 0644)
47+
err := os.WriteFile(testFile, b, 0o644)
4748
if err != nil {
4849
t.Fatal(err)
4950
}
@@ -167,6 +168,46 @@ func TestStyles(t *testing.T) {
167168
}
168169
}
169170

171+
// TestCustomStyle checks the expected errors with custom styling. We need to
172+
// support built-in styles and custom style sheets.
173+
func TestCustomStyle(t *testing.T) {
174+
md := "testdata/example.md"
175+
tests := []struct {
176+
name string
177+
stylePath string
178+
err error
179+
expected string
180+
}{
181+
{name: "style exists", stylePath: "testdata/custom.style", err: nil, expected: "testdata/custom.style"},
182+
{name: "style doesn't exist", stylePath: "testdata/notfound.style", err: os.ErrNotExist, expected: AutoStyle},
183+
{name: "style is empty", stylePath: "", err: nil, expected: AutoStyle},
184+
}
185+
186+
for _, tc := range tests {
187+
t.Run(tc.name, func(t *testing.T) {
188+
t.Setenv("GLAMOUR_STYLE", tc.stylePath)
189+
g, err := NewTermRenderer(
190+
WithEnvironmentConfig(),
191+
)
192+
if !errors.Is(err, tc.err) {
193+
t.Fatal(err)
194+
}
195+
if !errors.Is(tc.err, os.ErrNotExist) {
196+
w, err := NewTermRenderer(WithStylePath(tc.expected))
197+
if err != nil {
198+
t.Fatal(err)
199+
}
200+
text, _ := os.ReadFile(md)
201+
want, err := w.RenderBytes(text)
202+
got, err := g.RenderBytes(text)
203+
if !bytes.Equal(want, got) {
204+
t.Error("Wrong style used")
205+
}
206+
}
207+
})
208+
}
209+
}
210+
170211
func TestRenderHelpers(t *testing.T) {
171212
in, err := os.ReadFile(markdown)
172213
if err != nil {

testdata/custom.style

Lines changed: 183 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,183 @@
1+
{
2+
"document": {
3+
"block_prefix": "\n",
4+
"block_suffix": "\n",
5+
"color": "252",
6+
"margin": 2
7+
},
8+
"block_quote": {
9+
"indent": 1,
10+
"indent_token": "│ "
11+
},
12+
"paragraph": {},
13+
"list": {
14+
"level_indent": 2
15+
},
16+
"heading": {
17+
"block_suffix": "\n",
18+
"color": "39",
19+
"bold": true
20+
},
21+
"h1": {
22+
"prefix": " ",
23+
"suffix": " ",
24+
"color": "228",
25+
"background_color": "63",
26+
"bold": true
27+
},
28+
"h6": {
29+
"prefix": " ",
30+
"color": "35",
31+
"bold": false
32+
},
33+
"text": {},
34+
"strikethrough": {
35+
"crossed_out": true
36+
},
37+
"emph": {
38+
"italic": true
39+
},
40+
"strong": {
41+
"bold": true
42+
},
43+
"hr": {
44+
"color": "240",
45+
"format": "\n--------\n"
46+
},
47+
"item": {
48+
"block_prefix": "• "
49+
},
50+
"enumeration": {
51+
"block_prefix": ". "
52+
},
53+
"task": {
54+
"ticked": "[✓] ",
55+
"unticked": "[ ] "
56+
},
57+
"link": {
58+
"color": "30",
59+
"underline": true
60+
},
61+
"link_text": {
62+
"color": "35",
63+
"bold": true
64+
},
65+
"image": {
66+
"color": "212",
67+
"underline": true
68+
},
69+
"image_text": {
70+
"color": "243",
71+
"format": "Image: {{.text}} →"
72+
},
73+
"code": {
74+
"prefix": " ",
75+
"suffix": " ",
76+
"color": "203",
77+
"background_color": "236"
78+
},
79+
"code_block": {
80+
"color": "244",
81+
"margin": 2,
82+
"chroma": {
83+
"text": {
84+
"color": "#C4C4C4"
85+
},
86+
"error": {
87+
"color": "#F1F1F1",
88+
"background_color": "#F05B5B"
89+
},
90+
"comment": {
91+
"color": "#676767"
92+
},
93+
"comment_preproc": {
94+
"color": "#FF875F"
95+
},
96+
"keyword": {
97+
"color": "#00AAFF"
98+
},
99+
"keyword_reserved": {
100+
"color": "#FF5FD2"
101+
},
102+
"keyword_namespace": {
103+
"color": "#FF5F87"
104+
},
105+
"keyword_type": {
106+
"color": "#6E6ED8"
107+
},
108+
"operator": {
109+
"color": "#EF8080"
110+
},
111+
"punctuation": {
112+
"color": "#E8E8A8"
113+
},
114+
"name": {
115+
"color": "#C4C4C4"
116+
},
117+
"name_builtin": {
118+
"color": "#FF8EC7"
119+
},
120+
"name_tag": {
121+
"color": "#B083EA"
122+
},
123+
"name_attribute": {
124+
"color": "#7A7AE6"
125+
},
126+
"name_class": {
127+
"color": "#F1F1F1",
128+
"underline": true,
129+
"bold": true
130+
},
131+
"name_constant": {},
132+
"name_decorator": {
133+
"color": "#FFFF87"
134+
},
135+
"name_exception": {},
136+
"name_function": {
137+
"color": "#00D787"
138+
},
139+
"name_other": {},
140+
"literal": {},
141+
"literal_number": {
142+
"color": "#6EEFC0"
143+
},
144+
"literal_date": {},
145+
"literal_string": {
146+
"color": "#C69669"
147+
},
148+
"literal_string_escape": {
149+
"color": "#AFFFD7"
150+
},
151+
"generic_deleted": {
152+
"color": "#FD5B5B"
153+
},
154+
"generic_emph": {
155+
"italic": true
156+
},
157+
"generic_inserted": {
158+
"color": "#00D787"
159+
},
160+
"generic_strong": {
161+
"bold": true
162+
},
163+
"generic_subheading": {
164+
"color": "#777777"
165+
},
166+
"background": {
167+
"background_color": "#373737"
168+
}
169+
}
170+
},
171+
"table": {
172+
"center_separator": "┼",
173+
"column_separator": "│",
174+
"row_separator": "─"
175+
},
176+
"definition_list": {},
177+
"definition_term": {},
178+
"definition_description": {
179+
"block_prefix": "\n🠶 "
180+
},
181+
"html_block": {},
182+
"html_span": {}
183+
}

testdata/empty.style

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{}

0 commit comments

Comments
 (0)