71 lines
1.9 KiB
Go
71 lines
1.9 KiB
Go
package fs_test
|
|
|
|
import (
|
|
"context"
|
|
"path"
|
|
"path/filepath"
|
|
"testing"
|
|
"testing/fstest"
|
|
|
|
"github.com/google/go-cmp/cmp"
|
|
"golang.org/x/text/language"
|
|
|
|
"source.toby3d.me/toby3d/home/internal/domain"
|
|
repository "source.toby3d.me/toby3d/home/internal/page/repository/fs"
|
|
)
|
|
|
|
func TestGet(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
testData := fstest.MapFS{
|
|
filepath.Join("both", "index.md"): &fstest.MapFile{Data: []byte("both/index.md")},
|
|
filepath.Join("both.md"): &fstest.MapFile{Data: []byte("both.md")},
|
|
filepath.Join("file.md"): &fstest.MapFile{Data: []byte("file.md")},
|
|
filepath.Join("folder", "index.md"): &fstest.MapFile{Data: []byte("folder/index.md")},
|
|
filepath.Join("index.md"): &fstest.MapFile{Data: []byte("index.md")},
|
|
}
|
|
|
|
repo := repository.NewFileSystemPageRepository(testData)
|
|
|
|
for name, tc := range map[string]struct {
|
|
expect *domain.Page
|
|
input string
|
|
}{
|
|
"index": {
|
|
input: path.Join("index"),
|
|
expect: &domain.Page{Content: []byte("index.md"), Resources: make([]*domain.Resource, 0)},
|
|
},
|
|
"file": {
|
|
input: path.Join("file"),
|
|
expect: &domain.Page{Content: []byte("file.md"), Resources: make([]*domain.Resource, 0)},
|
|
},
|
|
"folder": {
|
|
input: path.Join("folder", "index"),
|
|
expect: &domain.Page{Content: []byte("folder/index.md"), Resources: make([]*domain.Resource, 0)},
|
|
},
|
|
"both-file": {
|
|
input: path.Join("both"),
|
|
expect: &domain.Page{Content: []byte("both.md"), Resources: make([]*domain.Resource, 0)},
|
|
},
|
|
"both-folder": {
|
|
input: path.Join("both", "index"),
|
|
expect: &domain.Page{Content: []byte("both/index.md"), Resources: make([]*domain.Resource, 0)},
|
|
},
|
|
} {
|
|
name, tc := name, tc
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
out, err := repo.Get(context.Background(), language.Und, tc.input)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
if diff := cmp.Diff(out, tc.expect, cmp.AllowUnexported(language.Und)); diff != "" {
|
|
t.Error(diff)
|
|
}
|
|
})
|
|
}
|
|
}
|