Add posts.ExtractTitle func

This commit is contained in:
Matt Baer
2018-10-10 15:16:54 -04:00
parent 73c7314873
commit 355476e321
2 changed files with 54 additions and 0 deletions
+35
View File
@@ -0,0 +1,35 @@
package posts
import (
"testing"
)
type titleTest struct {
in, title, body string
}
func TestExtractTitle(t *testing.T) {
tests := []titleTest{
{`# Hello World
This is my post`, "Hello World", "This is my post"},
{"No title", "", "No title"},
{`Not explicit title
It's not explicit.
Yep.`, "", `Not explicit title
It's not explicit.
Yep.`},
{"# Only a title", "", "# Only a title"},
}
for _, test := range tests {
title, body := ExtractTitle(test.in)
if title != test.title {
t.Fatalf("Wanted title '%s', got '%s'", test.title, title)
}
if body != test.body {
t.Fatalf("Wanted body '%s', got '%s'", test.body, body)
}
}
}