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: tdd, go --- Hello World` secondBody = `Title: Post 2 Description: Description 2 Tags: rust, borrow-checker --- B L M` ) 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{"tdd", "go"}, Body: `Hello World`, }) } func TestWrongFile(t *testing.T) { fs := fstest.MapFS{ "hello world.txt": {Data: []byte("Yolo")}, } _, err := NewPostsFromFS(fs) if err == nil { t.Errorf("should be an error but not") } } func assertPost(t testing.TB, got Post, want Post) { t.Helper() if !reflect.DeepEqual(got, want) { t.Errorf("got %+v, want %+v", got, want) } }