Test case now can have a description

This commit is contained in:
yuin 2020-05-22 18:42:44 +09:00
parent 4709d43b81
commit 3d78558cf2

View file

@ -23,9 +23,10 @@ type TestingT interface {
// MarkdownTestCase represents a test case. // MarkdownTestCase represents a test case.
type MarkdownTestCase struct { type MarkdownTestCase struct {
No int No int
Markdown string Description string
Expected string Markdown string
Expected string
} }
const attributeSeparator = "//- - - - - - - - -//" const attributeSeparator = "//- - - - - - - - -//"
@ -41,9 +42,10 @@ func DoTestCaseFile(m goldmark.Markdown, filename string, t TestingT) {
scanner := bufio.NewScanner(fp) scanner := bufio.NewScanner(fp)
c := MarkdownTestCase{ c := MarkdownTestCase{
No: -1, No: -1,
Markdown: "", Description: "",
Expected: "", Markdown: "",
Expected: "",
} }
cases := []MarkdownTestCase{} cases := []MarkdownTestCase{}
line := 0 line := 0
@ -52,7 +54,15 @@ func DoTestCaseFile(m goldmark.Markdown, filename string, t TestingT) {
if util.IsBlank([]byte(scanner.Text())) { if util.IsBlank([]byte(scanner.Text())) {
continue continue
} }
c.No, err = strconv.Atoi(scanner.Text()) header := scanner.Text()
c.Description = ""
if strings.Contains(header, ":") {
parts := strings.Split(header, ":")
c.No, err = strconv.Atoi(strings.TrimSpace(parts[0]))
c.Description = strings.Join(parts[1:], ":")
} else {
c.No, err = strconv.Atoi(scanner.Text())
}
if err != nil { if err != nil {
panic(fmt.Sprintf("%s: invalid case No at line %d", filename, line)) panic(fmt.Sprintf("%s: invalid case No at line %d", filename, line))
} }
@ -100,8 +110,12 @@ func DoTestCase(m goldmark.Markdown, testCase MarkdownTestCase, t TestingT) {
var ok bool var ok bool
var out bytes.Buffer var out bytes.Buffer
defer func() { defer func() {
description := ""
if len(testCase.Description) != 0 {
description = ": " + testCase.Description
}
if err := recover(); err != nil { if err := recover(); err != nil {
format := `============= case %d ================ format := `============= case %d%s ================
Markdown: Markdown:
----------- -----------
%s %s
@ -115,9 +129,9 @@ Actual
%v %v
%s %s
` `
t.Errorf(format, testCase.No, testCase.Markdown, testCase.Expected, err, debug.Stack()) t.Errorf(format, testCase.No, description, testCase.Markdown, testCase.Expected, err, debug.Stack())
} else if !ok { } else if !ok {
format := `============= case %d ================ format := `============= case %d%s ================
Markdown: Markdown:
----------- -----------
%s %s
@ -130,7 +144,7 @@ Actual
--------- ---------
%s %s
` `
t.Errorf(format, testCase.No, testCase.Markdown, testCase.Expected, out.Bytes()) t.Errorf(format, testCase.No, description, testCase.Markdown, testCase.Expected, out.Bytes())
} }
}() }()