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