parser: Refactor frontmatter parser and add tests

Lots of cleanups here:

- Refactor InterfaceToConfig and InterfaceToFrontMatter to use io.Writer.
- Simplify InterfaceToFrontMatter by wrapping InterfaceToConfig.
- Export FrontmatterType since we return it in DetectFrontMatter.
- Refactor removeTOMLIdentifier to avoid blindly replacing "+++".
- Update HandleJSONMetaData to return an empty map on nil input.
- Updates vendored goorgeous package and test for org-mode frontmatter.
- Add tests and godoc comments.

Coverage for parser package increased from 45.2% to 85.2%.
This commit is contained in:
Cameron Moore 2016-12-26 15:23:20 -06:00 committed by Bjørn Erik Pedersen
parent ddc8cc0082
commit f039e3be9e
9 changed files with 552 additions and 91 deletions

View file

@ -46,17 +46,17 @@ func TestUndraftContent(t *testing.T) {
{yamlDraftFM, ""},
}
for _, test := range tests {
for i, test := range tests {
r := bytes.NewReader([]byte(test.fm))
p, _ := parser.ReadFrom(r)
res, err := undraftContent(p)
if test.expectedErr != "" {
if err == nil {
t.Error("Expected error, got none")
t.Error("[%d] Expected error, got none", i)
continue
}
if err.Error() != test.expectedErr {
t.Errorf("Expected %q, got %q", test.expectedErr, err)
t.Errorf("[%d] Expected %q, got %q", i, test.expectedErr, err)
continue
}
} else {
@ -64,19 +64,19 @@ func TestUndraftContent(t *testing.T) {
p, _ = parser.ReadFrom(r)
meta, err := p.Metadata()
if err != nil {
t.Errorf("unexpected error %q", err)
t.Errorf("[%d] unexpected error %q", i, err)
continue
}
for k, v := range meta.(map[string]interface{}) {
if k == "draft" {
if v.(bool) {
t.Errorf("Expected %q to be \"false\", got \"true\"", k)
t.Errorf("[%d] Expected %q to be \"false\", got \"true\"", i, k)
continue
}
}
if k == "date" {
if !strings.HasPrefix(v.(string), time.Now().Format("2006-01-02")) {
t.Errorf("Expected %v to start with %v", v.(string), time.Now().Format("2006-01-02"))
t.Errorf("[%d] Expected %v to start with %v", i, v.(string), time.Now().Format("2006-01-02"))
}
}
}