markdown/markdown_test.go

79 lines
1.7 KiB
Go
Raw Normal View History

2012-07-20 10:51:57 +00:00
package markdown
import (
"bytes"
"fmt"
"os"
"path/filepath"
"testing"
)
// for each pair of .text/.html files in the given subdirectory
// of `./tests' compare the expected html output with
// the output of Parser.Markdown.
func runDirTests(dir string, t *testing.T) {
dirPath := filepath.Join("tests", dir)
f, err := os.Open(dirPath)
if err != nil {
t.Fatal(err)
}
defer f.Close()
names, err := f.Readdirnames(-1)
if err != nil {
t.Fatal(err)
}
2012-08-31 22:29:18 +00:00
var buf bytes.Buffer
fHTML := ToHTML(&buf)
fGroff := ToGroffMM(&buf)
2012-07-20 10:51:57 +00:00
p := NewParser(nil)
for _, name := range names {
if filepath.Ext(name) != ".text" {
continue
}
2012-08-31 22:29:18 +00:00
if err = compareOutput(&buf, fHTML, ".html", filepath.Join(dirPath, name), p); err != nil {
t.Error(err)
}
if err = compareOutput(&buf, fGroff, ".mm", filepath.Join(dirPath, name), p); err != nil {
2012-07-20 10:51:57 +00:00
t.Error(err)
}
}
}
// Compare the output of the C-based peg-markdown, which
2012-08-31 22:29:18 +00:00
// is, for each test, available in either a .html or a .mm file accompanying
// the .text file, with the output of this package's Markdown processor.
func compareOutput(w *bytes.Buffer, f Formatter, ext string, textPath string, p *Parser) (err error) {
var bOrig bytes.Buffer
2012-07-20 10:51:57 +00:00
r, err := os.Open(textPath)
if err != nil {
return
}
defer r.Close()
2012-08-31 22:29:18 +00:00
w.Reset()
p.Markdown(r, f)
2012-07-20 10:51:57 +00:00
2012-08-31 22:29:18 +00:00
// replace .text extension by `ext'
2012-07-20 10:51:57 +00:00
base := textPath[:len(textPath)-len(".text")]
2012-08-31 22:29:18 +00:00
refPath := base + ext
2012-07-20 10:51:57 +00:00
2012-08-31 22:29:18 +00:00
r, err = os.Open(refPath)
2012-07-20 10:51:57 +00:00
if err != nil {
return
}
defer r.Close()
bOrig.ReadFrom(r)
2012-08-31 22:29:18 +00:00
if bytes.Compare(bOrig.Bytes(), w.Bytes()) != 0 {
err = fmt.Errorf("test %q failed", refPath)
2012-07-20 10:51:57 +00:00
}
return
}
func TestMarkdown103(t *testing.T) {
runDirTests("md1.0.3", t)
}