errors_test.go 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  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. frNoError := i18n.Message{
  13. ID: NoError.ID,
  14. Other: "Pas d'erreur",
  15. }
  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. }