2022-08-26 18:20:58 +02:00
|
|
|
package sheet
|
2019-11-06 12:27:02 +01:00
|
|
|
|
|
|
|
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
|
2022-08-26 18:20:58 +02:00
|
|
|
fm, text, err := parse(markdown)
|
2019-11-06 12:27:02 +01:00
|
|
|
|
|
|
|
// 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))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-13 10:36:00 +02:00
|
|
|
// TestHasNoFrontmatter asserts that markdown is properly parsed when it does not
|
2019-11-06 12:27:02 +01:00
|
|
|
// contain frontmatter
|
|
|
|
func TestHasNoFrontmatter(t *testing.T) {
|
|
|
|
|
|
|
|
// stub our cheatsheet content
|
|
|
|
markdown := "To foo the bar: baz"
|
|
|
|
|
|
|
|
// parse the frontmatter
|
2022-08-26 18:20:58 +02:00
|
|
|
fm, text, err := parse(markdown)
|
2019-11-06 12:27:02 +01:00
|
|
|
|
|
|
|
// 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))
|
|
|
|
}
|
|
|
|
}
|
2020-03-11 23:54:46 +01:00
|
|
|
|
|
|
|
// 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
|
2022-08-26 18:20:58 +02:00
|
|
|
_, text, err := parse(markdown)
|
2020-03-11 23:54:46 +01:00
|
|
|
|
|
|
|
// 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)
|
|
|
|
}
|
|
|
|
}
|