error_test.go 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. // SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
  2. // SPDX-License-Identifier: Apache-2.0
  3. package locale // import "miniflux.app/v2/internal/locale"
  4. import (
  5. "errors"
  6. "testing"
  7. )
  8. func TestNewLocalizedErrorWrapper(t *testing.T) {
  9. originalErr := errors.New("original error message")
  10. translationKey := "error.test_key"
  11. args := []any{"arg1", 42}
  12. wrapper := NewLocalizedErrorWrapper(originalErr, translationKey, args...)
  13. if wrapper.originalErr != originalErr {
  14. t.Errorf("Expected original error to be %v, got %v", originalErr, wrapper.originalErr)
  15. }
  16. if wrapper.translationKey != translationKey {
  17. t.Errorf("Expected translation key to be %q, got %q", translationKey, wrapper.translationKey)
  18. }
  19. if len(wrapper.translationArgs) != 2 {
  20. t.Errorf("Expected 2 translation args, got %d", len(wrapper.translationArgs))
  21. }
  22. if wrapper.translationArgs[0] != "arg1" || wrapper.translationArgs[1] != 42 {
  23. t.Errorf("Expected translation args [arg1, 42], got %v", wrapper.translationArgs)
  24. }
  25. }
  26. func TestLocalizedErrorWrapper_Error(t *testing.T) {
  27. originalErr := errors.New("original error message")
  28. wrapper := NewLocalizedErrorWrapper(originalErr, "error.test_key")
  29. result := wrapper.Error()
  30. if result != originalErr {
  31. t.Errorf("Expected Error() to return original error %v, got %v", originalErr, result)
  32. }
  33. }
  34. func TestLocalizedErrorWrapper_Translate(t *testing.T) {
  35. // Set up test catalog
  36. defaultCatalog = catalog{
  37. "en_US": translationDict{
  38. "error.test_key": "Error: %s (code: %d)",
  39. },
  40. "fr_FR": translationDict{
  41. "error.test_key": "Erreur : %s (code : %d)",
  42. },
  43. }
  44. originalErr := errors.New("original error")
  45. wrapper := NewLocalizedErrorWrapper(originalErr, "error.test_key", "test message", 404)
  46. // Test English translation
  47. result := wrapper.Translate("en_US")
  48. expected := "Error: test message (code: 404)"
  49. if result != expected {
  50. t.Errorf("Expected English translation %q, got %q", expected, result)
  51. }
  52. // Test French translation
  53. result = wrapper.Translate("fr_FR")
  54. expected = "Erreur : test message (code : 404)"
  55. if result != expected {
  56. t.Errorf("Expected French translation %q, got %q", expected, result)
  57. }
  58. // Test with missing language (should use key as fallback with args applied)
  59. result = wrapper.Translate("invalid_lang")
  60. expected = "error.test_key%!(EXTRA string=test message, int=404)"
  61. if result != expected {
  62. t.Errorf("Expected fallback translation %q, got %q", expected, result)
  63. }
  64. }
  65. func TestLocalizedErrorWrapper_TranslateWithEmptyKey(t *testing.T) {
  66. originalErr := errors.New("original error message")
  67. wrapper := NewLocalizedErrorWrapper(originalErr, "")
  68. result := wrapper.Translate("en_US")
  69. expected := "original error message"
  70. if result != expected {
  71. t.Errorf("Expected original error message %q, got %q", expected, result)
  72. }
  73. }
  74. func TestLocalizedErrorWrapper_TranslateWithNoArgs(t *testing.T) {
  75. defaultCatalog = catalog{
  76. "en_US": translationDict{
  77. "error.simple": "Simple error message",
  78. },
  79. }
  80. originalErr := errors.New("original error")
  81. wrapper := NewLocalizedErrorWrapper(originalErr, "error.simple")
  82. result := wrapper.Translate("en_US")
  83. expected := "Simple error message"
  84. if result != expected {
  85. t.Errorf("Expected translation %q, got %q", expected, result)
  86. }
  87. }
  88. func TestNewLocalizedError(t *testing.T) {
  89. translationKey := "error.validation"
  90. args := []any{"field1", "invalid"}
  91. localizedErr := NewLocalizedError(translationKey, args...)
  92. if localizedErr.translationKey != translationKey {
  93. t.Errorf("Expected translation key to be %q, got %q", translationKey, localizedErr.translationKey)
  94. }
  95. if len(localizedErr.translationArgs) != 2 {
  96. t.Errorf("Expected 2 translation args, got %d", len(localizedErr.translationArgs))
  97. }
  98. if localizedErr.translationArgs[0] != "field1" || localizedErr.translationArgs[1] != "invalid" {
  99. t.Errorf("Expected translation args [field1, invalid], got %v", localizedErr.translationArgs)
  100. }
  101. }
  102. func TestLocalizedError_String(t *testing.T) {
  103. defaultCatalog = catalog{
  104. "en_US": translationDict{
  105. "error.validation": "Validation failed for %s: %s",
  106. },
  107. }
  108. localizedErr := NewLocalizedError("error.validation", "username", "too short")
  109. result := localizedErr.String()
  110. expected := "Validation failed for username: too short"
  111. if result != expected {
  112. t.Errorf("Expected String() result %q, got %q", expected, result)
  113. }
  114. }
  115. func TestLocalizedError_StringWithMissingTranslation(t *testing.T) {
  116. defaultCatalog = catalog{
  117. "en_US": translationDict{},
  118. }
  119. localizedErr := NewLocalizedError("error.missing", "arg1")
  120. result := localizedErr.String()
  121. expected := "error.missing%!(EXTRA string=arg1)"
  122. if result != expected {
  123. t.Errorf("Expected String() result %q, got %q", expected, result)
  124. }
  125. }
  126. func TestLocalizedError_Error(t *testing.T) {
  127. defaultCatalog = catalog{
  128. "en_US": translationDict{
  129. "error.database": "Database connection failed: %s",
  130. },
  131. }
  132. localizedErr := NewLocalizedError("error.database", "timeout")
  133. result := localizedErr.Error()
  134. if result == nil {
  135. t.Error("Expected Error() to return a non-nil error")
  136. }
  137. expected := "Database connection failed: timeout"
  138. if result.Error() != expected {
  139. t.Errorf("Expected Error() message %q, got %q", expected, result.Error())
  140. }
  141. }
  142. func TestLocalizedError_Translate(t *testing.T) {
  143. defaultCatalog = catalog{
  144. "en_US": translationDict{
  145. "error.permission": "Permission denied for %s",
  146. },
  147. "es_ES": translationDict{
  148. "error.permission": "Permiso denegado para %s",
  149. },
  150. }
  151. localizedErr := NewLocalizedError("error.permission", "admin panel")
  152. // Test English translation
  153. result := localizedErr.Translate("en_US")
  154. expected := "Permission denied for admin panel"
  155. if result != expected {
  156. t.Errorf("Expected English translation %q, got %q", expected, result)
  157. }
  158. // Test Spanish translation
  159. result = localizedErr.Translate("es_ES")
  160. expected = "Permiso denegado para admin panel"
  161. if result != expected {
  162. t.Errorf("Expected Spanish translation %q, got %q", expected, result)
  163. }
  164. // Test with missing language
  165. result = localizedErr.Translate("invalid_lang")
  166. expected = "error.permission%!(EXTRA string=admin panel)"
  167. if result != expected {
  168. t.Errorf("Expected fallback translation %q, got %q", expected, result)
  169. }
  170. }
  171. func TestLocalizedError_TranslateWithNoArgs(t *testing.T) {
  172. defaultCatalog = catalog{
  173. "en_US": translationDict{
  174. "error.generic": "An error occurred",
  175. },
  176. "de_DE": translationDict{
  177. "error.generic": "Ein Fehler ist aufgetreten",
  178. },
  179. }
  180. localizedErr := NewLocalizedError("error.generic")
  181. // Test English
  182. result := localizedErr.Translate("en_US")
  183. expected := "An error occurred"
  184. if result != expected {
  185. t.Errorf("Expected English translation %q, got %q", expected, result)
  186. }
  187. // Test German
  188. result = localizedErr.Translate("de_DE")
  189. expected = "Ein Fehler ist aufgetreten"
  190. if result != expected {
  191. t.Errorf("Expected German translation %q, got %q", expected, result)
  192. }
  193. }
  194. func TestLocalizedError_TranslateWithComplexArgs(t *testing.T) {
  195. defaultCatalog = catalog{
  196. "en_US": translationDict{
  197. "error.complex": "Error %d: %s occurred at %s with severity %s",
  198. },
  199. }
  200. localizedErr := NewLocalizedError("error.complex", 500, "Internal Server Error", "2024-01-01", "high")
  201. result := localizedErr.Translate("en_US")
  202. expected := "Error 500: Internal Server Error occurred at 2024-01-01 with severity high"
  203. if result != expected {
  204. t.Errorf("Expected complex translation %q, got %q", expected, result)
  205. }
  206. }
  207. func TestLocalizedErrorWrapper_WithNilError(t *testing.T) {
  208. // This tests edge case behavior - what happens with nil error
  209. wrapper := NewLocalizedErrorWrapper(nil, "error.test")
  210. // Error() should return nil
  211. result := wrapper.Error()
  212. if result != nil {
  213. t.Errorf("Expected Error() to return nil, got %v", result)
  214. }
  215. }
  216. func TestLocalizedError_EmptyKey(t *testing.T) {
  217. localizedErr := NewLocalizedError("")
  218. result := localizedErr.String()
  219. expected := ""
  220. if result != expected {
  221. t.Errorf("Expected empty string for empty key, got %q", result)
  222. }
  223. result = localizedErr.Translate("en_US")
  224. if result != expected {
  225. t.Errorf("Expected empty string for empty key translation, got %q", result)
  226. }
  227. }