// // Blackfriday Markdown Processor // Available at http://github.com/russross/blackfriday // // Copyright © 2011 Russ Ross . // 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 runMarkdown(input string, params TestParams) string { params.HTMLRendererParameters.Flags = params.HTMLFlags params.HTMLRendererParameters.Extensions = params.Options.Extensions renderer := NewHTMLRenderer(params.HTMLRendererParameters) return string(Markdown([]byte(input), renderer, params.Options)) } func doTestsBlock(t *testing.T, tests []string, extensions Extensions) { doTestsParam(t, tests, TestParams{ Options: Options{Extensions: extensions}, HTMLFlags: UseXHTML, }) } func doTestsParam(t *testing.T, tests []string, params TestParams) { 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 := runMarkdown(*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] runMarkdown(*candidate, params) } } } } }) } 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) { params.Options.Extensions |= Autolink params.Options.Extensions |= Strikethrough params.HTMLFlags |= UseXHTML doTestsParam(t, tests, params) } func transformLinks(tests []string, prefix string) []string { newTests := make([]string, len(tests)) anchorRe := regexp.MustCompile(`