From 39af355deb3549e6c4ebd2c56c7a92bcc40f362e Mon Sep 17 00:00:00 2001 From: facundoolano Date: Sat, 10 Feb 2024 12:30:09 -0300 Subject: [PATCH] more front matter tests --- commands/templates_test.go | 53 +++++++++++++++++++++++++++++++++----- 1 file changed, 46 insertions(+), 7 deletions(-) diff --git a/commands/templates_test.go b/commands/templates_test.go index 2dab12a..3df1e59 100644 --- a/commands/templates_test.go +++ b/commands/templates_test.go @@ -22,16 +22,55 @@ tags: ["software", "web"] assertEqual(t, yaml["tags"].([]interface{})[1], "web") } -func TestInvalidFrontMatterDelimiter(t *testing.T) { - // TODO +func TestNonFrontMatterDelimiter(t *testing.T) { + // not identified as front matter, leaving file as is + input := `+++ +title: my new post +subtitle: a blog post ++++ +

Hello World!

` + + out, yaml, err := extractFrontMatter(strings.NewReader(input)) + + assertEqual(t, string(out), input) + assertEqual(t, err, nil) + assertEqual(t, len(yaml), 0) + + // not first thing in file, leaving as is + input = `#+OPTIONS: toc:nil num:nil +--- +title: my new post +subtitle: a blog post +tags: ["software", "web"] +--- +

Hello World!

` + + out, yaml, err = extractFrontMatter(strings.NewReader(input)) + + assertEqual(t, string(out), input) + assertEqual(t, err, nil) + assertEqual(t, len(yaml), 0) } func TestInvalidFrontMatterYaml(t *testing.T) { - // TODO -} + input := `--- +title: my new post +subtitle: a blog post +tags: ["software", "web"] +` -func TestFrontMatterNotAtTop(t *testing.T) { - // TODO + _, _, err := extractFrontMatter(strings.NewReader(input)) + assertEqual(t, err.Error(), "front matter not closed") + + input = `--- +title +tags: ["software", "web"] +--- +

Hello World!

` + + _, _, err = extractFrontMatter(strings.NewReader(input)) + msg := strings.Split(err.Error(), ":")[0] + assertEqual(t, msg, "invalid yaml") } func TestRenderHtml(t *testing.T) { @@ -45,6 +84,6 @@ func TestRenderOrg(t *testing.T) { // TODO move to assert package func assertEqual(t *testing.T, a interface{}, b interface{}) { if a != b { - t.Fatalf("%s != %s", a, b) + t.Fatalf("%v != %v", a, b) } }