goldmark/commonmark_test.go
Abhinav Gupta 8c55e6fa9c Move test utilities to testutil/
This moves the following functions meant for use from tests into a
testutil subpackage.

    func DoTestCase(m Markdown, testCase MarkdownTestCase, t TestingT)
    func DoTestCaseFile(m Markdown, filename string, t TestingT)
    func DoTestCases(m goldmark.Markdown, cases []MarkdownTestCase, t TestingT)

This will help keep the top-level goldmark package clean and limited to
core functionality.

(Note that tests in the top-level goldmark package that make use of
these functions must now use the package name `goldmark_test` so that
they're considered separate from the main `goldmark` package, otherwise
you'll see an import cycle: goldmark imports testutil imports goldmark.)
2019-08-25 03:18:18 -07:00

44 lines
987 B
Go

package goldmark_test
import (
"encoding/json"
"io/ioutil"
"testing"
. "github.com/yuin/goldmark"
"github.com/yuin/goldmark/testutil"
"github.com/yuin/goldmark/renderer/html"
)
type commonmarkSpecTestCase struct {
Markdown string `json:"markdown"`
HTML string `json:"html"`
Example int `json:"example"`
StartLine int `json:"start_line"`
EndLine int `json:"end_line"`
Section string `json:"section"`
}
func TestSpec(t *testing.T) {
bs, err := ioutil.ReadFile("_test/spec.json")
if err != nil {
panic(err)
}
var testCases []commonmarkSpecTestCase
if err := json.Unmarshal(bs, &testCases); err != nil {
panic(err)
}
cases := []testutil.MarkdownTestCase{}
for _, c := range testCases {
cases = append(cases, testutil.MarkdownTestCase{
No: c.Example,
Markdown: c.Markdown,
Expected: c.HTML,
})
}
markdown := New(WithRendererOptions(
html.WithXHTML(),
html.WithUnsafe(),
))
testutil.DoTestCases(markdown, cases, t)
}