Skip to content

Commit

Permalink
Add tests for custom node renderers
Browse files Browse the repository at this point in the history
  • Loading branch information
teekennedy committed Feb 8, 2024
1 parent 1a00bea commit 0e6a9a7
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 9 deletions.
1 change: 1 addition & 0 deletions renderer.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ func (r *Renderer) Render(w io.Writer, source []byte, n ast.Node) error {
})
}

// transform wraps a renderer.NodeRendererFunc to match the nodeRenderer function signature
func (r *Renderer) transform(fn renderer.NodeRendererFunc) nodeRenderer {
return func(n ast.Node, entering bool) ast.WalkStatus {
status, _ := fn(r.rc.writer, r.rc.source, n, entering)
Expand Down
20 changes: 19 additions & 1 deletion renderer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/stretchr/testify/assert"
"github.com/yuin/goldmark"
"github.com/yuin/goldmark/ast"
"github.com/yuin/goldmark/extension"
"github.com/yuin/goldmark/parser"
"github.com/yuin/goldmark/text"
"github.com/yuin/goldmark/util"
Expand Down Expand Up @@ -59,9 +60,26 @@ func TestRenderError(t *testing.T) {
assert.Equal(t, err, result)
}

// TestCustomRenderers tests that the renderer uses any config.NodeRenderers defined by the user
func TestCustomRenderers(t *testing.T) {
md := goldmark.New(
goldmark.WithRenderer(NewRenderer()),
goldmark.WithParserOptions(parser.WithASTTransformers(util.Prioritized(&transformer, 0))),
)
buf := bytes.Buffer{}
source := `# My Tasks
- [x] Add support for custom renderers
`

extension.TaskList.Extend(md)
err := md.Convert([]byte(source), &buf)
assert.NoError(t, err)
t.Log(buf.String())
}

// TestRenderedOutput tests that the renderer produces the expected output for all test cases
func TestRenderedOutput(t *testing.T) {
var testCases = []struct {
testCases := []struct {
name string
options []Option
source string
Expand Down
12 changes: 5 additions & 7 deletions writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,21 +140,19 @@ func (m *markdownWriter) Err() error {
return m.err
}

// returns how many bytes are unused in the buffer.
// Available returns how many bytes are unused in the buffer.
func (m *markdownWriter) Available() int {
return m.buf.Cap() - m.buf.Len()
return m.buf.Available()

Check failure on line 145 in writer.go

View workflow job for this annotation

GitHub Actions / lint

m.buf.Available undefined (type *bytes.Buffer has no field or method Available) (typecheck)
}

// Buffered returns the number of bytes that have been written into the current buffer.
func (m *markdownWriter) Buffered() int {
return m.buf.Len()
}

// Flush flushes the contents of the buffer to the output.
func (m *markdownWriter) Flush() error {
_, err := m.output.Write(m.buf.Bytes())
if err != nil {
return err
}
m.buf.Reset()
m.FlushLine()
return nil
}

Expand Down
23 changes: 22 additions & 1 deletion writer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,29 @@ import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestWrite(t *testing.T) {
buf := &bytes.Buffer{}
writer := newMarkdownWriter(buf, NewConfig())

available := writer.Available()
assert.Equal(t, 0, available)
assert.Equal(t, 0, writer.Buffered())

writer.WriteByte(byte('b'))
writer.WriteRune('r')
writer.WriteString("s")

assert.Less(t, available, writer.Available())
assert.Equal(t, 3, writer.Buffered())

err := writer.Flush()
require.NoError(t, err)
assert.Equal(t, "brs\n", buf.String())
}

// TestFlushLine tests that the writer will flush the current buffered line if non-empty.
func TestFlushLine(t *testing.T) {
assert := assert.New(t)
Expand Down Expand Up @@ -38,7 +59,7 @@ func TestEndLine(t *testing.T) {

// TestWriterOutputs tests that the writer produces expected output in various scenarios.
func TestWriterOutputs(t *testing.T) {
var testCases = []struct {
testCases := []struct {
name string
writeFunc func(writer *markdownWriter)
expected string
Expand Down

0 comments on commit 0e6a9a7

Please sign in to comment.