aboutsummaryrefslogtreecommitdiff
path: root/ki18n/ki18n_test.go
diff options
context:
space:
mode:
authorMartin Kagamino Lehoux <martin@lehoux.net>2025-12-30 21:03:53 +0100
committerMartin Kagamino Lehoux <martin@lehoux.net>2025-12-30 21:03:53 +0100
commit8a0c57a707a1d5e6ba75c0ca613b3e8b9e981326 (patch)
tree718e5abbf083114d60ab8bc8da088662ad37df36 /ki18n/ki18n_test.go
parent26d2799bc3ccc5fb556ac68d7a0bb6aabe69569f (diff)
Simplify testing
Diffstat (limited to 'ki18n/ki18n_test.go')
-rw-r--r--ki18n/ki18n_test.go37
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)
+}