56 lines
1.1 KiB
Go
56 lines
1.1 KiB
Go
package blogposts
|
|
|
|
import (
|
|
"reflect"
|
|
"testing"
|
|
"testing/fstest"
|
|
)
|
|
|
|
// NOTE: This should be a black box test outside blogposts package.
|
|
|
|
// NOTE: If we want to test the return error
|
|
//
|
|
// type StubFailingFS struct{}
|
|
//
|
|
// func (s StubFailingFS) Open(name string) (fs.File, error) {
|
|
// return nil, errors.New("fail")
|
|
// }
|
|
|
|
func TestNewBlogPosts(t *testing.T) {
|
|
const (
|
|
firstBody = `Title: Post 1
|
|
Description: Description 1
|
|
Tags: tag1, tag2`
|
|
secondBody = `Title: Post 2
|
|
Description: Description 2
|
|
Tags: tag1, tag2`
|
|
)
|
|
|
|
fs := fstest.MapFS{
|
|
"hello world.md": {Data: []byte(firstBody)},
|
|
"hello-world2.md": {Data: []byte(secondBody)},
|
|
}
|
|
|
|
posts, err := NewPostsFromFS(fs)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
if len(posts) != len(fs) {
|
|
t.Errorf("got %d posts, wanted %d posts", len(posts), len(fs))
|
|
}
|
|
|
|
assertPost(t, posts[0], Post{
|
|
Title: "Post 1",
|
|
Description: "Description 1",
|
|
Tags: []string{"tag1", "tag2"},
|
|
})
|
|
}
|
|
|
|
func assertPost(t testing.TB, got Post, want Post) {
|
|
t.Helper()
|
|
if !reflect.DeepEqual(got, want) {
|
|
t.Errorf("got %+v, want %+v", got, want)
|
|
}
|
|
}
|