2012-07-20 10:51:57 +00:00
|
|
|
package markdown
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2013-01-08 20:04:47 +00:00
|
|
|
"strings"
|
2012-07-20 10:51:57 +00:00
|
|
|
"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)
|
|
|
|
}
|
2013-01-08 20:04:47 +00:00
|
|
|
|
|
|
|
// This test will make the test run fail with a
|
|
|
|
// message like "Buffer not empty" under the
|
|
|
|
// following condition:
|
|
|
|
//
|
|
|
|
// There exists an unprocessed, remaining portion of the
|
|
|
|
// input buffer after the previous parser call, which
|
|
|
|
// consists only of whitespace.
|
|
|
|
// This whitespace should have been ignored, but, due to
|
|
|
|
// a bug, hasn't.
|
|
|
|
func TestTrailingWhitespaceBug(t *testing.T) {
|
|
|
|
const input = `* foo
|
|
|
|
|
|
|
|
# bar
|
|
|
|
|
|
|
|
* baz
|
|
|
|
`
|
|
|
|
var buf bytes.Buffer
|
|
|
|
p := NewParser(nil)
|
|
|
|
p.Markdown(strings.NewReader(input), ToHTML(&buf))
|
|
|
|
}
|