Added File methods tests

This commit is contained in:
Maxim Lebedev 2023-11-10 17:14:13 +06:00
parent 44e00a35cb
commit 704e7af69a
Signed by: toby3d
GPG Key ID: 1F14E25B7C119FC5
1 changed files with 170 additions and 0 deletions

View File

@ -0,0 +1,170 @@
package domain_test
import (
"path/filepath"
"testing"
"golang.org/x/text/language"
"source.toby3d.me/toby3d/home/internal/domain"
)
var (
testRegularFile string = filepath.Join("news", "a.en.md")
testLeafFile string = filepath.Join("news", "b", "index.en.md")
testBranchFile string = filepath.Join("news", "_index.en.md")
)
func TestFile_BaseFileName(t *testing.T) {
t.Parallel()
for name, tc := range map[string]struct {
input, expect string
}{
"regular": {testRegularFile, "a.en"},
"leaf": {testLeafFile, "index.en"},
"branch": {testBranchFile, "_index.en"},
} {
name, tc := name, tc
t.Run(name, func(t *testing.T) {
t.Parallel()
if actual := domain.NewFile(tc.input).BaseFileName(); actual != tc.expect {
t.Errorf("BaseFileName() = '%s', want '%s'", actual, tc.expect)
}
})
}
}
func TestFile_ContentBaseName(t *testing.T) {
t.Parallel()
for name, tc := range map[string]struct {
input, expect string
}{
"regular": {testRegularFile, "a"},
"leaf": {testLeafFile, "b"},
"branch": {testBranchFile, "news"},
} {
name, tc := name, tc
t.Run(name, func(t *testing.T) {
t.Parallel()
if actual := domain.NewFile(tc.input).ContentBaseName(); actual != tc.expect {
t.Errorf("ContentBaseName() = '%s', want '%s'", actual, tc.expect)
}
})
}
}
func TestFile_Dir(t *testing.T) {
t.Parallel()
for name, tc := range map[string]struct {
input, expect string
}{
"regular": {testRegularFile, "news/"},
"leaf": {testLeafFile, "news/b/"},
"branch": {testBranchFile, "news/"},
} {
name, tc := name, tc
t.Run(name, func(t *testing.T) {
t.Parallel()
if actual := domain.NewFile(tc.input).Dir(); actual != tc.expect {
t.Errorf("Dir() = '%s', want '%s'", actual, tc.expect)
}
})
}
}
func TestFile_Ext(t *testing.T) {
t.Parallel()
const expect string = "md"
for name, input := range map[string]string{
"regular": testRegularFile,
"leaf": testLeafFile,
"branch": testBranchFile,
} {
name, input := name, input
t.Run(name, func(t *testing.T) {
t.Parallel()
if actual := domain.NewFile(input).Ext(); actual != expect {
t.Errorf("Ext() = '%s', want '%s'", actual, expect)
}
})
}
}
func TestFile_Language(t *testing.T) {
t.Parallel()
var expect language.Tag = language.English
for name, input := range map[string]string{
"regular": testRegularFile,
"leaf": testLeafFile,
"branch": testBranchFile,
} {
name, input := name, input
t.Run(name, func(t *testing.T) {
t.Parallel()
if actual := domain.NewFile(input).Language(); actual != expect {
t.Errorf("Language() = '%s', want '%s'", actual, expect)
}
})
}
}
func TestFile_LogicalName(t *testing.T) {
t.Parallel()
for name, tc := range map[string]struct {
input, expect string
}{
"regular": {testRegularFile, "a.en.md"},
"leaf": {testLeafFile, "index.en.md"},
"branch": {testBranchFile, "_index.en.md"},
} {
name, tc := name, tc
t.Run(name, func(t *testing.T) {
t.Parallel()
if actual := domain.NewFile(tc.input).LogicalName(); actual != tc.expect {
t.Errorf("LogicalName() = '%s', want '%s'", actual, tc.expect)
}
})
}
}
func TestFile_TranslationBaseName(t *testing.T) {
t.Parallel()
for name, tc := range map[string]struct {
input, expect string
}{
"regular": {testRegularFile, "a"},
"leaf": {testLeafFile, "index"},
"branch": {testBranchFile, "_index"},
} {
name, tc := name, tc
t.Run(name, func(t *testing.T) {
t.Parallel()
if actual := domain.NewFile(tc.input).TranslationBaseName(); actual != tc.expect {
t.Errorf("TranslationBaseName() = '%s', want '%s'", actual, tc.expect)
}
})
}
}