home/internal/domain/path_test.go

178 lines
4.0 KiB
Go

package domain_test
import (
"path/filepath"
"testing"
"source.toby3d.me/toby3d/home/internal/domain"
)
var (
testRegularPath string = filepath.Join("news", "a.en.md")
testLeafPath string = filepath.Join("news", "b", "index.en.md")
testBranchPath string = filepath.Join("news", "_index.en.md")
testResource string = filepath.Join("news", "b", "photo.jpg")
)
func TestPath_BaseFileName(t *testing.T) {
t.Parallel()
for name, tc := range map[string]struct {
input, expect string
}{
"regular": {testRegularPath, "a.en"},
"leaf": {testLeafPath, "index.en"},
"branch": {testBranchPath, "_index.en"},
"resource": {testResource, "photo"},
} {
name, tc := name, tc
t.Run(name, func(t *testing.T) {
t.Parallel()
if actual := domain.NewPath(tc.input).BaseFileName(); actual != tc.expect {
t.Errorf("BaseFileName() = '%s', want '%s'", actual, tc.expect)
}
})
}
}
func TestPath_ContentBaseName(t *testing.T) {
t.Parallel()
for name, tc := range map[string]struct {
input, expect string
}{
"regular": {testRegularPath, "a"},
"leaf": {testLeafPath, "b"},
"branch": {testBranchPath, "news"},
"resource": {testResource, "b"},
} {
name, tc := name, tc
t.Run(name, func(t *testing.T) {
t.Parallel()
if actual := domain.NewPath(tc.input).ContentBaseName(); actual != tc.expect {
t.Errorf("ContentBaseName() = '%s', want '%s'", actual, tc.expect)
}
})
}
}
func TestPath_Dir(t *testing.T) {
t.Parallel()
for name, tc := range map[string]struct {
input, expect string
}{
"regular": {testRegularPath, "news/"},
"leaf": {testLeafPath, "news/b/"},
"branch": {testBranchPath, "news/"},
"resource": {testResource, "news/b/"},
} {
name, tc := name, tc
t.Run(name, func(t *testing.T) {
t.Parallel()
if actual := domain.NewPath(tc.input).Dir(); actual != tc.expect {
t.Errorf("Dir() = '%s', want '%s'", actual, tc.expect)
}
})
}
}
func TestPath_Ext(t *testing.T) {
t.Parallel()
const expect string = "md"
for name, tc := range map[string]struct {
input, expect string
}{
"regular": {testRegularPath, "md"},
"leaf": {testLeafPath, "md"},
"branch": {testBranchPath, "md"},
"resource": {testResource, "jpg"},
} {
name, tc := name, tc
t.Run(name, func(t *testing.T) {
t.Parallel()
if actual := domain.NewPath(tc.input).Ext(); actual != tc.expect {
t.Errorf("Ext() = '%s', want '%s'", actual, tc.expect)
}
})
}
}
func TestPath_Language(t *testing.T) {
t.Parallel()
var expect domain.Language = domain.NewLanguage("en")
for name, input := range map[string]string{
"regular": testRegularPath,
"leaf": testLeafPath,
"branch": testBranchPath,
} {
name, input := name, input
t.Run(name, func(t *testing.T) {
t.Parallel()
if actual := domain.NewPath(input).Language; actual != expect {
t.Errorf("Language() = '%s', want '%s'", actual, expect)
}
})
}
}
func TestPath_LogicalName(t *testing.T) {
t.Parallel()
for name, tc := range map[string]struct {
input, expect string
}{
"regular": {testRegularPath, "a.en.md"},
"leaf": {testLeafPath, "index.en.md"},
"branch": {testBranchPath, "_index.en.md"},
"resource": {testResource, "photo.jpg"},
} {
name, tc := name, tc
t.Run(name, func(t *testing.T) {
t.Parallel()
if actual := domain.NewPath(tc.input).LogicalName(); actual != tc.expect {
t.Errorf("LogicalName() = '%s', want '%s'", actual, tc.expect)
}
})
}
}
func TestPath_TranslationBaseName(t *testing.T) {
t.Parallel()
for name, tc := range map[string]struct {
input, expect string
}{
"regular": {testRegularPath, "a"},
"leaf": {testLeafPath, "index"},
"branch": {testBranchPath, "_index"},
"resource": {testResource, "photo"},
} {
name, tc := name, tc
t.Run(name, func(t *testing.T) {
t.Parallel()
if actual := domain.NewPath(tc.input).TranslationBaseName(); actual != tc.expect {
t.Errorf("TranslationBaseName() = '%s', want '%s'", actual, tc.expect)
}
})
}
}