1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253 |
- package domain
- import (
- "fmt"
- "testing"
- "github.com/nicksnyder/go-i18n/v2/i18n"
- "golang.org/x/text/language"
- )
- /*
- TestMakeErrorHappy tests localization of a known error.
- */
- func TestMakeErrorHappy(t *testing.T) {
- // Do not use a i18n.Message literal to initialize, to prevent goi18n extraction.
- var frNoError i18n.Message
- frNoError.ID = NoError.ID
- frNoError.Other = "Pas d'erreur"
- bundle := &i18n.Bundle{DefaultLanguage: language.English}
- bundle.AddMessages(language.English, &NoError)
- bundle.AddMessages(language.French, &frNoError)
- type testPass struct {
- tag string
- error i18n.Message
- details string
- expected string
- }
- passes := []testPass{
- {language.English.String(), NoError, "", NoError.Other},
- {language.English.String(), NoError, "details", NoError.Other + ": details"},
- {language.French.String(), frNoError, "", frNoError.Other},
- {language.French.String(), frNoError, "details", frNoError.Other + ": details"},
- }
- for _, pass := range passes {
- var subTestName string
- if pass.details == "" {
- subTestName = fmt.Sprintf("%s/<no details>", pass.tag)
- } else {
- subTestName = fmt.Sprintf("%s/%s", pass.tag, pass.details)
- }
- t.Run(subTestName, func(t *testing.T) {
- localizer := i18n.NewLocalizer(bundle, pass.tag)
- err := MakeError(localizer, pass.error.ID, pass.details)
- expected := pass.expected
- if err.Error() != expected {
- t.Errorf("Localized \"%s\" version (%v) does not match expected value (%v)\n",
- pass.tag, err.Error(), expected)
- }
- })
- }
- }
|