all repos — grayfriday @ b069de6276dd481b9f2af5e04315e85c53e35ff0

blackfriday fork with a few changes

Wrap inline test parameters in a struct

Take advantage of zero value initialization.
Vytautas Ĺ altenis vytas@rtfb.lt
Mon, 04 Apr 2016 11:18:50 +0300
commit

b069de6276dd481b9f2af5e04315e85c53e35ff0

parent

15e052e478acfe5aa596444d7b42831519705802

2 files changed, 117 insertions(+), 69 deletions(-)

jump to
M inline_test.goinline_test.go

@@ -99,38 +99,41 @@

"test [ref5][]\n", "<p>test <a href=\"http://www.ref5.com/\" title=\"Reference 5\">Moo</a></p>\n", } - doTestsInlineParam(t, tests, Options{ - ReferenceOverride: func(reference string) (rv *Reference, overridden bool) { - switch reference { - case "ref1": - // just an overriden reference exists without definition - return &Reference{ - Link: "http://www.ref1.com/", - Title: "Reference 1"}, true - case "ref2": - // overridden exists and reference defined - return &Reference{ - Link: "http://www.overridden.com/", - Title: "Reference Overridden"}, true - case "ref3": - // not overridden and reference defined + doTestsInlineParam(t, tests, TestParams{ + Options: Options{ + ReferenceOverride: func(reference string) (rv *Reference, overridden bool) { + switch reference { + case "ref1": + // just an overriden reference exists without definition + return &Reference{ + Link: "http://www.ref1.com/", + Title: "Reference 1"}, true + case "ref2": + // overridden exists and reference defined + return &Reference{ + Link: "http://www.overridden.com/", + Title: "Reference Overridden"}, true + case "ref3": + // not overridden and reference defined + return nil, false + case "ref4": + // overridden missing and defined + return nil, true + case "!(*http.ServeMux).ServeHTTP": + return &Reference{ + Link: "http://localhost:6060/pkg/net/http/#ServeMux.ServeHTTP", + Title: "ServeHTTP docs"}, true + case "ref5": + return &Reference{ + Link: "http://www.ref5.com/", + Title: "Reference 5", + Text: "Moo", + }, true + } return nil, false - case "ref4": - // overridden missing and defined - return nil, true - case "!(*http.ServeMux).ServeHTTP": - return &Reference{ - Link: "http://localhost:6060/pkg/net/http/#ServeMux.ServeHTTP", - Title: "ServeHTTP docs"}, true - case "ref5": - return &Reference{ - Link: "http://www.ref5.com/", - Title: "Reference 5", - Text: "Moo", - }, true - } - return nil, false - }}, 0, HTMLRendererParameters{}) + }, + }, + }) } func TestStrong(t *testing.T) {

@@ -340,9 +343,8 @@

"this has an \nextra space\n", "<p>this has an<br />\nextra space</p>\n", } - doTestsInlineParam(t, tests, Options{ - Extensions: BackslashLineBreak}, - 0, HTMLRendererParameters{}) + doTestsInlineParam(t, tests, TestParams{Options: Options{ + Extensions: BackslashLineBreak}}) } func TestInlineLink(t *testing.T) {

@@ -482,8 +484,9 @@

"[foo](../bar)\n", "<p><a href=\"../bar\">foo</a></p>\n", } - doTestsInlineParam(t, nofollowTests, Options{}, Safelink|NofollowLinks, - HTMLRendererParameters{}) + doTestsInlineParam(t, nofollowTests, TestParams{ + HTMLFlags: Safelink | NofollowLinks, + }) var noreferrerTests = []string{ "[foo](http://bar.com/foo/)\n",

@@ -492,8 +495,9 @@

"[foo](/bar/)\n", "<p><a href=\"/bar/\">foo</a></p>\n", } - doTestsInlineParam(t, noreferrerTests, Options{}, Safelink|NoreferrerLinks, - HTMLRendererParameters{}) + doTestsInlineParam(t, noreferrerTests, TestParams{ + HTMLFlags: Safelink | NoreferrerLinks, + }) var nofollownoreferrerTests = []string{ "[foo](http://bar.com/foo/)\n",

@@ -502,8 +506,9 @@

"[foo](/bar/)\n", "<p><a href=\"/bar/\">foo</a></p>\n", } - doTestsInlineParam(t, nofollownoreferrerTests, Options{}, Safelink|NofollowLinks|NoreferrerLinks, - HTMLRendererParameters{}) + doTestsInlineParam(t, nofollownoreferrerTests, TestParams{ + HTMLFlags: Safelink | NofollowLinks | NoreferrerLinks, + }) } func TestHrefTargetBlank(t *testing.T) {

@@ -530,7 +535,9 @@

"[foo](http://example.com)\n", "<p><a href=\"http://example.com\" target=\"_blank\">foo</a></p>\n", } - doTestsInlineParam(t, tests, Options{}, Safelink|HrefTargetBlank, HTMLRendererParameters{}) + doTestsInlineParam(t, tests, TestParams{ + HTMLFlags: Safelink | HrefTargetBlank, + }) } func TestSafeInlineLink(t *testing.T) {

@@ -918,7 +925,9 @@ `,

} func TestFootnotes(t *testing.T) { - doTestsInlineParam(t, footnoteTests, Options{Extensions: Footnotes}, 0, HTMLRendererParameters{}) + doTestsInlineParam(t, footnoteTests, TestParams{ + Options: Options{Extensions: Footnotes}, + }) } func TestFootnotesWithParameters(t *testing.T) {

@@ -943,7 +952,11 @@ FootnoteAnchorPrefix: prefix,

FootnoteReturnLinkContents: returnText, } - doTestsInlineParam(t, tests, Options{Extensions: Footnotes}, FootnoteReturnLinks, params) + doTestsInlineParam(t, tests, TestParams{ + Options: Options{Extensions: Footnotes}, + HTMLFlags: FootnoteReturnLinks, + HTMLRendererParameters: params, + }) } func TestNestedFootnotes(t *testing.T) {

@@ -970,8 +983,7 @@

</div> `, } - doTestsInlineParam(t, tests, Options{Extensions: Footnotes}, 0, - HTMLRendererParameters{}) + doTestsInlineParam(t, tests, TestParams{Options: Options{Extensions: Footnotes}}) } func TestInlineComments(t *testing.T) {

@@ -1000,7 +1012,11 @@

"blahblah\n<!--- foo -->\nrhubarb\n", "<p>blahblah\n<!--- foo -->\nrhubarb</p>\n", } - doTestsInlineParam(t, tests, Options{Extensions: Smartypants | SmartypantsDashes}, 0, HTMLRendererParameters{}) + doTestsInlineParam(t, tests, TestParams{ + Options: Options{ + Extensions: Smartypants | SmartypantsDashes, + }, + }) } func TestSmartDoubleQuotes(t *testing.T) {

@@ -1012,7 +1028,11 @@ "<p>this &ldquo; single double</p>\n",

"two pair of \"some\" quoted \"text\".\n", "<p>two pair of &ldquo;some&rdquo; quoted &ldquo;text&rdquo;.</p>\n"} - doTestsInlineParam(t, tests, Options{Extensions: Smartypants}, 0, HTMLRendererParameters{}) + doTestsInlineParam(t, tests, TestParams{ + Options: Options{ + Extensions: Smartypants, + }, + }) } func TestSmartAngledDoubleQuotes(t *testing.T) {

@@ -1024,7 +1044,11 @@ "<p>this &laquo; single double</p>\n",

"two pair of \"some\" quoted \"text\".\n", "<p>two pair of &laquo;some&raquo; quoted &laquo;text&raquo;.</p>\n"} - doTestsInlineParam(t, tests, Options{Extensions: Smartypants | SmartypantsAngledQuotes}, 0, HTMLRendererParameters{}) + doTestsInlineParam(t, tests, TestParams{ + Options: Options{ + Extensions: Smartypants | SmartypantsAngledQuotes, + }, + }) } func TestSmartFractions(t *testing.T) {

@@ -1034,7 +1058,11 @@ "<p>&frac12;, &frac14; and &frac34;; &frac14;th and &frac34;ths</p>\n",

"1/2/2015, 1/4/2015, 3/4/2015; 2015/1/2, 2015/1/4, 2015/3/4.\n", "<p>1/2/2015, 1/4/2015, 3/4/2015; 2015/1/2, 2015/1/4, 2015/3/4.</p>\n"} - doTestsInlineParam(t, tests, Options{Extensions: Smartypants}, 0, HTMLRendererParameters{}) + doTestsInlineParam(t, tests, TestParams{ + Options: Options{ + Extensions: Smartypants, + }, + }) tests = []string{ "1/2, 2/3, 81/100 and 1000000/1048576.\n",

@@ -1042,7 +1070,11 @@ "<p><sup>1</sup>&frasl;<sub>2</sub>, <sup>2</sup>&frasl;<sub>3</sub>, <sup>81</sup>&frasl;<sub>100</sub> and <sup>1000000</sup>&frasl;<sub>1048576</sub>.</p>\n",

"1/2/2015, 1/4/2015, 3/4/2015; 2015/1/2, 2015/1/4, 2015/3/4.\n", "<p>1/2/2015, 1/4/2015, 3/4/2015; 2015/1/2, 2015/1/4, 2015/3/4.</p>\n"} - doTestsInlineParam(t, tests, Options{Extensions: Smartypants | SmartypantsFractions}, 0, HTMLRendererParameters{}) + doTestsInlineParam(t, tests, TestParams{ + Options: Options{ + Extensions: Smartypants | SmartypantsFractions, + }, + }) } func TestDisableSmartDashes(t *testing.T) {

@@ -1053,7 +1085,7 @@ "foo -- bar\n",

"<p>foo -- bar</p>\n", "foo --- bar\n", "<p>foo --- bar</p>\n", - }, Options{}, 0, HTMLRendererParameters{}) + }, TestParams{}) doTestsInlineParam(t, []string{ "foo - bar\n", "<p>foo &ndash; bar</p>\n",

@@ -1061,7 +1093,7 @@ "foo -- bar\n",

"<p>foo &mdash; bar</p>\n", "foo --- bar\n", "<p>foo &mdash;&ndash; bar</p>\n", - }, Options{Extensions: Smartypants | SmartypantsDashes}, 0, HTMLRendererParameters{}) + }, TestParams{Options: Options{Extensions: Smartypants | SmartypantsDashes}}) doTestsInlineParam(t, []string{ "foo - bar\n", "<p>foo - bar</p>\n",

@@ -1069,8 +1101,11 @@ "foo -- bar\n",

"<p>foo &ndash; bar</p>\n", "foo --- bar\n", "<p>foo &mdash; bar</p>\n", - }, Options{Extensions: Smartypants | SmartypantsLatexDashes | SmartypantsDashes}, 0, - HTMLRendererParameters{}) + }, TestParams{ + Options: Options{ + Extensions: Smartypants | SmartypantsLatexDashes | SmartypantsDashes, + }, + }) doTestsInlineParam(t, []string{ "foo - bar\n", "<p>foo - bar</p>\n",

@@ -1078,7 +1113,6 @@ "foo -- bar\n",

"<p>foo -- bar</p>\n", "foo --- bar\n", "<p>foo --- bar</p>\n", - }, Options{Extensions: Smartypants | SmartypantsLatexDashes}, - 0, - HTMLRendererParameters{}) + }, TestParams{ + Options: Options{Extensions: Smartypants | SmartypantsLatexDashes}}) }
M test_helpers.gotest_helpers.go

