191 lines
5.1 KiB
Go
191 lines
5.1 KiB
Go
package translation_test
|
|
|
|
import (
|
|
"os"
|
|
"testing"
|
|
|
|
"git.apihub24.de/admin/translation"
|
|
exampletranslations "git.apihub24.de/admin/translation/example_translations"
|
|
)
|
|
|
|
const (
|
|
paramTranslationValue = "tlv"
|
|
)
|
|
|
|
type testCaseError struct {
|
|
format string
|
|
params []string
|
|
}
|
|
|
|
type testCase struct {
|
|
name string
|
|
fallbackCulture string
|
|
keys []string
|
|
keyParams [][]string
|
|
cultures []string
|
|
expectedValues []string
|
|
errorMessages []testCaseError
|
|
}
|
|
|
|
func getTestCases() []testCase {
|
|
return []testCase{
|
|
{
|
|
name: "Test Translation.Init",
|
|
fallbackCulture: "",
|
|
keys: []string{"KEY"},
|
|
keyParams: [][]string{},
|
|
cultures: []string{"de"},
|
|
expectedValues: []string{"WERT"},
|
|
errorMessages: []testCaseError{
|
|
{
|
|
format: "translations not initialized!",
|
|
params: []string{},
|
|
},
|
|
},
|
|
},
|
|
{
|
|
name: "Test Translation.Get de",
|
|
fallbackCulture: "",
|
|
keys: []string{"KEY"},
|
|
keyParams: [][]string{},
|
|
cultures: []string{"de"},
|
|
expectedValues: []string{"WERT"},
|
|
errorMessages: []testCaseError{
|
|
{
|
|
format: "expect German KEY to have Value 'WERT' but was %[1]s",
|
|
params: []string{paramTranslationValue},
|
|
},
|
|
},
|
|
},
|
|
{
|
|
name: "Test Translation.Get en",
|
|
fallbackCulture: "",
|
|
keys: []string{"KEY"},
|
|
keyParams: [][]string{},
|
|
cultures: []string{"en"},
|
|
expectedValues: []string{"VALUE"},
|
|
errorMessages: []testCaseError{
|
|
{
|
|
format: "expect English KEY to have Value 'VALUE' but was %[1]s",
|
|
params: []string{paramTranslationValue},
|
|
},
|
|
},
|
|
},
|
|
{
|
|
name: "Test Translation.Get fr",
|
|
fallbackCulture: "",
|
|
keys: []string{"KEY"},
|
|
keyParams: [][]string{},
|
|
cultures: []string{"fr"},
|
|
expectedValues: []string{"VALEUR"},
|
|
errorMessages: []testCaseError{
|
|
{
|
|
format: "expect France KEY to have Value 'VALEUR' but was %[1]s",
|
|
params: []string{paramTranslationValue},
|
|
},
|
|
},
|
|
},
|
|
{
|
|
name: "Test Translation.Get missing Culture Fallback to English",
|
|
fallbackCulture: "",
|
|
keys: []string{"KEY"},
|
|
keyParams: [][]string{},
|
|
cultures: []string{"notexists"},
|
|
expectedValues: []string{"VALUE"},
|
|
errorMessages: []testCaseError{
|
|
{
|
|
format: "expect Fallback KEY to have Value 'VALUE' but was %[1]s",
|
|
params: []string{paramTranslationValue},
|
|
},
|
|
},
|
|
},
|
|
{
|
|
name: "Test Translation.Get can change Fallback to German",
|
|
fallbackCulture: "de",
|
|
keys: []string{"KEY"},
|
|
keyParams: [][]string{},
|
|
cultures: []string{"notexists"},
|
|
expectedValues: []string{"WERT"},
|
|
errorMessages: []testCaseError{
|
|
{
|
|
format: "expect Fallback KEY to have Value 'WERT' but was %[1]s",
|
|
params: []string{paramTranslationValue},
|
|
},
|
|
},
|
|
},
|
|
{
|
|
name: "Test Translation.Get return Error message when Key not exists",
|
|
fallbackCulture: "",
|
|
keys: []string{"notexists"},
|
|
keyParams: [][]string{},
|
|
cultures: []string{"de"},
|
|
expectedValues: []string{"no value for key notexists found in source de"},
|
|
errorMessages: []testCaseError{
|
|
{
|
|
format: "expect to get the missing key and culture but get: %[1]s",
|
|
params: []string{paramTranslationValue},
|
|
},
|
|
},
|
|
},
|
|
{
|
|
name: "Test Translation.Get with Parameters",
|
|
fallbackCulture: "",
|
|
keys: []string{"WITH_PARAM", "WITH_PARAM", "WITH_PARAM"},
|
|
keyParams: [][]string{{"a", "b"}, {"a", "b"}, {"a", "b"}},
|
|
cultures: []string{"de", "en", "fr"},
|
|
expectedValues: []string{"WERT a_b", "VALUE a_b", "VALEUR a_b"},
|
|
errorMessages: []testCaseError{
|
|
{
|
|
format: "expect German KEY to have Value 'WERT a_b' but was %[1]s",
|
|
params: []string{paramTranslationValue},
|
|
},
|
|
{
|
|
format: "expect English KEY to have Value 'VALUE a_b' but was %[1]s",
|
|
params: []string{paramTranslationValue},
|
|
},
|
|
{
|
|
format: "expect France KEY to have Value 'VALEUR a_b' but was %[1]s",
|
|
params: []string{paramTranslationValue},
|
|
},
|
|
},
|
|
},
|
|
}
|
|
}
|
|
|
|
func TestMain(m *testing.M) {
|
|
translation.Init(exampletranslations.Files)
|
|
code := m.Run()
|
|
os.Exit(code)
|
|
}
|
|
|
|
func Test(t *testing.T) {
|
|
for _, tc := range getTestCases() {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
if len(tc.fallbackCulture) > 0 {
|
|
translation.SetDefaultCulture(tc.fallbackCulture)
|
|
}
|
|
for idx := range tc.keys {
|
|
value := translation.Get(tc.keys[idx], tc.cultures[idx])
|
|
if idx < len(tc.keyParams) {
|
|
value = translation.Get(tc.keys[idx], tc.cultures[idx], tc.keyParams[idx]...)
|
|
}
|
|
if len(tc.fallbackCulture) > 0 {
|
|
translation.SetDefaultCulture("en")
|
|
}
|
|
if value != tc.expectedValues[idx] {
|
|
params := make([]any, 0)
|
|
for _, key := range tc.errorMessages[idx].params {
|
|
if key == paramTranslationValue {
|
|
params = append(params, value)
|
|
continue
|
|
}
|
|
params = append(params, key)
|
|
}
|
|
t.Errorf(tc.errorMessages[idx].format, params...)
|
|
return
|
|
}
|
|
}
|
|
})
|
|
}
|
|
}
|