41 lines
797 B
Go
41 lines
797 B
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) {
|
|
fs := fstest.MapFS{
|
|
"hello world.md": {Data: []byte("Title: Post 1")},
|
|
"hello-world2.md": {Data: []byte("Title: Post 2")},
|
|
}
|
|
|
|
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))
|
|
}
|
|
|
|
got := posts[0]
|
|
want := Post{Title: "Post 1"}
|
|
|
|
if !reflect.DeepEqual(got, want) {
|
|
t.Errorf("got %+v, want %+v", got, want)
|
|
}
|
|
}
|