Skip to content

Commit

Permalink
fix merge issue, more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
gernotstarke committed Jan 3, 2025
1 parent 6b322c3 commit 8748abb
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 5 deletions.
9 changes: 9 additions & 0 deletions go-app/internal/domain/language.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,15 @@ var (
}
)

func IsLanguageSupported(tag language.Tag) bool {
for _, supportedTag := range supportedLanguages {
if tag == supportedTag {
return true
}
}
return false
}

// GetMatcher returns the language matcher for supported languages
func GetMatcher() language.Matcher {
return matcher
Expand Down
20 changes: 20 additions & 0 deletions go-app/internal/domain/language_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,26 @@ func TestUnsupportedSystemLanguageFallback(t *testing.T) {
assert.Equal(t, expectedTexts.BlankPageText, config.BlankPageText, "Blank page text should be in English")
}

func TestIsLanguageSupported(t *testing.T) {
tests := []struct {
lang language.Tag
want bool
}{
{language.German, true},
{language.English, true},
{language.French, true},
{language.Icelandic, false},
{language.Zulu, false},
}

for _, tt := range tests {
got := IsLanguageSupported(tt.lang)
if got != tt.want {
t.Errorf("IsLanguageSupported(%v) = %v, want %v", tt.lang, got, tt.want)
}
}
}

func TestParseLanguageCode(t *testing.T) {
tests := []struct {
name string
Expand Down
15 changes: 15 additions & 0 deletions go-app/internal/domain/minionTypes.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,23 @@ func NewDefaultConfig(systemLanguage language.Tag) *MinionConfig {
// MergeWith merges the current config with another config, giving precedence to the other config
func (c *MinionConfig) MergeWith(other *MinionConfig) error {
if other == nil {
// don't change c if other is nil
return nil
}
// handle language separately:
// if other language is set, use it for all language-specific fields
// and set these fields to language-specific defaults.
if other.Language.String() != "" {
c.Language = other.Language
if IsLanguageSupported(other.Language) {
texts := DefaultTexts[other.Language]
c.ChapterPrefix = texts.ChapterPrefix
c.RunningHeader = texts.RunningHeader
c.PagePrefix = texts.PageNumber
c.PageCountPrefix = texts.PageCountPrefix
c.BlankPageText = texts.BlankPageText
}
}

// Only override non-zero values
if other.ConfigFileName != "" {
Expand Down
49 changes: 44 additions & 5 deletions go-app/internal/domain/minionTypes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
)

func TestDefaultConfigIsEnglish(t *testing.T) {
// Setup a default config
base := NewDefaultConfig(language.English)

// the language specific values should be set to the English values
Expand All @@ -18,7 +17,6 @@ func TestDefaultConfigIsEnglish(t *testing.T) {
}

func TestDefaultConfigLanguageGerman(t *testing.T) {
// Setup a default config
base := NewDefaultConfig(language.German)

// the language specific values should be set to the German values
Expand All @@ -29,7 +27,6 @@ func TestDefaultConfigLanguageGerman(t *testing.T) {
}

func TestDefaultConfigLanguageFrench(t *testing.T) {
// Setup a default config
base := NewDefaultConfig(language.French)

assert.Equal(t, DefaultTexts[language.French].PageNumber, base.PagePrefix)
Expand All @@ -39,7 +36,7 @@ func TestDefaultConfigLanguageFrench(t *testing.T) {
}

func TestDefaultConfigLanguageUnknown(t *testing.T) {
// Setup a default config with an unknown language "Zulu"
// Set up a default config with an unknown language "Zulu"
base := NewDefaultConfig(language.Zulu)

// the language specific values should be set to the English values
Expand All @@ -49,11 +46,53 @@ func TestDefaultConfigLanguageUnknown(t *testing.T) {
assert.Equal(t, DefaultTexts[language.English].BlankPageText, base.BlankPageText)
}

// TestDefaultConfigMergeWithFlagEnglish merges an English default config with
// --language German. The language specific values should be set to the German (default)
func TestDefaultConfigMergeWithFlagGerman(t *testing.T) {
base := NewDefaultConfig(language.English)

german := &MinionConfig{
Language: language.German,
SetFields: map[string]bool{"language": true},
}

// can merge without error
assert.NoError(t, base.MergeWith(german), "MergeWith should not return an error")

// the language specific values should be set to the German values
// the language specific values should be set to the German values
assert.Equal(t, DefaultTexts[language.German].PageNumber, base.PagePrefix)
assert.Equal(t, DefaultTexts[language.German].ChapterPrefix, base.ChapterPrefix)
assert.Equal(t, DefaultTexts[language.German].PageCountPrefix, base.PageCountPrefix)
assert.Equal(t, DefaultTexts[language.German].BlankPageText, base.BlankPageText)
}

// TestDefaultConfigMergeWithFlagEnglish merges an English default config with
// --language French. The language specific values should be set to the German (default)
func TestDefaultConfigMergeWithFlagFrench(t *testing.T) {
base := NewDefaultConfig(language.English)

french := &MinionConfig{
Language: language.French,
SetFields: map[string]bool{"language": true},
}

// can merge without error
assert.NoError(t, base.MergeWith(french), "MergeWith should not return an error")

// the language specific values should be set to the German values
// the language specific values should be set to the German values
assert.Equal(t, DefaultTexts[language.French].PageNumber, base.PagePrefix)
assert.Equal(t, DefaultTexts[language.French].ChapterPrefix, base.ChapterPrefix)
assert.Equal(t, DefaultTexts[language.French].PageCountPrefix, base.PageCountPrefix)
assert.Equal(t, DefaultTexts[language.French].BlankPageText, base.BlankPageText)
}

func TestMinionConfig_MergeWithMinimal(t *testing.T) {
minimalBaseConfig := &MinionConfig{SourceDir: "/original/source"}
other := &MinionConfig{
SourceDir: "/new/source",
SetFields: map[string]bool{"Source": true},
SetFields: map[string]bool{"source": true},
}
assert.NoError(t, minimalBaseConfig.MergeWith(other), "MergeWith should not return an error")
assert.Equal(t, "/new/source", minimalBaseConfig.SourceDir)
Expand Down

0 comments on commit 8748abb

Please sign in to comment.