Skip to content
Permalink
Browse files

Writing tests

  • Loading branch information...
Depado committed Oct 11, 2017
1 parent 2a3bd08 commit f445a919699d4bec7ee7838c816c292c8e6a6d7e
Showing with 62 additions and 8 deletions.
  1. +15 −8 renderer.go
  2. +47 −0 renderer_test.go
@@ -16,6 +16,14 @@ type Option func(r *Renderer)
// Style is a function option allowing to set the style used by chroma
// Default : "monokai"
func Style(s string) Option {
return func(r *Renderer) {
r.Style = styles.Get(s)
}
}

// ChromaStyle is an option to directly set the style of the renderer using a
// chroma style instead of a string
func ChromaStyle(s *chroma.Style) Option {
return func(r *Renderer) {
r.Style = s
}
@@ -38,6 +46,7 @@ func ChromaOptions(options ...html.Option) Option {
}
}

// Extend allows to specify the blackfriday renderer which is extended
func Extend(br bf.Renderer) Option {
return func(r *Renderer) {
r.Base = br
@@ -50,18 +59,20 @@ func NewRenderer(options ...Option) *Renderer {
Base: bf.NewHTMLRenderer(bf.HTMLRendererParameters{
Flags: bf.CommonHTMLFlags,
}),
Style: "monokai",
Style: styles.Monokai,
Autodetect: true,
}
for _, option := range options {
option(r)
}
r.Formatter = html.New(r.ChromaOptions...)
return r
}

// RenderWithChroma will render the given text to the w io.Writer
func (r *Renderer) RenderWithChroma(w io.Writer, text []byte, data bf.CodeBlockData) error {
var lexer chroma.Lexer

if len(data.Info) > 0 {
lexer = lexers.Get(string(data.Info))
} else if r.Autodetect {
@@ -70,25 +81,21 @@ func (r *Renderer) RenderWithChroma(w io.Writer, text []byte, data bf.CodeBlockD
if lexer == nil {
lexer = lexers.Fallback
}
cstyle := styles.Get(r.Style)
if cstyle == nil {
cstyle = styles.Fallback
}
formatter := html.New(r.ChromaOptions...)
iterator, err := lexer.Tokenise(nil, string(text))
if err != nil {
return err
}
return formatter.Format(w, cstyle, iterator)
return r.Formatter.Format(w, r.Style, iterator)
}

// Renderer is a custom Blackfriday renderer that uses the capabilities of
// chroma to highlight code with triple backtick notation
type Renderer struct {
Base bf.Renderer
Autodetect bool
Style string
ChromaOptions []html.Option
Style *chroma.Style
Formatter chroma.Formatter
}

// RenderNode satisfies the Renderer interface
@@ -0,0 +1,47 @@
package bfchroma

import (
"testing"

"github.com/alecthomas/chroma/styles"

"github.com/stretchr/testify/assert"
bf "gopkg.in/russross/blackfriday.v2"
)

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 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 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 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")
}

0 comments on commit f445a91

Please sign in to comment.
You can’t perform that action at this time.