1
0
mirror of https://github.com/danog/blackfriday.git synced 2025-01-22 13:21:36 +01:00

Create a place for document-level unit tests.

These will be helpful for catching regressions or changes in behavior
to edge cases such as empty input, or specifically crafted inputs that
may cause panics, etc.

Move test for issue #172 there since it's a document-level test, not an
inline one.

Add test for issue #173.

Make some things more consistent.

Don't use a named receiver in methods that don't use it. This makes the
code more readable since one can more quickly tell the inputs to the
method.
This commit is contained in:
Dmitri Shuralyov 2016-07-15 13:52:01 -04:00
parent 1d6b8e9301
commit 96537c6eaa
4 changed files with 83 additions and 14 deletions

View File

@ -515,7 +515,7 @@ func (p *parser) htmlFindEnd(tag string, data []byte) int {
return i + skip
}
func (p *parser) isEmpty(data []byte) int {
func (*parser) isEmpty(data []byte) int {
// it is okay to call isEmpty on an empty buffer
if len(data) == 0 {
return 0
@ -530,7 +530,7 @@ func (p *parser) isEmpty(data []byte) int {
return i + 1
}
func (p *parser) isHRule(data []byte) bool {
func (*parser) isHRule(data []byte) bool {
i := 0
// skip up to three spaces

View File

@ -59,13 +59,11 @@ func doTestsInlineParam(t *testing.T, tests []string, opts Options, htmlFlags in
params HtmlRendererParameters) {
// catch and report panics
var candidate string
/*
defer func() {
if err := recover(); err != nil {
t.Errorf("\npanic while processing [%#v] (%v)\n", candidate, err)
}
}()
*/
defer func() {
if err := recover(); err != nil {
t.Errorf("\npanic while processing [%#v]: %s\n", candidate, err)
}
}()
for i := 0; i+1 < len(tests); i += 2 {
input := tests[i]
@ -722,10 +720,6 @@ func TestReferenceLink(t *testing.T) {
"[link][ref]\n [ref]: /url/",
"<p><a href=\"/url/\">link</a></p>\n",
// Issue 172 in blackfriday
"[]:<",
"<p>[]:&lt;</p>\n",
}
doLinkTestsInline(t, tests)
}

75
markdown_test.go Normal file
View File

@ -0,0 +1,75 @@
//
// 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.
//
//
// Unit tests for full document parsing and rendering
//
package blackfriday
import (
"testing"
)
func runMarkdown(input string) string {
return string(MarkdownCommon([]byte(input)))
}
func doTests(t *testing.T, tests []string) {
// catch and report panics
var candidate string
defer func() {
if err := recover(); err != nil {
t.Errorf("\npanic while processing [%#v]: %s\n", candidate, err)
}
}()
for i := 0; i+1 < len(tests); i += 2 {
input := tests[i]
candidate = input
expected := tests[i+1]
actual := runMarkdown(candidate)
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]
_ = runMarkdown(candidate)
}
}
}
}
}
func TestDocument(t *testing.T) {
var tests = []string{
// Empty document.
"",
"",
" ",
"",
// This shouldn't panic.
// https://github.com/russross/blackfriday/issues/172
"[]:<",
"<p>[]:&lt;</p>\n",
// This shouldn't panic.
// https://github.com/russross/blackfriday/issues/173
" [",
"<p>[</p>\n",
}
doTests(t, tests)
}

View File

@ -29,7 +29,7 @@ func doTestsReference(t *testing.T, files []string, flag int) {
var candidate string
defer func() {
if err := recover(); err != nil {
t.Errorf("\npanic while processing [%#v]\n", candidate)
t.Errorf("\npanic while processing [%#v]: %s\n", candidate, err)
}
}()