Skip to content

Commit

Permalink
Add case=x,x.. aruguments for tests
Browse files Browse the repository at this point in the history
  • Loading branch information
yuin committed Jul 2, 2020
1 parent 33089e2 commit feff0bb
Show file tree
Hide file tree
Showing 10 changed files with 39 additions and 11 deletions.
2 changes: 1 addition & 1 deletion extension/definition_list_test.go
Expand Up @@ -17,5 +17,5 @@ func TestDefinitionList(t *testing.T) {
DefinitionList,
),
)
testutil.DoTestCaseFile(markdown, "_test/definition_list.txt", t)
testutil.DoTestCaseFile(markdown, "_test/definition_list.txt", t, testutil.ParseCliCaseArg()...)
}
2 changes: 1 addition & 1 deletion extension/footnote_test.go
Expand Up @@ -17,5 +17,5 @@ func TestFootnote(t *testing.T) {
Footnote,
),
)
testutil.DoTestCaseFile(markdown, "_test/footnote.txt", t)
testutil.DoTestCaseFile(markdown, "_test/footnote.txt", t, testutil.ParseCliCaseArg()...)
}
2 changes: 1 addition & 1 deletion extension/linkify_test.go
Expand Up @@ -18,7 +18,7 @@ func TestLinkify(t *testing.T) {
Linkify,
),
)
testutil.DoTestCaseFile(markdown, "_test/linkify.txt", t)
testutil.DoTestCaseFile(markdown, "_test/linkify.txt", t, testutil.ParseCliCaseArg()...)
}

func TestLinkifyWithAllowedProtocols(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion extension/strikethrough_test.go
Expand Up @@ -17,5 +17,5 @@ func TestStrikethrough(t *testing.T) {
Strikethrough,
),
)
testutil.DoTestCaseFile(markdown, "_test/strikethrough.txt", t)
testutil.DoTestCaseFile(markdown, "_test/strikethrough.txt", t, testutil.ParseCliCaseArg()...)
}
2 changes: 1 addition & 1 deletion extension/table_test.go
Expand Up @@ -17,5 +17,5 @@ func TestTable(t *testing.T) {
Table,
),
)
testutil.DoTestCaseFile(markdown, "_test/table.txt", t)
testutil.DoTestCaseFile(markdown, "_test/table.txt", t, testutil.ParseCliCaseArg()...)
}
2 changes: 1 addition & 1 deletion extension/tasklist_test.go
Expand Up @@ -17,5 +17,5 @@ func TestTaskList(t *testing.T) {
TaskList,
),
)
testutil.DoTestCaseFile(markdown, "_test/tasklist.txt", t)
testutil.DoTestCaseFile(markdown, "_test/tasklist.txt", t, testutil.ParseCliCaseArg()...)
}
2 changes: 1 addition & 1 deletion extension/typographer_test.go
Expand Up @@ -17,5 +17,5 @@ func TestTypographer(t *testing.T) {
Typographer,
),
)
testutil.DoTestCaseFile(markdown, "_test/typographer.txt", t)
testutil.DoTestCaseFile(markdown, "_test/typographer.txt", t, testutil.ParseCliCaseArg()...)
}
2 changes: 1 addition & 1 deletion extra_test.go
Expand Up @@ -16,7 +16,7 @@ func TestExtras(t *testing.T) {
html.WithXHTML(),
html.WithUnsafe(),
))
testutil.DoTestCaseFile(markdown, "_test/extra.txt", t)
testutil.DoTestCaseFile(markdown, "_test/extra.txt", t, testutil.ParseCliCaseArg()...)
}

func TestEndsWithNonSpaceCharacters(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion options_test.go
Expand Up @@ -15,5 +15,5 @@ func TestAttributeAndAutoHeadingID(t *testing.T) {
parser.WithAutoHeadingID(),
),
)
testutil.DoTestCaseFile(markdown, "_test/options.txt", t)
testutil.DoTestCaseFile(markdown, "_test/options.txt", t, testutil.ParseCliCaseArg()...)
}
32 changes: 30 additions & 2 deletions testutil/testutil.go
Expand Up @@ -32,8 +32,25 @@ type MarkdownTestCase struct {
const attributeSeparator = "//- - - - - - - - -//"
const caseSeparator = "//= = = = = = = = = = = = = = = = = = = = = = = =//"

// ParseCliCaseArg parses -case command line args.
func ParseCliCaseArg() []int {
ret := []int{}
for _, a := range os.Args {
if strings.HasPrefix(a, "case=") {
parts := strings.Split(a, "=")
for _, cas := range strings.Split(parts[1], ",") {
value, err := strconv.Atoi(strings.TrimSpace(cas))
if err == nil {
ret = append(ret, value)
}
}
}
}
return ret
}

// DoTestCaseFile runs test cases in a given file.
func DoTestCaseFile(m goldmark.Markdown, filename string, t TestingT) {
func DoTestCaseFile(m goldmark.Markdown, filename string, t TestingT, no ...int) {
fp, err := os.Open(filename)
if err != nil {
panic(err)
Expand Down Expand Up @@ -93,7 +110,18 @@ func DoTestCaseFile(m goldmark.Markdown, filename string, t TestingT) {
buf = append(buf, text)
}
c.Expected = strings.Join(buf, "\n")
cases = append(cases, c)
shouldAdd := len(no) == 0
if !shouldAdd {
for _, n := range no {
if n == c.No {
shouldAdd = true
break
}
}
}
if shouldAdd {
cases = append(cases, c)
}
}
DoTestCases(m, cases, t)
}
Expand Down

0 comments on commit feff0bb

Please sign in to comment.