number_test.go 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. package ut_test
  2. import (
  3. "testing"
  4. "github.com/go-playground/universal-translator"
  5. _ "github.com/go-playground/universal-translator/resources/locales"
  6. . "gopkg.in/go-playground/assert.v1"
  7. )
  8. // NOTES:
  9. // - Run "go test" to run tests
  10. // - Run "gocov test | gocov report" to report on test converage by file
  11. // - Run "gocov test | gocov annotate -" to report on all code and functions, those ,marked with "MISS" were never called
  12. //
  13. // or
  14. //
  15. // -- may be a good idea to change to output path to somewherelike /tmp
  16. // go test -coverprofile cover.out && go tool cover -html=cover.out -o cover.html
  17. //
  18. func TestCurrencyEn(t *testing.T) {
  19. en, err := ut.GetTranslator("en")
  20. Equal(t, err, nil)
  21. result, err := en.FmtCurrencySafe(ut.CurrencyStandard, "USD", 12345.6789)
  22. Equal(t, err, nil)
  23. Equal(t, result, "$12,345.68")
  24. result = en.FmtCurrency(ut.CurrencyStandard, "USD", 12345.6789)
  25. Equal(t, result, "$12,345.68")
  26. result, err = en.FmtCurrencySafe(ut.CurrencyAccounting, "USD", 12345.6789)
  27. Equal(t, err, nil)
  28. Equal(t, result, "$12,345.68")
  29. result = en.FmtCurrency(ut.CurrencyAccounting, "USD", 12345.6789)
  30. Equal(t, result, "$12,345.68")
  31. result, err = en.FmtCurrencySafe(ut.CurrencyStandard, "USD", -12345.6789)
  32. Equal(t, err, nil)
  33. Equal(t, result, "-$12,345.68")
  34. result = en.FmtCurrency(ut.CurrencyStandard, "USD", -12345.6789)
  35. Equal(t, result, "-$12,345.68")
  36. result, err = en.FmtCurrencySafe(ut.CurrencyAccounting, "USD", -12345.6789)
  37. Equal(t, err, nil)
  38. Equal(t, result, "($12,345.68)")
  39. result = en.FmtCurrency(ut.CurrencyAccounting, "USD", -12345.6789)
  40. Equal(t, result, "($12,345.68)")
  41. result, err = en.FmtCurrencySafe(ut.CurrencyStandard, "WHAT???", 12345.6789)
  42. NotEqual(t, err, nil)
  43. Equal(t, err.Error(), "**** WARNING **** unknown currency: WHAT???")
  44. result = en.FmtCurrency(ut.CurrencyStandard, "WHAT???", 12345.6789)
  45. Equal(t, result, "WHAT???12,345.68")
  46. result, err = en.FmtCurrencySafe(ut.CurrencyAccounting, "WHAT???", 12345.6789)
  47. NotEqual(t, err, nil)
  48. Equal(t, err.Error(), "**** WARNING **** unknown currency: WHAT???")
  49. result = en.FmtCurrency(ut.CurrencyAccounting, "WHAT???", 12345.6789)
  50. Equal(t, result, "WHAT???12,345.68")
  51. // try some really big numbers to make sure weird floaty stuff doesn't
  52. // happen
  53. result, err = en.FmtCurrencySafe(ut.CurrencyStandard, "USD", 12345000000000.6789)
  54. Equal(t, err, nil)
  55. Equal(t, result, "$12,345,000,000,000.68")
  56. result = en.FmtCurrency(ut.CurrencyStandard, "USD", 12345000000000.6789)
  57. Equal(t, result, "$12,345,000,000,000.68")
  58. result, err = en.FmtCurrencySafe(ut.CurrencyAccounting, "USD", 12345000000000.6789)
  59. Equal(t, err, nil)
  60. Equal(t, result, "$12,345,000,000,000.68")
  61. result = en.FmtCurrency(ut.CurrencyAccounting, "USD", 12345000000000.6789)
  62. Equal(t, result, "$12,345,000,000,000.68")
  63. result, err = en.FmtCurrencySafe(ut.CurrencyStandard, "USD", -12345000000000.6789)
  64. Equal(t, err, nil)
  65. Equal(t, result, "-$12,345,000,000,000.68")
  66. result = en.FmtCurrency(ut.CurrencyStandard, "USD", -12345000000000.6789)
  67. Equal(t, result, "-$12,345,000,000,000.68")
  68. result, err = en.FmtCurrencySafe(ut.CurrencyAccounting, "USD", -12345000000000.6789)
  69. Equal(t, err, nil)
  70. Equal(t, result, "($12,345,000,000,000.68)")
  71. result = en.FmtCurrency(ut.CurrencyAccounting, "USD", -12345000000000.6789)
  72. Equal(t, result, "($12,345,000,000,000.68)")
  73. saq, err := ut.GetTranslator("saq")
  74. Equal(t, err, nil)
  75. // no Formatting or symbols for "saq" in locales
  76. result, err = saq.FmtCurrencySafe(ut.CurrencyStandard, "USD", 12345.6789)
  77. Equal(t, err, nil)
  78. Equal(t, result, "1234568")
  79. result = saq.FmtCurrency(ut.CurrencyStandard, "USD", 12345.6789)
  80. Equal(t, result, "1234568")
  81. result, err = saq.FmtCurrencySafe(ut.CurrencyAccounting, "USD", 12345.6789)
  82. Equal(t, err, nil)
  83. Equal(t, result, "1234568")
  84. result = saq.FmtCurrency(ut.CurrencyAccounting, "USD", 12345.6789)
  85. Equal(t, result, "1234568")
  86. ar, err := ut.GetTranslator("ar")
  87. Equal(t, err, nil)
  88. result, err = ar.FmtCurrencySafe(ut.CurrencyStandard, "USD", -12345.6789)
  89. Equal(t, err, nil)
  90. Equal(t, result, "\u200F-US$\u00A012٬345٫68")
  91. result = ar.FmtCurrency(ut.CurrencyStandard, "USD", -12345.6789)
  92. Equal(t, result, "\u200F-US$\u00A012٬345٫68")
  93. result, err = ar.FmtCurrencySafe(ut.CurrencyAccounting, "USD", -12345.6789)
  94. Equal(t, err, nil)
  95. Equal(t, result, "\u200F-US$\u00A012٬345٫68")
  96. result = ar.FmtCurrency(ut.CurrencyAccounting, "USD", -12345.6789)
  97. Equal(t, result, "\u200F-US$\u00A012٬345٫68")
  98. // And one more for with some unusual symbols for good measure
  99. result, err = en.FmtCurrencySafe(ut.CurrencyStandard, "USD", 0.0084)
  100. Equal(t, err, nil)
  101. Equal(t, result, "$0.01")
  102. result = en.FmtCurrency(ut.CurrencyStandard, "USD", 0.0084)
  103. Equal(t, result, "$0.01")
  104. }
  105. func TestCurrencyWholeEn(t *testing.T) {
  106. en, err := ut.GetTranslator("en")
  107. Equal(t, err, nil)
  108. result, err := en.FmtCurrencyWholeSafe(ut.CurrencyStandard, "USD", 12345.6789)
  109. Equal(t, err, nil)
  110. Equal(t, result, "$12,346")
  111. result = en.FmtCurrencyWhole(ut.CurrencyStandard, "USD", 12345.6789)
  112. Equal(t, result, "$12,346")
  113. result, err = en.FmtCurrencyWholeSafe(ut.CurrencyAccounting, "USD", 12345.6789)
  114. Equal(t, err, nil)
  115. Equal(t, result, "$12,346")
  116. result = en.FmtCurrencyWhole(ut.CurrencyAccounting, "USD", 12345.6789)
  117. Equal(t, result, "$12,346")
  118. result, err = en.FmtCurrencyWholeSafe(ut.CurrencyStandard, "USD", -12345.6789)
  119. Equal(t, err, nil)
  120. Equal(t, result, "-$12,346")
  121. result = en.FmtCurrencyWhole(ut.CurrencyStandard, "USD", -12345.6789)
  122. Equal(t, result, "-$12,346")
  123. result, err = en.FmtCurrencyWholeSafe(ut.CurrencyAccounting, "USD", -12345.6789)
  124. Equal(t, err, nil)
  125. Equal(t, result, "($12,346)")
  126. result = en.FmtCurrencyWhole(ut.CurrencyAccounting, "USD", -12345.6789)
  127. Equal(t, result, "($12,346)")
  128. result, err = en.FmtCurrencyWholeSafe(ut.CurrencyStandard, "WHAT???", 12345.6789)
  129. NotEqual(t, err, nil)
  130. Equal(t, result, "WHAT???12,346")
  131. result = en.FmtCurrencyWhole(ut.CurrencyStandard, "WHAT???", 12345.6789)
  132. Equal(t, result, "WHAT???12,346")
  133. // try some really big numbers to make sure weird floaty stuff doesn't
  134. // happen
  135. result, err = en.FmtCurrencyWholeSafe(ut.CurrencyStandard, "USD", 12345000000000.6789)
  136. Equal(t, err, nil)
  137. Equal(t, result, "$12,345,000,000,001")
  138. result, err = en.FmtCurrencyWholeSafe(ut.CurrencyAccounting, "USD", 12345000000000.6789)
  139. Equal(t, err, nil)
  140. Equal(t, result, "$12,345,000,000,001")
  141. result, err = en.FmtCurrencyWholeSafe(ut.CurrencyStandard, "USD", -12345000000000.6789)
  142. Equal(t, err, nil)
  143. Equal(t, result, "-$12,345,000,000,001")
  144. result = en.FmtCurrencyWhole(ut.CurrencyStandard, "USD", -12345000000000.6789)
  145. Equal(t, result, "-$12,345,000,000,001")
  146. result, err = en.FmtCurrencyWholeSafe(ut.CurrencyAccounting, "USD", -12345000000000.6789)
  147. Equal(t, err, nil)
  148. Equal(t, result, "($12,345,000,000,001)")
  149. result = en.FmtCurrencyWhole(ut.CurrencyAccounting, "USD", -12345000000000.6789)
  150. Equal(t, result, "($12,345,000,000,001)")
  151. }
  152. func TestNumberEn(t *testing.T) {
  153. en, err := ut.GetTranslator("en")
  154. Equal(t, err, nil)
  155. // check basic english
  156. result := en.FmtNumber(12345.6789)
  157. Equal(t, result, "12,345.679")
  158. result = en.FmtNumber(-12345.6789)
  159. Equal(t, result, "-12,345.679")
  160. result = en.FmtNumber(123456789)
  161. Equal(t, result, "123,456,789")
  162. hi, err := ut.GetTranslator("hi")
  163. Equal(t, err, nil)
  164. // check Hindi - different group sizes
  165. result = hi.FmtNumber(12345.6789)
  166. Equal(t, result, "12,345.679")
  167. result = hi.FmtNumber(-12345.6789)
  168. Equal(t, result, "-12,345.679")
  169. result = hi.FmtNumber(123456789)
  170. Equal(t, result, "12,34,56,789")
  171. uz, err := ut.GetTranslator("uz")
  172. Equal(t, err, nil)
  173. // check Uzbek - something with a partial fallback
  174. result = uz.FmtNumber(12345.6789)
  175. Equal(t, result, "12٬345٫679")
  176. result = uz.FmtNumber(-12345.6789)
  177. Equal(t, result, "-12٬345٫679")
  178. result = uz.FmtNumber(123456789)
  179. Equal(t, result, "123٬456٬789")
  180. }
  181. func TestNumberWholeEn(t *testing.T) {
  182. en, err := ut.GetTranslator("en")
  183. Equal(t, err, nil)
  184. result := en.FmtNumberWhole(12345.6789)
  185. Equal(t, result, "12,346")
  186. result = en.FmtNumberWhole(-12345.6789)
  187. Equal(t, result, "-12,346")
  188. }
  189. func TestPercentEn(t *testing.T) {
  190. en, err := ut.GetTranslator("en")
  191. Equal(t, err, nil)
  192. result := en.FmtPercent(0.01234)
  193. Equal(t, result, "1%")
  194. result = en.FmtPercent(0.1234)
  195. Equal(t, result, "12%")
  196. result = en.FmtPercent(1.234)
  197. Equal(t, result, "123%")
  198. result = en.FmtPercent(12.34)
  199. Equal(t, result, "1,234%")
  200. }