blackfriday/test_helpers.go
Vytautas Šaltenis f35fae8188 Extract panic recovery code
Call recover() in a single place so it could be easily turned off when
not needed.
2016-04-04 13:13:57 +03:00

195 lines
5.8 KiB
Go

//
// Blackfriday Markdown Processor
// Available at http://github.com/russross/blackfriday
//
// Copyright © 2011 Russ Ross <russ@russross.com>.
// Distributed under the Simplified BSD License.
// See README.md for details.
//
//
// Helper functions for unit testing
//
package blackfriday
import (
"io/ioutil"
"path/filepath"
"regexp"
"testing"
)
type TestParams struct {
Options
HTMLFlags
HTMLRendererParameters
}
func execRecoverableTestSuite(t *testing.T, tests []string, params TestParams, suite func(candidate *string)) {
// Catch and report panics. This is useful when running 'go test -v' on
// the integration server. When developing, though, crash dump is often
// preferable, so recovery can be easily turned off with doRecover = false.
var candidate string
const doRecover = true
if doRecover {
defer func() {
if err := recover(); err != nil {
t.Errorf("\npanic while processing [%#v]: %s\n", candidate, err)
}
}()
}
suite(&candidate)
}
func runMarkdownBlockWithRenderer(input string, extensions Extensions, renderer Renderer) string {
return string(Markdown([]byte(input), renderer, extensions))
}
func runMarkdownBlock(input string, params TestParams) string {
renderer := HTMLRendererWithParameters(params.HTMLFlags|UseXHTML,
params.Options.Extensions, "", "", params.HTMLRendererParameters)
return runMarkdownBlockWithRenderer(input, params.Options.Extensions, renderer)
}
func doTestsBlock(t *testing.T, tests []string, extensions Extensions) {
doTestsBlockWithRunner(t, tests, TestParams{
Options: Options{Extensions: extensions},
}, runMarkdownBlock)
}
func doTestsBlockWithRunner(t *testing.T, tests []string, params TestParams, runner func(string, TestParams) string) {
doTestsWithRunner(t, tests, params, runner)
}
func doTestsWithRunner(t *testing.T, tests []string, params TestParams, runner func(string, TestParams) string) {
execRecoverableTestSuite(t, tests, params, func(candidate *string) {
for i := 0; i+1 < len(tests); i += 2 {
input := tests[i]
*candidate = input
expected := tests[i+1]
actual := runner(*candidate, params)
if actual != expected {
t.Errorf("\nInput [%#v]\nExpected[%#v]\nActual [%#v]",
candidate, expected, actual)
}
// now test every substring to stress test bounds checking
if !testing.Short() {
for start := 0; start < len(input); start++ {
for end := start + 1; end <= len(input); end++ {
*candidate = input[start:end]
runner(*candidate, params)
}
}
}
}
})
}
func runMarkdownInline(input string, params TestParams) string {
opts := params.Options
opts.Extensions |= Autolink
opts.Extensions |= Strikethrough
renderer := HTMLRendererWithParameters(params.HTMLFlags|UseXHTML,
opts.Extensions, "", "", params.HTMLRendererParameters)
return string(MarkdownOptions([]byte(input), renderer, opts))
}
func doTestsInline(t *testing.T, tests []string) {
doTestsInlineParam(t, tests, TestParams{})
}
func doLinkTestsInline(t *testing.T, tests []string) {
doTestsInline(t, tests)
prefix := "http://localhost"
params := HTMLRendererParameters{AbsolutePrefix: prefix}
transformTests := transformLinks(tests, prefix)
doTestsInlineParam(t, transformTests, TestParams{
HTMLRendererParameters: params,
})
doTestsInlineParam(t, transformTests, TestParams{
HTMLFlags: CommonHtmlFlags,
HTMLRendererParameters: params,
})
}
func doSafeTestsInline(t *testing.T, tests []string) {
doTestsInlineParam(t, tests, TestParams{HTMLFlags: Safelink})
// All the links in this test should not have the prefix appended, so
// just rerun it with different parameters and the same expectations.
prefix := "http://localhost"
params := HTMLRendererParameters{AbsolutePrefix: prefix}
transformTests := transformLinks(tests, prefix)
doTestsInlineParam(t, transformTests, TestParams{
HTMLFlags: Safelink,
HTMLRendererParameters: params,
})
}
func doTestsInlineParam(t *testing.T, tests []string, params TestParams) {
doTestsWithRunner(t, tests, params, runMarkdownInline)
}
func transformLinks(tests []string, prefix string) []string {
newTests := make([]string, len(tests))
anchorRe := regexp.MustCompile(`<a href="/(.*?)"`)
imgRe := regexp.MustCompile(`<img src="/(.*?)"`)
for i, test := range tests {
if i%2 == 1 {
test = anchorRe.ReplaceAllString(test, `<a href="`+prefix+`/$1"`)
test = imgRe.ReplaceAllString(test, `<img src="`+prefix+`/$1"`)
}
newTests[i] = test
}
return newTests
}
func runMarkdownReference(input string, params TestParams) string {
renderer := HTMLRenderer(0, params.Options.Extensions, "", "")
return string(Markdown([]byte(input), renderer, params.Options.Extensions))
}
func doTestsReference(t *testing.T, files []string, flag Extensions) {
params := TestParams{Options: Options{Extensions: flag}}
execRecoverableTestSuite(t, files, params, func(candidate *string) {
for _, basename := range files {
filename := filepath.Join("testdata", basename+".text")
inputBytes, err := ioutil.ReadFile(filename)
if err != nil {
t.Errorf("Couldn't open '%s', error: %v\n", filename, err)
continue
}
input := string(inputBytes)
filename = filepath.Join("testdata", basename+".html")
expectedBytes, err := ioutil.ReadFile(filename)
if err != nil {
t.Errorf("Couldn't open '%s', error: %v\n", filename, err)
continue
}
expected := string(expectedBytes)
actual := string(runMarkdownReference(input, params))
if actual != expected {
t.Errorf("\n [%#v]\nExpected[%#v]\nActual [%#v]",
basename+".text", expected, actual)
}
// now test every prefix of every input to check for
// bounds checking
if !testing.Short() {
start, max := 0, len(input)
for end := start + 1; end <= max; end++ {
*candidate = input[start:end]
_ = runMarkdownReference(*candidate, params)
}
}
}
})
}