markdown/helpers_test.go

106 lines
2.9 KiB
Go
Raw Permalink Normal View History

2018-01-25 21:01:19 +00:00
package markdown
import (
"bytes"
2019-11-07 08:56:45 +00:00
"encoding/json"
"regexp"
2019-11-07 08:56:45 +00:00
"strings"
"testing"
"github.com/status-im/markdown/parser"
)
type TestParams struct {
2018-01-28 02:50:27 +00:00
extensions parser.Extensions
referenceOverride parser.ReferenceOverrideFunc
}
2019-11-07 08:56:45 +00:00
func runMarkdown(input string, params TestParams) (string, error) {
2018-01-28 03:45:55 +00:00
parser := parser.NewWithExtensions(params.extensions)
2018-01-26 08:11:58 +00:00
parser.ReferenceOverride = params.referenceOverride
2019-11-07 08:56:45 +00:00
result := parser.Parse([]byte(input))
if result == nil {
return "", nil
}
output, err := json.Marshal(result)
return string(output), err
}
// doTests runs full document tests using MarkdownCommon configuration.
func doTests(t *testing.T, tests []string) {
doTestsParam(t, tests, TestParams{
2018-01-28 02:50:27 +00:00
extensions: parser.CommonExtensions,
})
}
2018-01-28 02:50:27 +00:00
func doTestsBlock(t *testing.T, tests []string, extensions parser.Extensions) {
2016-04-04 11:00:26 +00:00
doTestsParam(t, tests, TestParams{
extensions: extensions,
2016-04-04 11:00:26 +00:00
})
}
2016-04-04 11:00:26 +00:00
func doTestsParam(t *testing.T, tests []string, params TestParams) {
2019-02-02 08:17:14 +00:00
for i := 0; i+1 < len(tests); i += 2 {
input := tests[i]
2019-11-07 08:56:45 +00:00
expected := strings.TrimRight(tests[i+1], "\n")
got, err := runMarkdown(input, params)
if err != nil {
t.Errorf("Failed to marshal json: %+v\n", err)
}
2019-02-02 08:17:14 +00:00
if got != expected {
2019-02-03 06:33:23 +00:00
t.Errorf("\nInput [%#v]\nExpected[%#v]\nGot [%#v]\nInput:\n%s\nExpected:\n%s\nGot:\n%s\n",
2019-02-02 08:17:14 +00:00
input, expected, got, input, expected, got)
}
2019-02-02 08:17:14 +00:00
}
}
func doTestsInline(t *testing.T, tests []string) {
doTestsInlineParam(t, tests, TestParams{})
}
func doLinkTestsInline(t *testing.T, tests []string) {
doTestsInline(t, tests)
prefix := "http://localhost"
transformTests := transformLinks(tests, prefix)
2019-11-07 08:56:45 +00:00
doTestsInlineParam(t, transformTests, TestParams{})
doTestsInlineParam(t, transformTests, TestParams{})
}
func doSafeTestsInline(t *testing.T, tests []string) {
2019-11-07 08:56:45 +00:00
doTestsInlineParam(t, tests, TestParams{})
// 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"
transformTests := transformLinks(tests, prefix)
2019-11-07 08:56:45 +00:00
doTestsInlineParam(t, transformTests, TestParams{})
}
func doTestsInlineParam(t *testing.T, tests []string, params TestParams) {
2018-01-28 02:50:27 +00:00
params.extensions |= parser.Autolink | parser.Strikethrough
2016-04-04 11:00:26 +00:00
doTestsParam(t, tests, params)
}
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 normalizeNewlines(d []byte) []byte {
// replace CR LF (windows) with LF (unix)
d = bytes.Replace(d, []byte{13, 10}, []byte{10}, -1)
// replace CF (mac) with LF (unix)
d = bytes.Replace(d, []byte{13}, []byte{10}, -1)
return d
}