hugolib, i18n: Update tests with flat format and TOML files

This commit is contained in:
Albert Nigmatzianov 2017-04-01 22:36:07 +02:00 committed by Bjørn Erik Pedersen
parent 28fdd71c29
commit 6b29bccfee
2 changed files with 20 additions and 23 deletions

View file

@ -1050,8 +1050,8 @@ func createMultiTestSitesForConfig(t *testing.T, siteConfig testSiteConfig, conf
if err := afero.WriteFile(mf, if err := afero.WriteFile(mf,
filepath.Join("i18n", "en.yaml"), filepath.Join("i18n", "en.yaml"),
[]byte(` []byte(`
- id: hello hello:
translation: "Hello" other: "Hello"
`), `),
0755); err != nil { 0755); err != nil {
t.Fatalf("Failed to write language file: %s", err) t.Fatalf("Failed to write language file: %s", err)
@ -1059,8 +1059,8 @@ func createMultiTestSitesForConfig(t *testing.T, siteConfig testSiteConfig, conf
if err := afero.WriteFile(mf, if err := afero.WriteFile(mf,
filepath.Join("i18n", "fr.yaml"), filepath.Join("i18n", "fr.yaml"),
[]byte(` []byte(`
- id: hello hello:
translation: "Bonjour" other: "Bonjour"
`), `),
0755); err != nil { 0755); err != nil {
t.Fatalf("Failed to write language file: %s", err) t.Fatalf("Failed to write language file: %s", err)

View file

@ -40,8 +40,8 @@ var i18nTests = []i18nTest{
// All translations present // All translations present
{ {
data: map[string][]byte{ data: map[string][]byte{
"en.yaml": []byte("- id: \"hello\"\n translation: \"Hello, World!\""), "en.toml": []byte("[hello]\nother = \"Hello, World!\""),
"es.yaml": []byte("- id: \"hello\"\n translation: \"¡Hola, Mundo!\""), "es.toml": []byte("[hello]\nother = \"¡Hola, Mundo!\""),
}, },
args: nil, args: nil,
lang: "es", lang: "es",
@ -52,8 +52,8 @@ var i18nTests = []i18nTest{
// Translation missing in current language but present in default // Translation missing in current language but present in default
{ {
data: map[string][]byte{ data: map[string][]byte{
"en.yaml": []byte("- id: \"hello\"\n translation: \"Hello, World!\""), "en.toml": []byte("[hello]\nother = \"Hello, World!\""),
"es.yaml": []byte("- id: \"goodbye\"\n translation: \"¡Adiós, Mundo!\""), "es.toml": []byte("[goodbye]\nother = \"¡Adiós, Mundo!\""),
}, },
args: nil, args: nil,
lang: "es", lang: "es",
@ -64,8 +64,8 @@ var i18nTests = []i18nTest{
// Translation missing in default language but present in current // Translation missing in default language but present in current
{ {
data: map[string][]byte{ data: map[string][]byte{
"en.yaml": []byte("- id: \"goodybe\"\n translation: \"Goodbye, World!\""), "en.toml": []byte("[goodybe]\nother = \"Goodbye, World!\""),
"es.yaml": []byte("- id: \"hello\"\n translation: \"¡Hola, Mundo!\""), "es.toml": []byte("[hello]\nother = \"¡Hola, Mundo!\""),
}, },
args: nil, args: nil,
lang: "es", lang: "es",
@ -76,8 +76,8 @@ var i18nTests = []i18nTest{
// Translation missing in both default and current language // Translation missing in both default and current language
{ {
data: map[string][]byte{ data: map[string][]byte{
"en.yaml": []byte("- id: \"goodbye\"\n translation: \"Goodbye, World!\""), "en.toml": []byte("[goodbye]\nother = \"Goodbye, World!\""),
"es.yaml": []byte("- id: \"goodbye\"\n translation: \"¡Adiós, Mundo!\""), "es.toml": []byte("[goodbye]\nother = \"¡Adiós, Mundo!\""),
}, },
args: nil, args: nil,
lang: "es", lang: "es",
@ -88,7 +88,7 @@ var i18nTests = []i18nTest{
// Default translation file missing or empty // Default translation file missing or empty
{ {
data: map[string][]byte{ data: map[string][]byte{
"en.yaml": []byte(""), "en.toml": []byte(""),
}, },
args: nil, args: nil,
lang: "es", lang: "es",
@ -99,8 +99,8 @@ var i18nTests = []i18nTest{
// Context provided // Context provided
{ {
data: map[string][]byte{ data: map[string][]byte{
"en.yaml": []byte("- id: \"wordCount\"\n translation: \"Hello, {{.WordCount}} people!\""), "en.toml": []byte("[wordCount]\nother = \"Hello, {{.WordCount}} people!\""),
"es.yaml": []byte("- id: \"wordCount\"\n translation: \"¡Hola, {{.WordCount}} gente!\""), "es.toml": []byte("[wordCount]\nother = \"¡Hola, {{.WordCount}} gente!\""),
}, },
args: struct { args: struct {
WordCount int WordCount int
@ -114,10 +114,10 @@ var i18nTests = []i18nTest{
}, },
} }
func doTestI18nTranslate(t *testing.T, data map[string][]byte, lang, id string, args interface{}, cfg config.Provider) string { func doTestI18nTranslate(t *testing.T, test i18nTest, cfg config.Provider) string {
i18nBundle := bundle.New() i18nBundle := bundle.New()
for file, content := range data { for file, content := range test.data {
err := i18nBundle.ParseTranslationFileBytes(file, content) err := i18nBundle.ParseTranslationFileBytes(file, content)
if err != nil { if err != nil {
t.Errorf("Error parsing translation file: %s", err) t.Errorf("Error parsing translation file: %s", err)
@ -125,11 +125,8 @@ func doTestI18nTranslate(t *testing.T, data map[string][]byte, lang, id string,
} }
translator := NewTranslator(i18nBundle, cfg, logger) translator := NewTranslator(i18nBundle, cfg, logger)
f := translator.Func(test.lang)
f := translator.Func(lang) translated := f(test.id, test.args)
translated := f(id, args)
return translated return translated
} }
@ -148,7 +145,7 @@ func TestI18nTranslate(t *testing.T) {
} else { } else {
expected = test.expected expected = test.expected
} }
actual = doTestI18nTranslate(t, test.data, test.lang, test.id, test.args, v) actual = doTestI18nTranslate(t, test, v)
require.Equal(t, expected, actual) require.Equal(t, expected, actual)
} }
} }