add some tests

This commit is contained in:
dre 2021-07-05 22:12:10 +08:00
parent 8acd0f6489
commit 1f2311a0b3
5 changed files with 134 additions and 51 deletions

81
mdproc/hugo_test.go Normal file
View file

@ -0,0 +1,81 @@
package mdproc_test
import (
"strings"
"testing"
"github.com/n0x1m/md2gmi/mdproc"
"github.com/n0x1m/md2gmi/pipe"
)
func TestHugoMarkdownToGmi(t *testing.T) {
source := func() chan pipe.StreamItem {
input := `
---
title: "This is the Title!"
categories: [a,b]
---
<!-- a
comment -->
> this is
a quote
This is
a paragraph.
` + "```" + `
this is
code
` + "```" + `
`
data := make(chan pipe.StreamItem, len(strings.Split(input, "\n")))
for _, line := range strings.Split(input, "\n") {
data <- pipe.NewItem(0, []byte(line))
}
close(data)
return data
}
sink := func(dest chan pipe.StreamItem) {
var data []byte
for in := range dest {
data = append(data, in.Payload()...)
}
expected := `
--- title: "This is the Title!" categories: [a,b] ---
<!-- a comment -->
> this is a quote
This is a paragraph.
` + "```" + `
this is
code
` + "```" + `
`
if string(data) != expected {
t.Errorf("mismatch, expected '%s' but was '%s'", expected, data)
}
}
sink(mdproc.Preproc()(source()))
//s := pipe.New()
//s.Use(mdproc.Preproc())
//s.Use(mdproc.RemoveFrontMatter)
//s.Use(mdproc.RemoveComments)
//s.Use(mdproc.FormatHeadings)
//s.Use(mdproc.FormatLinks)
//s.Handle(source, sink)
}

View file

@ -1,43 +1,7 @@
package pipe
import (
"bytes"
"encoding/gob"
)
type Connector chan StreamItem
type StreamItem struct {
index int
payload []byte
}
func NewItem(index int, payload []byte) StreamItem {
var buf bytes.Buffer
w := StreamItem{index: index}
if err := gob.NewEncoder(&buf).Encode(payload); err != nil {
// assert no broken pipes
panic(err)
}
w.payload = buf.Bytes()
return w
}
func (w *StreamItem) Index() int {
return w.index
}
func (w *StreamItem) Payload() []byte {
var dec []byte
buf := bytes.NewReader(w.payload)
if err := gob.NewDecoder(buf).Decode(&dec); err != nil {
// assert no broken pipes
panic(err)
}
return dec
}
type Source func() chan StreamItem
type Sink func(chan StreamItem)
type Pipeline func(chan StreamItem) chan StreamItem

View file

@ -1,7 +0,0 @@
package pipe
type Connector chan StreamItem
type Source func() chan StreamItem
type Sink func(chan StreamItem)
type Pipeline func(chan StreamItem) chan StreamItem

View file

@ -15,10 +15,6 @@ func (s *Stream) Use(nodes ...Pipeline) {
s.nodes = append(s.nodes, nodes...)
}
func Chain(middlewares ...Pipeline) Pipelines {
return Pipelines(middlewares)
}
// chain builds a Connector composed of an inline pipeline stack and endpoint
// processor in the order they are passed.
func chain(nodes []Pipeline, src Connector) Connector {

49
pipe/streamitem.go Normal file
View file

@ -0,0 +1,49 @@
package pipe
import (
"bytes"
"context"
"encoding/gob"
)
type StreamItem struct {
ctx context.Context
index int
payload []byte
}
func (s *StreamItem) Context() context.Context {
return s.ctx
}
func NewItem(index int, payload []byte) StreamItem {
var buf bytes.Buffer
w := StreamItem{index: index}
if err := gob.NewEncoder(&buf).Encode(payload); err != nil {
// assert no broken pipes
panic(err)
}
w.payload = buf.Bytes()
return w
}
func (w *StreamItem) Index() int {
return w.index
}
func (w *StreamItem) Payload() []byte {
var dec []byte
buf := bytes.NewReader(w.payload)
if err := gob.NewDecoder(buf).Decode(&dec); err != nil {
// assert no broken pipes
panic(err)
}
return dec
}