Skip to content

Commit

Permalink
hugolib, i18n: Update tests with flat format and TOML files
Browse files Browse the repository at this point in the history
  • Loading branch information
n10v authored and bep committed Apr 2, 2017
1 parent 28fdd71 commit 6b29bcc
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 23 deletions.
8 changes: 4 additions & 4 deletions hugolib/hugo_sites_build_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1050,17 +1050,17 @@ func createMultiTestSitesForConfig(t *testing.T, siteConfig testSiteConfig, conf
if err := afero.WriteFile(mf,
filepath.Join("i18n", "en.yaml"),
[]byte(`
- id: hello
translation: "Hello"
hello:
other: "Hello"
`),
0755); err != nil {
t.Fatalf("Failed to write language file: %s", err)
}
if err := afero.WriteFile(mf,
filepath.Join("i18n", "fr.yaml"),
[]byte(`
- id: hello
translation: "Bonjour"
hello:
other: "Bonjour"
`),
0755); err != nil {
t.Fatalf("Failed to write language file: %s", err)
Expand Down
35 changes: 16 additions & 19 deletions i18n/i18n_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ var i18nTests = []i18nTest{
// All translations present
{
data: map[string][]byte{
"en.yaml": []byte("- id: \"hello\"\n translation: \"Hello, World!\""),
"es.yaml": []byte("- id: \"hello\"\n translation: \"¡Hola, Mundo!\""),
"en.toml": []byte("[hello]\nother = \"Hello, World!\""),
"es.toml": []byte("[hello]\nother = \"¡Hola, Mundo!\""),
},
args: nil,
lang: "es",
Expand All @@ -52,8 +52,8 @@ var i18nTests = []i18nTest{
// Translation missing in current language but present in default
{
data: map[string][]byte{
"en.yaml": []byte("- id: \"hello\"\n translation: \"Hello, World!\""),
"es.yaml": []byte("- id: \"goodbye\"\n translation: \"¡Adiós, Mundo!\""),
"en.toml": []byte("[hello]\nother = \"Hello, World!\""),
"es.toml": []byte("[goodbye]\nother = \"¡Adiós, Mundo!\""),
},
args: nil,
lang: "es",
Expand All @@ -64,8 +64,8 @@ var i18nTests = []i18nTest{
// Translation missing in default language but present in current
{
data: map[string][]byte{
"en.yaml": []byte("- id: \"goodybe\"\n translation: \"Goodbye, World!\""),
"es.yaml": []byte("- id: \"hello\"\n translation: \"¡Hola, Mundo!\""),
"en.toml": []byte("[goodybe]\nother = \"Goodbye, World!\""),
"es.toml": []byte("[hello]\nother = \"¡Hola, Mundo!\""),
},
args: nil,
lang: "es",
Expand All @@ -76,8 +76,8 @@ var i18nTests = []i18nTest{
// Translation missing in both default and current language
{
data: map[string][]byte{
"en.yaml": []byte("- id: \"goodbye\"\n translation: \"Goodbye, World!\""),
"es.yaml": []byte("- id: \"goodbye\"\n translation: \"¡Adiós, Mundo!\""),
"en.toml": []byte("[goodbye]\nother = \"Goodbye, World!\""),
"es.toml": []byte("[goodbye]\nother = \"¡Adiós, Mundo!\""),
},
args: nil,
lang: "es",
Expand All @@ -88,7 +88,7 @@ var i18nTests = []i18nTest{
// Default translation file missing or empty
{
data: map[string][]byte{
"en.yaml": []byte(""),
"en.toml": []byte(""),
},
args: nil,
lang: "es",
Expand All @@ -99,8 +99,8 @@ var i18nTests = []i18nTest{
// Context provided
{
data: map[string][]byte{
"en.yaml": []byte("- id: \"wordCount\"\n translation: \"Hello, {{.WordCount}} people!\""),
"es.yaml": []byte("- id: \"wordCount\"\n translation: \"¡Hola, {{.WordCount}} gente!\""),
"en.toml": []byte("[wordCount]\nother = \"Hello, {{.WordCount}} people!\""),
"es.toml": []byte("[wordCount]\nother = \"¡Hola, {{.WordCount}} gente!\""),
},
args: struct {
WordCount int
Expand All @@ -114,22 +114,19 @@ 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()

for file, content := range data {
for file, content := range test.data {
err := i18nBundle.ParseTranslationFileBytes(file, content)
if err != nil {
t.Errorf("Error parsing translation file: %s", err)
}
}

translator := NewTranslator(i18nBundle, cfg, logger)

f := translator.Func(lang)

translated := f(id, args)

f := translator.Func(test.lang)
translated := f(test.id, test.args)
return translated
}

Expand All @@ -148,7 +145,7 @@ func TestI18nTranslate(t *testing.T) {
} else {
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)
}
}
Expand Down

0 comments on commit 6b29bcc

Please sign in to comment.