You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

191 lines
4.3 KiB

  1. // Copyright 2017 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package number
  5. import (
  6. "strings"
  7. "testing"
  8. "golang.org/x/text/language"
  9. "golang.org/x/text/message"
  10. )
  11. func TestFormatter(t *testing.T) {
  12. overrides := map[string]string{
  13. "en": "*e#######0",
  14. "nl": "*n#######0",
  15. }
  16. testCases := []struct {
  17. desc string
  18. tag string
  19. f Formatter
  20. want string
  21. }{{
  22. desc: "decimal",
  23. f: Decimal(3),
  24. want: "3",
  25. }, {
  26. desc: "decimal fraction",
  27. f: Decimal(0.123),
  28. want: "0.123",
  29. }, {
  30. desc: "separators",
  31. f: Decimal(1234.567),
  32. want: "1,234.567",
  33. }, {
  34. desc: "no separators",
  35. f: Decimal(1234.567, NoSeparator()),
  36. want: "1234.567",
  37. }, {
  38. desc: "max integer",
  39. f: Decimal(1973, MaxIntegerDigits(2)),
  40. want: "73",
  41. }, {
  42. desc: "max integer overflow",
  43. f: Decimal(1973, MaxIntegerDigits(1000)),
  44. want: "1,973",
  45. }, {
  46. desc: "min integer",
  47. f: Decimal(12, MinIntegerDigits(5)),
  48. want: "00,012",
  49. }, {
  50. desc: "max fraction zero",
  51. f: Decimal(0.12345, MaxFractionDigits(0)),
  52. want: "0",
  53. }, {
  54. desc: "max fraction 2",
  55. f: Decimal(0.12, MaxFractionDigits(2)),
  56. want: "0.12",
  57. }, {
  58. desc: "min fraction 2",
  59. f: Decimal(0.12, MaxFractionDigits(2)),
  60. want: "0.12",
  61. }, {
  62. desc: "max fraction overflow",
  63. f: Decimal(0.125, MaxFractionDigits(1e6)),
  64. want: "0.125",
  65. }, {
  66. desc: "min integer overflow",
  67. f: Decimal(0, MinIntegerDigits(1e6)),
  68. want: strings.Repeat("000,", 255/3-1) + "000",
  69. }, {
  70. desc: "min fraction overflow",
  71. f: Decimal(0, MinFractionDigits(1e6)),
  72. want: "0." + strings.Repeat("0", 255), // TODO: fraction separators
  73. }, {
  74. desc: "format width",
  75. f: Decimal(123, FormatWidth(10)),
  76. want: " 123",
  77. }, {
  78. desc: "format width pad option before",
  79. f: Decimal(123, Pad('*'), FormatWidth(10)),
  80. want: "*******123",
  81. }, {
  82. desc: "format width pad option after",
  83. f: Decimal(123, FormatWidth(10), Pad('*')),
  84. want: "*******123",
  85. }, {
  86. desc: "format width illegal",
  87. f: Decimal(123, FormatWidth(-1)),
  88. want: "123",
  89. }, {
  90. desc: "increment",
  91. f: Decimal(10.33, IncrementString("0.5")),
  92. want: "10.5",
  93. }, {
  94. desc: "increment",
  95. f: Decimal(10, IncrementString("ppp")),
  96. want: "10",
  97. }, {
  98. desc: "increment and scale",
  99. f: Decimal(10.33, IncrementString("0.5"), Scale(2)),
  100. want: "10.50",
  101. }, {
  102. desc: "pattern overrides en",
  103. tag: "en",
  104. f: Decimal(101, PatternOverrides(overrides)),
  105. want: "eeeee101",
  106. }, {
  107. desc: "pattern overrides nl",
  108. tag: "nl",
  109. f: Decimal(101, PatternOverrides(overrides)),
  110. want: "nnnnn101",
  111. }, {
  112. desc: "pattern overrides de",
  113. tag: "de",
  114. f: Decimal(101, PatternOverrides(overrides)),
  115. want: "101",
  116. }, {
  117. desc: "language selection",
  118. tag: "bn",
  119. f: Decimal(123456.78, Scale(2)),
  120. want: "১,২৩,৪৫৬.৭৮",
  121. }, {
  122. desc: "scale",
  123. f: Decimal(1234.567, Scale(2)),
  124. want: "1,234.57",
  125. }, {
  126. desc: "scientific",
  127. f: Scientific(3.00),
  128. want: "3\u202f×\u202f10⁰",
  129. }, {
  130. desc: "scientific",
  131. f: Scientific(1234),
  132. want: "1.234\u202f×\u202f10³",
  133. }, {
  134. desc: "scientific",
  135. f: Scientific(1234, Scale(2)),
  136. want: "1.23\u202f×\u202f10³",
  137. }, {
  138. desc: "engineering",
  139. f: Engineering(12345),
  140. want: "12.345\u202f×\u202f10³",
  141. }, {
  142. desc: "engineering scale",
  143. f: Engineering(12345, Scale(2)),
  144. want: "12.34\u202f×\u202f10³",
  145. }, {
  146. desc: "engineering precision(4)",
  147. f: Engineering(12345, Precision(4)),
  148. want: "12.34\u202f×\u202f10³",
  149. }, {
  150. desc: "engineering precision(2)",
  151. f: Engineering(1234.5, Precision(2)),
  152. want: "1.2\u202f×\u202f10³",
  153. }, {
  154. desc: "percent",
  155. f: Percent(0.12),
  156. want: "12%",
  157. }, {
  158. desc: "permille",
  159. f: PerMille(0.123),
  160. want: "123‰",
  161. }, {
  162. desc: "percent rounding",
  163. f: PerMille(0.12345),
  164. want: "123‰",
  165. }, {
  166. desc: "percent fraction",
  167. f: PerMille(0.12345, Scale(2)),
  168. want: "123.45‰",
  169. }, {
  170. desc: "percent fraction",
  171. f: PerMille(0.12344, Scale(1)),
  172. want: "123.4‰",
  173. }}
  174. for _, tc := range testCases {
  175. t.Run(tc.desc, func(t *testing.T) {
  176. tag := language.Und
  177. if tc.tag != "" {
  178. tag = language.MustParse(tc.tag)
  179. }
  180. got := message.NewPrinter(tag).Sprint(tc.f)
  181. if got != tc.want {
  182. t.Errorf("got %q; want %q", got, tc.want)
  183. }
  184. })
  185. }
  186. }