Skip to content

Commit

Permalink
Move test utilities to testutil/
Browse files Browse the repository at this point in the history
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.)
  • Loading branch information
abhinav committed Aug 25, 2019
1 parent f98eb98 commit 8c55e6f
Show file tree
Hide file tree
Showing 12 changed files with 50 additions and 28 deletions.
10 changes: 6 additions & 4 deletions commonmark_test.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package goldmark
package goldmark_test

import (
"encoding/json"
"io/ioutil"
"testing"

. "github.com/yuin/goldmark"
"github.com/yuin/goldmark/testutil"
"github.com/yuin/goldmark/renderer/html"
)

Expand All @@ -26,9 +28,9 @@ func TestSpec(t *testing.T) {
if err := json.Unmarshal(bs, &testCases); err != nil {
panic(err)
}
cases := []MarkdownTestCase{}
cases := []testutil.MarkdownTestCase{}
for _, c := range testCases {
cases = append(cases, MarkdownTestCase{
cases = append(cases, testutil.MarkdownTestCase{
No: c.Example,
Markdown: c.Markdown,
Expected: c.HTML,
Expand All @@ -38,5 +40,5 @@ func TestSpec(t *testing.T) {
html.WithXHTML(),
html.WithUnsafe(),
))
DoTestCases(markdown, cases, t)
testutil.DoTestCases(markdown, cases, t)
}
6 changes: 4 additions & 2 deletions extension/definition_list_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package extension

import (
"testing"

"github.com/yuin/goldmark"
"github.com/yuin/goldmark/testutil"
"github.com/yuin/goldmark/renderer/html"
"testing"
)

func TestDefinitionList(t *testing.T) {
Expand All @@ -15,5 +17,5 @@ func TestDefinitionList(t *testing.T) {
DefinitionList,
),
)
goldmark.DoTestCaseFile(markdown, "_test/definition_list.txt", t)
testutil.DoTestCaseFile(markdown, "_test/definition_list.txt", t)
}
6 changes: 4 additions & 2 deletions extension/footnote_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package extension

import (
"testing"

"github.com/yuin/goldmark"
"github.com/yuin/goldmark/testutil"
"github.com/yuin/goldmark/renderer/html"
"testing"
)

func TestFootnote(t *testing.T) {
Expand All @@ -15,5 +17,5 @@ func TestFootnote(t *testing.T) {
Footnote,
),
)
goldmark.DoTestCaseFile(markdown, "_test/footnote.txt", t)
testutil.DoTestCaseFile(markdown, "_test/footnote.txt", t)
}
6 changes: 4 additions & 2 deletions extension/linkify_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package extension

import (
"testing"

"github.com/yuin/goldmark"
"github.com/yuin/goldmark/testutil"
"github.com/yuin/goldmark/renderer/html"
"testing"
)

func TestLinkify(t *testing.T) {
Expand All @@ -15,5 +17,5 @@ func TestLinkify(t *testing.T) {
Linkify,
),
)
goldmark.DoTestCaseFile(markdown, "_test/linkify.txt", t)
testutil.DoTestCaseFile(markdown, "_test/linkify.txt", t)
}
6 changes: 4 additions & 2 deletions extension/strikethrough_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package extension

import (
"testing"

"github.com/yuin/goldmark"
"github.com/yuin/goldmark/testutil"
"github.com/yuin/goldmark/renderer/html"
"testing"
)

func TestStrikethrough(t *testing.T) {
Expand All @@ -15,5 +17,5 @@ func TestStrikethrough(t *testing.T) {
Strikethrough,
),
)
goldmark.DoTestCaseFile(markdown, "_test/strikethrough.txt", t)
testutil.DoTestCaseFile(markdown, "_test/strikethrough.txt", t)
}
6 changes: 4 additions & 2 deletions extension/table_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package extension

import (
"testing"

"github.com/yuin/goldmark"
"github.com/yuin/goldmark/testutil"
"github.com/yuin/goldmark/renderer/html"
"testing"
)

func TestTable(t *testing.T) {
Expand All @@ -15,5 +17,5 @@ func TestTable(t *testing.T) {
Table,
),
)
goldmark.DoTestCaseFile(markdown, "_test/table.txt", t)
testutil.DoTestCaseFile(markdown, "_test/table.txt", t)
}
6 changes: 4 additions & 2 deletions extension/tasklist_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package extension

