2020-07-04 12:54:35 +02:00
|
|
|
package i18n
|
|
|
|
|
|
|
|
import (
|
2020-07-15 01:26:54 +02:00
|
|
|
"os"
|
2020-07-04 12:54:35 +02:00
|
|
|
"testing"
|
|
|
|
|
2020-07-15 01:26:54 +02:00
|
|
|
"github.com/leonelquinteros/gotext"
|
2020-07-04 12:54:35 +02:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
2020-07-15 01:26:54 +02:00
|
|
|
func TestMain(m *testing.M) {
|
|
|
|
gotext.Configure(localeDir, string(locale), "default")
|
|
|
|
|
|
|
|
code := m.Run()
|
|
|
|
|
|
|
|
os.Exit(code)
|
|
|
|
}
|
|
|
|
|
2020-07-04 12:54:35 +02:00
|
|
|
func TestMsg(t *testing.T) {
|
|
|
|
t.Run("already exists", func(t *testing.T) {
|
|
|
|
msg := Msg(ErrAlreadyExists, "A cat")
|
|
|
|
assert.Equal(t, "A cat already exists", msg)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("unexpected error", func(t *testing.T) {
|
|
|
|
msg := Msg(ErrUnexpected, "A cat")
|
2023-02-09 14:17:16 +01:00
|
|
|
assert.Equal(t, "Something went wrong, try again", msg)
|
2020-07-04 12:54:35 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("already exists german", func(t *testing.T) {
|
2020-07-15 01:26:54 +02:00
|
|
|
SetLocale("de")
|
2020-12-16 13:27:44 +01:00
|
|
|
msgTrans := Msg(ErrAlreadyExists, "Eine Katze")
|
|
|
|
assert.Equal(t, "Eine Katze existiert bereits", msgTrans)
|
2020-07-15 01:26:54 +02:00
|
|
|
SetLocale("")
|
2020-07-04 12:54:35 +02:00
|
|
|
msgDefault := Msg(ErrAlreadyExists, "A cat")
|
|
|
|
assert.Equal(t, "A cat already exists", msgDefault)
|
|
|
|
})
|
|
|
|
|
2020-07-15 01:26:54 +02:00
|
|
|
t.Run("already exists polish", func(t *testing.T) {
|
|
|
|
SetLocale("pl")
|
2020-12-16 13:27:44 +01:00
|
|
|
msgTrans := Msg(ErrAlreadyExists, "Kot")
|
|
|
|
assert.Equal(t, "Kot już istnieje", msgTrans)
|
|
|
|
SetLocale("")
|
|
|
|
msgDefault := Msg(ErrAlreadyExists, "A cat")
|
|
|
|
assert.Equal(t, "A cat already exists", msgDefault)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("Brazilian Portuguese", func(t *testing.T) {
|
|
|
|
SetLocale("pt_BR")
|
|
|
|
msgTrans := Msg(ErrAlreadyExists, "Gata")
|
|
|
|
assert.Equal(t, "Gata já existe", msgTrans)
|
2020-07-15 01:26:54 +02:00
|
|
|
SetLocale("")
|
|
|
|
msgDefault := Msg(ErrAlreadyExists, "A cat")
|
2020-07-04 12:54:35 +02:00
|
|
|
assert.Equal(t, "A cat already exists", msgDefault)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestError(t *testing.T) {
|
|
|
|
t.Run("already exists", func(t *testing.T) {
|
|
|
|
err := Error(ErrAlreadyExists, "A cat")
|
2022-07-19 16:58:43 +02:00
|
|
|
assert.EqualError(t, err, "A cat already exists")
|
2020-07-04 12:54:35 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("unexpected error", func(t *testing.T) {
|
|
|
|
err := Error(ErrUnexpected, "A cat")
|
2023-02-09 14:17:16 +01:00
|
|
|
assert.EqualError(t, err, "Something went wrong, try again")
|
2020-07-04 12:54:35 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("already exists german", func(t *testing.T) {
|
2020-07-15 01:26:54 +02:00
|
|
|
SetLocale("de")
|
2020-07-04 12:54:35 +02:00
|
|
|
errGerman := Error(ErrAlreadyExists, "Eine Katze")
|
2022-07-19 16:58:43 +02:00
|
|
|
assert.EqualError(t, errGerman, "Eine Katze existiert bereits")
|
2020-07-15 01:26:54 +02:00
|
|
|
SetLocale("")
|
2020-07-04 12:54:35 +02:00
|
|
|
errDefault := Error(ErrAlreadyExists, "A cat")
|
2022-07-19 16:58:43 +02:00
|
|
|
assert.EqualError(t, errDefault, "A cat already exists")
|
2020-07-04 12:54:35 +02:00
|
|
|
})
|
|
|
|
}
|
2023-03-15 18:19:03 +01:00
|
|
|
|
|
|
|
func TestLower(t *testing.T) {
|
|
|
|
t.Run("ErrAlreadyExists", func(t *testing.T) {
|
|
|
|
msg := Lower(ErrAlreadyExists, "Eine Katze")
|
|
|
|
assert.Equal(t, "eine katze already exists", msg)
|
|
|
|
SetLocale("de")
|
|
|
|
errGerman := Lower(ErrAlreadyExists, "Eine Katze")
|
|
|
|
assert.Equal(t, errGerman, "eine katze already exists")
|
|
|
|
SetLocale("")
|
|
|
|
errDefault := Lower(ErrAlreadyExists, "Eine Katze")
|
|
|
|
assert.Equal(t, errDefault, "eine katze already exists")
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("ErrForbidden", func(t *testing.T) {
|
|
|
|
msg := Lower(ErrForbidden, "A cat")
|
|
|
|
assert.Equal(t, "permission denied", msg)
|
|
|
|
})
|
|
|
|
}
|