Add tests for new footnote functionality

This commit is contained in:
Daniel Imfeld 2014-05-25 13:07:05 -05:00
parent 628c02d37b
commit 2ce0592896
2 changed files with 36 additions and 17 deletions

View File

@ -14,29 +14,33 @@
package blackfriday package blackfriday
import ( import (
"regexp"
"testing" "testing"
"strings"
) )
func runMarkdownInline(input string, extensions, htmlFlags int) string { func runMarkdownInline(input string, extensions, htmlFlags int, params HtmlRendererParameters) string {
extensions |= EXTENSION_AUTOLINK extensions |= EXTENSION_AUTOLINK
extensions |= EXTENSION_STRIKETHROUGH extensions |= EXTENSION_STRIKETHROUGH
htmlFlags |= HTML_USE_XHTML htmlFlags |= HTML_USE_XHTML
renderer := HtmlRenderer(htmlFlags, "", "") renderer := HtmlRendererWithParameters(htmlFlags, "", "", params)
return string(Markdown([]byte(input), renderer, extensions)) return string(Markdown([]byte(input), renderer, extensions))
} }
func doTestsInline(t *testing.T, tests []string) { func doTestsInline(t *testing.T, tests []string) {
doTestsInlineParam(t, tests, 0, 0) doTestsInlineParam(t, tests, 0, 0, HtmlRendererParameters{})
} }
func doSafeTestsInline(t *testing.T, tests []string) { func doSafeTestsInline(t *testing.T, tests []string) {
doTestsInlineParam(t, tests, 0, HTML_SAFELINK) doTestsInlineParam(t, tests, 0, HTML_SAFELINK, HtmlRendererParameters{})
} }
func doTestsInlineParam(t *testing.T, tests []string, extensions, htmlFlags int) { func doTestsInlineParam(t *testing.T, tests []string, extensions, htmlFlags int,
params HtmlRendererParameters) {
// catch and report panics // catch and report panics
var candidate string var candidate string
/* /*
@ -51,7 +55,7 @@ func doTestsInlineParam(t *testing.T, tests []string, extensions, htmlFlags int)
input := tests[i] input := tests[i]
candidate = input candidate = input
expected := tests[i+1] expected := tests[i+1]
actual := runMarkdownInline(candidate, extensions, htmlFlags) actual := runMarkdownInline(candidate, extensions, htmlFlags, params)
if actual != expected { if actual != expected {
t.Errorf("\nInput [%#v]\nExpected[%#v]\nActual [%#v]", t.Errorf("\nInput [%#v]\nExpected[%#v]\nActual [%#v]",
candidate, expected, actual) candidate, expected, actual)
@ -62,7 +66,7 @@ func doTestsInlineParam(t *testing.T, tests []string, extensions, htmlFlags int)
for start := 0; start < len(input); start++ { for start := 0; start < len(input); start++ {
for end := start + 1; end <= len(input); end++ { for end := start + 1; end <= len(input); end++ {
candidate = input[start:end] candidate = input[start:end]
_ = runMarkdownInline(candidate, extensions, htmlFlags) _ = runMarkdownInline(candidate, extensions, htmlFlags, params)
} }
} }
} }
@ -391,13 +395,14 @@ func TestNofollowLink(t *testing.T) {
"[foo](http://bar.com/foo/)\n", "[foo](http://bar.com/foo/)\n",
"<p><a href=\"http://bar.com/foo/\" rel=\"nofollow\">foo</a></p>\n", "<p><a href=\"http://bar.com/foo/\" rel=\"nofollow\">foo</a></p>\n",
} }
doTestsInlineParam(t, tests, 0, HTML_SAFELINK|HTML_NOFOLLOW_LINKS|HTML_SANITIZE_OUTPUT) doTestsInlineParam(t, tests, 0, HTML_SAFELINK|HTML_NOFOLLOW_LINKS|HTML_SANITIZE_OUTPUT,
HtmlRendererParameters{})
// HTML_SANITIZE_OUTPUT won't allow relative links, so test that separately: // HTML_SANITIZE_OUTPUT won't allow relative links, so test that separately:
tests = []string{ tests = []string{
"[foo](/bar/)\n", "[foo](/bar/)\n",
"<p><a href=\"/bar/\">foo</a></p>\n", "<p><a href=\"/bar/\">foo</a></p>\n",
} }
doTestsInlineParam(t, tests, 0, HTML_SAFELINK|HTML_NOFOLLOW_LINKS) doTestsInlineParam(t, tests, 0, HTML_SAFELINK|HTML_NOFOLLOW_LINKS, HtmlRendererParameters{})
} }
func TestHrefTargetBlank(t *testing.T) { func TestHrefTargetBlank(t *testing.T) {
@ -409,7 +414,7 @@ func TestHrefTargetBlank(t *testing.T) {
"[foo](http://example.com)\n", "[foo](http://example.com)\n",
"<p><a href=\"http://example.com\" target=\"_blank\">foo</a></p>\n", "<p><a href=\"http://example.com\" target=\"_blank\">foo</a></p>\n",
} }
doTestsInlineParam(t, tests, 0, HTML_SAFELINK|HTML_HREF_TARGET_BLANK) doTestsInlineParam(t, tests, 0, HTML_SAFELINK|HTML_HREF_TARGET_BLANK, HtmlRendererParameters{})
} }
func TestSafeInlineLink(t *testing.T) { func TestSafeInlineLink(t *testing.T) {
@ -595,7 +600,7 @@ func TestAutoLink(t *testing.T) {
doTestsInline(t, tests) doTestsInline(t, tests)
} }
var footenoteTests = []string{ var footnoteTests = []string{
"testing footnotes.[^a]\n\n[^a]: This is the note\n", "testing footnotes.[^a]\n\n[^a]: This is the note\n",
`<p>testing footnotes.<sup class="footnote-ref" id="fnref:a"><a rel="footnote" href="#fn:a">1</a></sup></p> `<p>testing footnotes.<sup class="footnote-ref" id="fnref:a"><a rel="footnote" href="#fn:a">1</a></sup></p>
<div class="footnotes"> <div class="footnotes">
@ -731,16 +736,30 @@ what happens here
} }
func TestFootnotes(t *testing.T) { func TestFootnotes(t *testing.T) {
doTestsInlineParam(t, footenoteTests, EXTENSION_FOOTNOTES, 0) doTestsInlineParam(t, footnoteTests, EXTENSION_FOOTNOTES, 0, HtmlRendererParameters{})
} }
func TestFootnotesWithParameters(t *testing.T) { func TestFootnotesWithParameters(t *testing.T) {
tests := make([]string, len(footnoteTests)) tests := make([]string, len(footnoteTests))
prefix := "testPrefix" prefix := "testPrefix"
for i, test := range footnoteTests { returnText := "ret"
re := regexp.MustCompile(`(?ms)<li id="fn:(\S+?)">(.*?)</li>`)
// Transform the test expectations to match the parameters we're using.
for i, test := range footnoteTests {
if i%2 == 1 {
test = strings.Replace(test, "fn:", "fn:"+prefix, -1)
test = strings.Replace(test, "fnref:", "fnref:"+prefix, -1)
test = re.ReplaceAllString(test, `<li id="fn:$1">$2 <a class="footnote-return" href="#fnref:$1">ret</a></li>`)
}
tests[i] = test
} }
doTestsInlineParam(t, tests, EXTENSION_FOOTNOTES, HTML_FOOTNOTE_RETURN_LINKS) params := HtmlRendererParameters{
FootnoteAnchorPrefix: prefix,
FootnoteReturnLinkContents: returnText,
}
doTestsInlineParam(t, tests, EXTENSION_FOOTNOTES, HTML_FOOTNOTE_RETURN_LINKS, params)
} }

View File

@ -5,7 +5,7 @@ import (
) )
func doTestsSanitize(t *testing.T, tests []string) { func doTestsSanitize(t *testing.T, tests []string) {
doTestsInlineParam(t, tests, 0, HTML_SKIP_STYLE|HTML_SANITIZE_OUTPUT) doTestsInlineParam(t, tests, 0, HTML_SKIP_STYLE|HTML_SANITIZE_OUTPUT, HtmlRendererParameters{})
} }
func TestSanitizeRawHtmlTag(t *testing.T) { func TestSanitizeRawHtmlTag(t *testing.T) {
@ -192,8 +192,8 @@ func TestSanitizeInlineLink(t *testing.T) {
tests := []string{ tests := []string{
"[link](javascript:evil)", "[link](javascript:evil)",
"<p><a>link</a></p>\n", "<p><a>link</a></p>\n",
"[link](/abc)", "[link](/abc)",
"<p><a href=\"/abc\">link</a></p>\n", "<p><a href=\"/abc\">link</a></p>\n",
} }
doTestsSanitize(t, tests) doTestsSanitize(t, tests)
} }