import (
"testing"

"github.com/yuin/goldmark"
"github.com/yuin/goldmark/testutil"
"github.com/yuin/goldmark/renderer/html"
"testing"
)

func TestTaskList(t *testing.T) {
Expand All @@ -15,5 +17,5 @@ func TestTaskList(t *testing.T) {
TaskList,
),
)
goldmark.DoTestCaseFile(markdown, "_test/tasklist.txt", t)
testutil.DoTestCaseFile(markdown, "_test/tasklist.txt", t)
}
6 changes: 4 additions & 2 deletions extension/typographer_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package extension

import (
"testing"

"github.com/yuin/goldmark"
"github.com/yuin/goldmark/testutil"
"github.com/yuin/goldmark/renderer/html"
"testing"
)

func TestTypographer(t *testing.T) {
Expand All @@ -15,5 +17,5 @@ func TestTypographer(t *testing.T) {
Typographer,
),
)
goldmark.DoTestCaseFile(markdown, "_test/typographer.txt", t)
testutil.DoTestCaseFile(markdown, "_test/typographer.txt", t)
}
6 changes: 4 additions & 2 deletions extra_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package goldmark
package goldmark_test

import (
"testing"

. "github.com/yuin/goldmark"
"github.com/yuin/goldmark/testutil"
"github.com/yuin/goldmark/renderer/html"
)

Expand All @@ -11,5 +13,5 @@ func TestDefinitionList(t *testing.T) {
html.WithXHTML(),
html.WithUnsafe(),
))
DoTestCaseFile(markdown, "_test/extra.txt", t)
testutil.DoTestCaseFile(markdown, "_test/extra.txt", t)
}
9 changes: 6 additions & 3 deletions options_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package goldmark
package goldmark_test

import (
"github.com/yuin/goldmark/parser"
"testing"

. "github.com/yuin/goldmark"
"github.com/yuin/goldmark/testutil"
"github.com/yuin/goldmark/parser"
)

func TestAttributeAndAutoHeadingID(t *testing.T) {
Expand All @@ -12,5 +15,5 @@ func TestAttributeAndAutoHeadingID(t *testing.T) {
parser.WithAutoHeadingID(),
),
)
DoTestCaseFile(markdown, "_test/options.txt", t)
testutil.DoTestCaseFile(markdown, "_test/options.txt", t)
}
9 changes: 5 additions & 4 deletions testutil.go → testutil/testutil.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package goldmark
package testutil

import (
"bufio"
Expand All @@ -9,6 +9,7 @@ import (
"strconv"
"strings"

"github.com/yuin/goldmark"
"github.com/yuin/goldmark/util"
)

Expand All @@ -29,7 +30,7 @@ type MarkdownTestCase struct {
const attributeSeparator = "//- - - - - - - - -//"
const caseSeparator = "//= = = = = = = = = = = = = = = = = = = = = = = =//"

func DoTestCaseFile(m Markdown, filename string, t TestingT) {
func DoTestCaseFile(m goldmark.Markdown, filename string, t TestingT) {
fp, err := os.Open(filename)
if err != nil {
panic(err)
Expand Down Expand Up @@ -85,13 +86,13 @@ func DoTestCaseFile(m Markdown, filename string, t TestingT) {
DoTestCases(m, cases, t)
}

func DoTestCases(m Markdown, cases []MarkdownTestCase, t TestingT) {
func DoTestCases(m goldmark.Markdown, cases []MarkdownTestCase, t TestingT) {
for _, testCase := range cases {
DoTestCase(m, testCase, t)
}
}

func DoTestCase(m Markdown, testCase MarkdownTestCase, t TestingT) {
func DoTestCase(m goldmark.Markdown, testCase MarkdownTestCase, t TestingT) {
var ok bool
var out bytes.Buffer
defer func() {
Expand Down
2 changes: 1 addition & 1 deletion testutil_test.go → testutil/testutil_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package goldmark
package testutil

import "testing"

Expand Down

0 comments on commit 8c55e6f

Please sign in to comment.