errors_test.go 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. package domain
  2. import (
  3. "fmt"
  4. "testing"
  5. "github.com/nicksnyder/go-i18n/v2/i18n"
  6. "golang.org/x/text/language"
  7. )
  8. /*
  9. TestMakeErrorHappy tests localization of a known error.
  10. */
  11. func TestMakeErrorHappy(t *testing.T) {
  12. // Do not use a i18n.Message literal to initialize, to prevent goi18n extraction.
  13. var frNoError i18n.Message
  14. frNoError.ID = NoError.ID
  15. frNoError.Other = "Pas d'erreur"
  16. bundle := &i18n.Bundle{DefaultLanguage: language.English}
  17. bundle.AddMessages(language.English, &NoError)
  18. bundle.AddMessages(language.French, &frNoError)
  19. type testPass struct {
  20. tag string
  21. error i18n.Message
  22. details string
  23. expected string
  24. }
  25. passes := []testPass{
  26. {language.English.String(), NoError, "", NoError.Other},
  27. {language.English.String(), NoError, "details", NoError.Other + ": details"},
  28. {language.French.String(), frNoError, "", frNoError.Other},
  29. {language.French.String(), frNoError, "details", frNoError.Other + ": details"},
  30. }
  31. for _, pass := range passes {
  32. var subTestName string
  33. if pass.details == "" {
  34. subTestName = fmt.Sprintf("%s/<no details>", pass.tag)
  35. } else {
  36. subTestName = fmt.Sprintf("%s/%s", pass.tag, pass.details)
  37. }
  38. t.Run(subTestName, func(t *testing.T) {
  39. localizer := i18n.NewLocalizer(bundle, pass.tag)
  40. err := MakeError(localizer, pass.error.ID, pass.details)
  41. expected := pass.expected
  42. if err.Error() != expected {
  43. t.Errorf("Localized \"%s\" version (%v) does not match expected value (%v)\n",
  44. pass.tag, err.Error(), expected)
  45. }
  46. })
  47. }
  48. }