@@ -20,6 +20,12 @@ "regexp"

"testing" ) +type TestParams struct { + Options + HTMLFlags + HTMLRendererParameters +} + func runMarkdownBlockWithRenderer(input string, extensions Extensions, renderer Renderer) string { return string(Markdown([]byte(input), renderer, extensions)) }

@@ -71,19 +77,19 @@ }

} } -func runMarkdownInline(input string, opts Options, htmlFlags HTMLFlags, params HTMLRendererParameters) string { +func runMarkdownInline(input string, params TestParams) string { + opts := params.Options opts.Extensions |= Autolink opts.Extensions |= Strikethrough - htmlFlags |= UseXHTML - - renderer := HTMLRendererWithParameters(htmlFlags, opts.Extensions, "", "", params) + 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, Options{}, 0, HTMLRendererParameters{}) + doTestsInlineParam(t, tests, TestParams{}) } func doLinkTestsInline(t *testing.T, tests []string) {

@@ -92,22 +98,30 @@

prefix := "http://localhost" params := HTMLRendererParameters{AbsolutePrefix: prefix} transformTests := transformLinks(tests, prefix) - doTestsInlineParam(t, transformTests, Options{}, 0, params) - doTestsInlineParam(t, transformTests, Options{}, CommonHtmlFlags, params) + doTestsInlineParam(t, transformTests, TestParams{ + HTMLRendererParameters: params, + }) + doTestsInlineParam(t, transformTests, TestParams{ + HTMLFlags: CommonHtmlFlags, + HTMLRendererParameters: params, + }) } func doSafeTestsInline(t *testing.T, tests []string) { - doTestsInlineParam(t, tests, Options{}, Safelink, HTMLRendererParameters{}) + 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, Options{}, Safelink, params) + doTestsInlineParam(t, transformTests, TestParams{ + HTMLFlags: Safelink, + HTMLRendererParameters: params, + }) } -func doTestsInlineParam(t *testing.T, tests []string, opts Options, htmlFlags HTMLFlags, params HTMLRendererParameters) { +func doTestsInlineParam(t *testing.T, tests []string, params TestParams) { // catch and report panics var candidate string defer func() {

@@ -120,7 +134,7 @@ for i := 0; i+1 < len(tests); i += 2 {

input := tests[i] candidate = input expected := tests[i+1] - actual := runMarkdownInline(candidate, opts, htmlFlags, params) + actual := runMarkdownInline(candidate, params) if actual != expected { t.Errorf("\nInput [%#v]\nExpected[%#v]\nActual [%#v]", candidate, expected, actual)

@@ -131,7 +145,7 @@ if !testing.Short() {

for start := 0; start < len(input); start++ { for end := start + 1; end <= len(input); end++ { candidate = input[start:end] - _ = runMarkdownInline(candidate, opts, htmlFlags, params) + runMarkdownInline(candidate, params) } } }