diff options
| author | Martin Kagamino Lehoux <martin@lehoux.net> | 2025-12-30 21:03:53 +0100 |
|---|---|---|
| committer | Martin Kagamino Lehoux <martin@lehoux.net> | 2025-12-30 21:03:53 +0100 |
| commit | 8a0c57a707a1d5e6ba75c0ca613b3e8b9e981326 (patch) | |
| tree | 718e5abbf083114d60ab8bc8da088662ad37df36 | |
| parent | 26d2799bc3ccc5fb556ac68d7a0bb6aabe69569f (diff) | |
Simplify testing
| -rw-r--r-- | ki18n/ki18n_test.go | 37 |
1 files changed, 25 insertions, 12 deletions
diff --git a/ki18n/ki18n_test.go b/ki18n/ki18n_test.go index fc134fa..6d1395e 100644 --- a/ki18n/ki18n_test.go +++ b/ki18n/ki18n_test.go @@ -3,6 +3,8 @@ package ki18n import ( "bytes" "context" + "fmt" + "strings" "testing" "testing/fstest" @@ -10,12 +12,10 @@ import ( ) func TestSimpleText(t *testing.T) { - fs := fstest.MapFS{ - "fr-FR/index.yml": &fstest.MapFile{ - Data: []byte("\"Hello you\": \"Bonjour toi\""), - }, - } - Init(fs) + initFrenchLocale(map[string]string{ + "Hello you": "Bonjour toi", + }) + c := Tr("fr-FR", "Hello you") w := &bytes.Buffer{} err := c.Render(context.Background(), w) @@ -24,15 +24,28 @@ func TestSimpleText(t *testing.T) { } func TestVariables(t *testing.T) { - fs := fstest.MapFS{ - "fr-FR/index.yml": &fstest.MapFile{ - Data: []byte("\"Hello %s\": \"Bonjour %s\""), - }, - } - Init(fs) + initFrenchLocale(map[string]string{ + "Hello %s": "Bonjour %s", + }) + c := Tr("fr-FR", "Hello %s", "John") w := &bytes.Buffer{} err := c.Render(context.Background(), w) assert.NoError(t, err) assert.Equal(t, "Bonjour John", w.String()) } + +// initFrenchLocale creates a filesystem with fr-FR locale from a translation map +func initFrenchLocale(translations map[string]string) { + var sb strings.Builder + for key, value := range translations { + fmt.Fprintf(&sb, "%q: %q\n", key, value) + } + + fs := fstest.MapFS{ + "fr-FR/index.yml": &fstest.MapFile{ + Data: []byte(sb.String()), + }, + } + Init(fs) +} |
