summaryrefslogtreecommitdiff
path: root/vendor/golang.org/x/text/number/number_test.go
blob: 3dcac3649d64040b3d02a838209607891b898212 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
// Copyright 2017 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package number

import (
	"strings"
	"testing"

	"golang.org/x/text/language"
	"golang.org/x/text/message"
)

func TestFormatter(t *testing.T) {
	overrides := map[string]string{
		"en": "*e#######0",
		"nl": "*n#######0",
	}
	testCases := []struct {
		desc string
		tag  string
		f    Formatter
		want string
	}{{
		desc: "decimal",
		f:    Decimal(3),
		want: "3",
	}, {
		desc: "decimal fraction",
		f:    Decimal(0.123),
		want: "0.123",
	}, {
		desc: "separators",
		f:    Decimal(1234.567),
		want: "1,234.567",
	}, {
		desc: "no separators",
		f:    Decimal(1234.567, NoSeparator()),
		want: "1234.567",
	}, {
		desc: "max integer",
		f:    Decimal(1973, MaxIntegerDigits(2)),
		want: "73",
	}, {
		desc: "max integer overflow",
		f:    Decimal(1973, MaxIntegerDigits(1000)),
		want: "1,973",
	}, {
		desc: "min integer",
		f:    Decimal(12, MinIntegerDigits(5)),
		want: "00,012",
	}, {
		desc: "max fraction zero",
		f:    Decimal(0.12345, MaxFractionDigits(0)),
		want: "0",
	}, {
		desc: "max fraction 2",
		f:    Decimal(0.12, MaxFractionDigits(2)),
		want: "0.12",
	}, {
		desc: "min fraction 2",
		f:    Decimal(0.12, MaxFractionDigits(2)),
		want: "0.12",
	}, {
		desc: "max fraction overflow",
		f:    Decimal(0.125, MaxFractionDigits(1e6)),
		want: "0.125",
	}, {
		desc: "min integer overflow",
		f:    Decimal(0, MinIntegerDigits(1e6)),
		want: strings.Repeat("000,", 255/3-1) + "000",
	}, {
		desc: "min fraction overflow",
		f:    Decimal(0, MinFractionDigits(1e6)),
		want: "0." + strings.Repeat("0", 255), // TODO: fraction separators
	}, {
		desc: "format width",
		f:    Decimal(123, FormatWidth(10)),
		want: "       123",
	}, {
		desc: "format width pad option before",
		f:    Decimal(123, Pad('*'), FormatWidth(10)),
		want: "*******123",
	}, {
		desc: "format width pad option after",
		f:    Decimal(123, FormatWidth(10), Pad('*')),
		want: "*******123",
	}, {
		desc: "format width illegal",
		f:    Decimal(123, FormatWidth(-1)),
		want: "123",
	}, {
		desc: "increment",
		f:    Decimal(10.33, IncrementString("0.5")),
		want: "10.5",
	}, {
		desc: "increment",
		f:    Decimal(10, IncrementString("ppp")),
		want: "10",
	}, {
		desc: "increment and scale",
		f:    Decimal(10.33, IncrementString("0.5"), Scale(2)),
		want: "10.50",
	}, {
		desc: "pattern overrides en",
		tag:  "en",
		f:    Decimal(101, PatternOverrides(overrides)),
		want: "eeeee101",
	}, {
		desc: "pattern overrides nl",
		tag:  "nl",
		f:    Decimal(101, PatternOverrides(overrides)),
		want: "nnnnn101",
	}, {
		desc: "pattern overrides de",
		tag:  "de",
		f:    Decimal(101, PatternOverrides(overrides)),
		want: "101",
	}, {
		desc: "language selection",
		tag:  "bn",
		f:    Decimal(123456.78, Scale(2)),
		want: "১,২৩,৪৫৬.৭৮",
	}, {
		desc: "scale",
		f:    Decimal(1234.567, Scale(2)),
		want: "1,234.57",
	}, {
		desc: "scientific",
		f:    Scientific(3.00),
		want: "3\u202f×\u202f10⁰",
	}, {
		desc: "scientific",
		f:    Scientific(1234),
		want: "1.234\u202f×\u202f10³",
	}, {
		desc: "scientific",
		f:    Scientific(1234, Scale(2)),
		want: "1.23\u202f×\u202f10³",
	}, {
		desc: "engineering",
		f:    Engineering(12345),
		want: "12.345\u202f×\u202f10³",
	}, {
		desc: "engineering scale",
		f:    Engineering(12345, Scale(2)),
		want: "12.34\u202f×\u202f10³",
	}, {
		desc: "engineering precision(4)",
		f:    Engineering(12345, Precision(4)),
		want: "12.34\u202f×\u202f10³",
	}, {
		desc: "engineering precision(2)",
		f:    Engineering(1234.5, Precision(2)),
		want: "1.2\u202f×\u202f10³",
	}, {
		desc: "percent",
		f:    Percent(0.12),
		want: "12%",
	}, {
		desc: "permille",
		f:    PerMille(0.123),
		want: "123‰",
	}, {
		desc: "percent rounding",
		f:    PerMille(0.12345),
		want: "123‰",
	}, {
		desc: "percent fraction",
		f:    PerMille(0.12345, Scale(2)),
		want: "123.45‰",
	}, {
		desc: "percent fraction",
		f:    PerMille(0.12344, Scale(1)),
		want: "123.4‰",
	}}
	for _, tc := range testCases {
		t.Run(tc.desc, func(t *testing.T) {
			tag := language.Und
			if tc.tag != "" {
				tag = language.MustParse(tc.tag)
			}
			got := message.NewPrinter(tag).Sprint(tc.f)
			if got != tc.want {
				t.Errorf("got %q; want %q", got, tc.want)
			}
		})
	}
}