Skip to content

Commit

Permalink
test: add test for custom styles
Browse files Browse the repository at this point in the history
  • Loading branch information
bashbunni committed Mar 14, 2024
1 parent ef3b2d1 commit c271197
Show file tree
Hide file tree
Showing 4 changed files with 230 additions and 3 deletions.
6 changes: 4 additions & 2 deletions glamour.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,10 @@ const (
PinkStyle = "pink"
)

const defaultWidth = 80
const highPriority = 1000
const (
defaultWidth = 80
highPriority = 1000
)

// A TermRendererOption sets an option on a TermRenderer.
type TermRendererOption func(*TermRenderer) error
Expand Down
43 changes: 42 additions & 1 deletion glamour_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package glamour

import (
"bytes"
"errors"
"io"
"os"
"strings"
Expand Down Expand Up @@ -43,7 +44,7 @@ func TestTermRendererWriter(t *testing.T) {

// generate
if generate {
err := os.WriteFile(testFile, b, 0644)
err := os.WriteFile(testFile, b, 0o644)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -167,6 +168,46 @@ func TestStyles(t *testing.T) {
}
}

// TestCustomStyle checks the expected errors with custom styling. We need to
// support built-in styles and custom style sheets.
func TestCustomStyle(t *testing.T) {
md := "testdata/example.md"
tests := []struct {
name string
stylePath string
err error
expected string
}{
{name: "style exists", stylePath: "testdata/custom.style", err: nil, expected: "testdata/custom.style"},
{name: "style doesn't exist", stylePath: "testdata/notfound.style", err: os.ErrNotExist, expected: AutoStyle},
{name: "style is empty", stylePath: "", err: nil, expected: AutoStyle},
}

for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
t.Setenv("GLAMOUR_STYLE", tc.stylePath)
g, err := NewTermRenderer(
WithEnvironmentConfig(),
)
if !errors.Is(err, tc.err) {
t.Fatal(err)
}
if !errors.Is(tc.err, os.ErrNotExist) {
w, err := NewTermRenderer(WithStylePath(tc.expected))
if err != nil {
t.Fatal(err)
}
text, _ := os.ReadFile(md)
want, err := w.RenderBytes(text)
got, err := g.RenderBytes(text)
if !bytes.Equal(want, got) {
t.Error("Wrong style used")
}
}
})
}
}

func TestRenderHelpers(t *testing.T) {
in, err := os.ReadFile(markdown)
if err != nil {
Expand Down
183 changes: 183 additions & 0 deletions testdata/custom.style
Original file line number Diff line number Diff line change
@@ -0,0 +1,183 @@
{
"document": {
"block_prefix": "\n",
"block_suffix": "\n",
"color": "252",
"margin": 2
},
"block_quote": {
"indent": 1,
"indent_token": "│ "
},
"paragraph": {},
"list": {
"level_indent": 2
},
"heading": {
"block_suffix": "\n",
"color": "39",
"bold": true
},
"h1": {
"prefix": " ",
"suffix": " ",
"color": "228",
"background_color": "63",
"bold": true
},
"h6": {
"prefix": " ",
"color": "35",
"bold": false
},
"text": {},
"strikethrough": {
"crossed_out": true
},
"emph": {
"italic": true
},
"strong": {
"bold": true
},
"hr": {
"color": "240",
"format": "\n--------\n"
},
"item": {
"block_prefix": "• "
},
"enumeration": {
"block_prefix": ". "
},
"task": {
"ticked": "[✓] ",
"unticked": "[ ] "
},
"link": {
"color": "30",
"underline": true
},
"link_text": {
"color": "35",
"bold": true
},
"image": {
"color": "212",
"underline": true
},
"image_text": {
"color": "243",
"format": "Image: {{.text}} →"
},
"code": {
"prefix": " ",
"suffix": " ",
"color": "203",
"background_color": "236"
},
"code_block": {
"color": "244",
"margin": 2,
"chroma": {
"text": {
"color": "#C4C4C4"
},
"error": {
"color": "#F1F1F1",
"background_color": "#F05B5B"
},
"comment": {
"color": "#676767"
},
"comment_preproc": {
"color": "#FF875F"
},
"keyword": {
"color": "#00AAFF"
},
"keyword_reserved": {
"color": "#FF5FD2"
},
"keyword_namespace": {
"color": "#FF5F87"
},
"keyword_type": {
"color": "#6E6ED8"
},
"operator": {
"color": "#EF8080"
},
"punctuation": {
"color": "#E8E8A8"
},
"name": {
"color": "#C4C4C4"
},
"name_builtin": {
"color": "#FF8EC7"
},
"name_tag": {
"color": "#B083EA"
},
"name_attribute": {
"color": "#7A7AE6"
},
"name_class": {
"color": "#F1F1F1",
"underline": true,
"bold": true
},
"name_constant": {},
"name_decorator": {
"color": "#FFFF87"
},
"name_exception": {},
"name_function": {
"color": "#00D787"
},
"name_other": {},
"literal": {},
"literal_number": {
"color": "#6EEFC0"
},
"literal_date": {},
"literal_string": {
"color": "#C69669"
},
"literal_string_escape": {
"color": "#AFFFD7"
},
"generic_deleted": {
"color": "#FD5B5B"
},
"generic_emph": {
"italic": true
},
"generic_inserted": {
"color": "#00D787"
},
"generic_strong": {
"bold": true
},
"generic_subheading": {
"color": "#777777"
},
"background": {
"background_color": "#373737"
}
}
},
"table": {
"center_separator": "┼",
"column_separator": "│",
"row_separator": "─"
},
"definition_list": {},
"definition_term": {},
"definition_description": {
"block_prefix": "\n🠶 "
},
"html_block": {},
"html_span": {}
}
1 change: 1 addition & 0 deletions testdata/empty.style
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}

0 comments on commit c271197

Please sign in to comment.