mirror of
https://github.com/cheat/cheat.git
synced 2024-12-22 20:55:06 +01:00
5288bd0c1c
- Fix error in `frontmatter.Parse` which would cause a `panic` when encountering malformed frontmatter - Add a unit-test to cover the above
96 lines
2.1 KiB
Go
96 lines
2.1 KiB
Go
package frontmatter
|
|
|
|
import (
|
|
"testing"
|
|
)
|
|
|
|
// TestHasFrontmatter asserts that markdown is properly parsed when it contains
|
|
// frontmatter
|
|
func TestHasFrontmatter(t *testing.T) {
|
|
|
|
// stub our cheatsheet content
|
|
markdown := `---
|
|
syntax: go
|
|
tags: [ test ]
|
|
---
|
|
To foo the bar: baz`
|
|
|
|
// parse the frontmatter
|
|
text, fm, err := Parse(markdown)
|
|
|
|
// assert expectations
|
|
if err != nil {
|
|
t.Errorf("failed to parse markdown: %v", err)
|
|
}
|
|
|
|
want := "To foo the bar: baz"
|
|
if text != want {
|
|
t.Errorf("failed to parse text: want: %s, got: %s", want, text)
|
|
}
|
|
|
|
want = "go"
|
|
if fm.Syntax != want {
|
|
t.Errorf("failed to parse syntax: want: %s, got: %s", want, fm.Syntax)
|
|
}
|
|
|
|
want = "test"
|
|
if fm.Tags[0] != want {
|
|
t.Errorf("failed to parse tags: want: %s, got: %s", want, fm.Tags[0])
|
|
}
|
|
if len(fm.Tags) != 1 {
|
|
t.Errorf("failed to parse tags: want: len 0, got: len %d", len(fm.Tags))
|
|
}
|
|
}
|
|
|
|
// TestHasFrontmatter asserts that markdown is properly parsed when it does not
|
|
// contain frontmatter
|
|
func TestHasNoFrontmatter(t *testing.T) {
|
|
|
|
// stub our cheatsheet content
|
|
markdown := "To foo the bar: baz"
|
|
|
|
// parse the frontmatter
|
|
text, fm, err := Parse(markdown)
|
|
|
|
// assert expectations
|
|
if err != nil {
|
|
t.Errorf("failed to parse markdown: %v", err)
|
|
}
|
|
|
|
if text != markdown {
|
|
t.Errorf("failed to parse text: want: %s, got: %s", markdown, text)
|
|
}
|
|
|
|
if fm.Syntax != "" {
|
|
t.Errorf("failed to parse syntax: want: '', got: %s", fm.Syntax)
|
|
}
|
|
|
|
if len(fm.Tags) != 0 {
|
|
t.Errorf("failed to parse tags: want: len 0, got: len %d", len(fm.Tags))
|
|
}
|
|
}
|
|
|
|
// TestHasInvalidFrontmatter asserts that markdown is properly parsed when it
|
|
// contains invalid frontmatter
|
|
func TestHasInvalidFrontmatter(t *testing.T) {
|
|
|
|
// stub our cheatsheet content (with invalid frontmatter)
|
|
markdown := `---
|
|
syntax: go
|
|
tags: [ test ]
|
|
To foo the bar: baz`
|
|
|
|
// parse the frontmatter
|
|
text, _, err := Parse(markdown)
|
|
|
|
// assert that an error was returned
|
|
if err == nil {
|
|
t.Error("failed to error on invalid frontmatter")
|
|
}
|
|
|
|
// assert that the "raw" markdown was returned
|
|
if text != markdown {
|
|
t.Errorf("failed to parse text: want: %s, got: %s", markdown, text)
|
|
}
|
|
}
|