2018-08-20 08:53:13 +02:00
|
|
|
package i18n
|
|
|
|
|
|
|
|
import (
|
2018-08-20 21:04:04 +02:00
|
|
|
"fmt"
|
2018-08-26 07:46:18 +02:00
|
|
|
"io/ioutil"
|
2018-08-20 08:53:13 +02:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/nicksnyder/go-i18n/v2/i18n"
|
|
|
|
|
2018-08-23 14:22:03 +02:00
|
|
|
"github.com/sirupsen/logrus"
|
2018-08-20 08:53:13 +02:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
2018-08-26 07:46:18 +02:00
|
|
|
func getDummyLog() *logrus.Entry {
|
|
|
|
log := logrus.New()
|
|
|
|
log.Out = ioutil.Discard
|
|
|
|
return log.WithField("test", "test")
|
|
|
|
}
|
2018-11-30 02:47:14 +02:00
|
|
|
|
|
|
|
// TestNewLocalizer is a function.
|
2018-08-20 08:53:13 +02:00
|
|
|
func TestNewLocalizer(t *testing.T) {
|
2018-08-26 07:46:18 +02:00
|
|
|
assert.NotNil(t, NewLocalizer(getDummyLog()))
|
2018-08-20 21:04:04 +02:00
|
|
|
}
|
|
|
|
|
2018-11-30 02:47:14 +02:00
|
|
|
// TestDetectLanguage is a function.
|
2018-08-20 21:04:04 +02:00
|
|
|
func TestDetectLanguage(t *testing.T) {
|
2018-08-20 08:53:13 +02:00
|
|
|
type scenario struct {
|
2018-08-20 21:04:04 +02:00
|
|
|
langDetector func() (string, error)
|
|
|
|
expected string
|
2018-08-20 08:53:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
scenarios := []scenario{
|
|
|
|
{
|
2018-08-20 21:04:04 +02:00
|
|
|
func() (string, error) {
|
|
|
|
return "", fmt.Errorf("An error occurred")
|
2018-08-20 08:53:13 +02:00
|
|
|
},
|
2018-08-20 21:04:04 +02:00
|
|
|
"C",
|
2018-08-20 08:53:13 +02:00
|
|
|
},
|
|
|
|
{
|
2018-08-20 21:04:04 +02:00
|
|
|
func() (string, error) {
|
|
|
|
return "en", nil
|
2018-08-20 08:53:13 +02:00
|
|
|
},
|
2018-08-20 21:04:04 +02:00
|
|
|
"en",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, s := range scenarios {
|
|
|
|
assert.EqualValues(t, s.expected, detectLanguage(s.langDetector))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-30 02:47:14 +02:00
|
|
|
// TestLocalizer is a function.
|
2018-08-20 21:04:04 +02:00
|
|
|
func TestLocalizer(t *testing.T) {
|
|
|
|
type scenario struct {
|
|
|
|
userLang string
|
|
|
|
test func(*Localizer)
|
|
|
|
}
|
2018-08-20 08:53:13 +02:00
|
|
|
|
2018-08-20 21:04:04 +02:00
|
|
|
scenarios := []scenario{
|
|
|
|
{
|
|
|
|
"C",
|
|
|
|
func(l *Localizer) {
|
|
|
|
assert.EqualValues(t, "C", l.GetLanguage())
|
2018-08-20 08:53:13 +02:00
|
|
|
assert.Equal(t, "Diff", l.Localize(&i18n.LocalizeConfig{
|
|
|
|
DefaultMessage: &i18n.Message{
|
|
|
|
ID: "DiffTitle",
|
|
|
|
},
|
|
|
|
}))
|
|
|
|
assert.Equal(t, "Diff", l.SLocalize("DiffTitle"))
|
2018-08-22 17:27:45 +02:00
|
|
|
assert.Equal(t, "Are you sure you want to delete the branch test?", l.TemplateLocalize("DeleteBranchMessage", Teml{"selectedBranchName": "test"}))
|
2018-08-20 08:53:13 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2018-08-20 21:04:04 +02:00
|
|
|
"nl",
|
|
|
|
func(l *Localizer) {
|
2018-08-20 08:53:13 +02:00
|
|
|
assert.EqualValues(t, "nl", l.GetLanguage())
|
|
|
|
assert.Equal(t, "Diff", l.Localize(&i18n.LocalizeConfig{
|
|
|
|
DefaultMessage: &i18n.Message{
|
|
|
|
ID: "DiffTitle",
|
|
|
|
},
|
|
|
|
}))
|
|
|
|
assert.Equal(t, "Diff", l.SLocalize("DiffTitle"))
|
2018-10-28 21:13:55 +02:00
|
|
|
assert.Equal(t, "Weet je zeker dat je branch test wilt verwijderen?", l.TemplateLocalize("DeleteBranchMessage", Teml{"selectedBranchName": "test"}))
|
2018-08-20 08:53:13 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, s := range scenarios {
|
2018-08-26 07:46:18 +02:00
|
|
|
s.test(setupLocalizer(getDummyLog(), s.userLang))
|
2018-08-20 08:53:13 +02:00
|
|
|
}
|
|
|
|
}
|