handle whitespace errors
commit 65b1ac1fa4c7386909d834b073ce6ac9f3510331 · parent 8c1550b905
· browse this tree
Calvin Behling, 2022-12-01 19:19:14 UTC
diff --git a/read.go b/read.go index 6179b08..a45edc1 100644 --- a/read.go +++ b/read.go @@ -92,6 +92,9 @@ func Parse(data []byte) (*Document, error) { // If this line, after the sigil prefix, doesn't begin with "(" and end with ")", it's not a well-formed markdown comment, and you should probably be told about that. remainder := line[len(sigilTestmark):] if len(remainder) < 2 || remainder[0] != '(' || remainder[len(remainder)-1] != ')' { + if unicode.IsSpace(rune(remainder[len(remainder)-1])) { + return &doc, fmt.Errorf("invalid markdown comment on line %d (should look like %q; remove trailing whitespace)", i+1, "[testmark]:# (data-name-here)") + } return &doc, fmt.Errorf("invalid markdown comment on line %d (should look like %q, mind the parens)", i+1, "[testmark]:# (data-name-here)") } remainder = remainder[1 : len(remainder)-1] diff --git a/read_test.go b/read_test.go index 62de99f..8c6946f 100644 --- a/read_test.go +++ b/read_test.go @@ -2,6 +2,7 @@ package testmark_test import ( "bytes" + "fmt" "io/ioutil" "path/filepath" "regexp" @@ -56,6 +57,47 @@ func TestParseCRLF(t *testing.T) { readFixturesExample(t, doc) } +func TestParseSimple(t *testing.T) { + buf := bytes.NewBuffer([]byte{}) + fmt.Fprintln(buf, "[testmark]:# (simple)") + fmt.Fprintln(buf, "```") + fmt.Fprintln(buf, "Hello, World!") + fmt.Fprintln(buf, "```") + doc, err := testmark.Parse(buf.Bytes()) + if err != nil { + t.Fatal(err) + } + hunk := doc.HunksByName["simple"] + assert(t, hunk.Body, "Hello, World!\n") +} + +func TestParseTrailingWhitespace(t *testing.T) { + buf := bytes.NewBuffer([]byte{}) + fmt.Fprintln(buf, "[testmark]:# (trailing/whitespace) ") + fmt.Fprintln(buf, "```") + fmt.Fprintln(buf, "foo") + fmt.Fprintln(buf, "```") + doc, err := testmark.Parse(buf.Bytes()) + assert(t, err.Error(), `invalid markdown comment on line 1 (should look like "[testmark]:# (data-name-here)"; remove trailing whitespace)`) + _, exists := doc.HunksByName["trailing/whitespace"] + if exists { + t.Errorf("hunk should not exist") + } +} + +func TestParseTrailingExtraLineBreak(t *testing.T) { + buf := bytes.NewBuffer([]byte("[testmark]:# (extra/newline)\n\n")) + fmt.Fprintln(buf, "```") + fmt.Fprintln(buf, "foo") + fmt.Fprintln(buf, "```") + doc, err := testmark.Parse(buf.Bytes()) + if err != nil { + t.Fatal(err) + } + hunk := doc.HunksByName["extra/newline"] + assert(t, hunk.Body, "foo\n") +} + func TestDuplicateHunk(t *testing.T) { testdata, err := filepath.Abs("testdata") if err != nil {