forked from braintree-go/braintree-go
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdecimal_test.go
256 lines (236 loc) · 6.03 KB
/
decimal_test.go
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
//go:build unit
// +build unit
package braintree
import (
"reflect"
"testing"
)
func TestDecimalUnmarshalText(t *testing.T) {
t.Parallel()
tests := []struct {
in []byte
out *Decimal
shouldError bool
}{
{[]byte("2.50"), NewDecimal(250, 2), false},
{[]byte("2"), NewDecimal(2, 0), false},
{[]byte("0.00"), NewDecimal(0, 2), false},
{[]byte("-5.504"), NewDecimal(-5504, 3), false},
{[]byte("0.5"), NewDecimal(5, 1), false},
{[]byte(".5"), NewDecimal(5, 1), false},
{[]byte("5.504.98"), NewDecimal(0, 0), true},
{[]byte("5E6"), NewDecimal(0, 0), true},
}
for _, tt := range tests {
d := &Decimal{}
err := d.UnmarshalText(tt.in)
if tt.shouldError {
if err == nil {
t.Errorf("expected UnmarshalText(%s) => to error, but it did not", tt.in)
}
} else {
if err != nil {
t.Errorf("expected UnmarshalText(%s) => to not error, but it did with %s", tt.in, err)
}
}
if !reflect.DeepEqual(d, tt.out) {
t.Errorf("UnmarshalText(%s) => %+v, want %+v", tt.in, d, tt.out)
}
}
}
func TestDecimalMarshalText(t *testing.T) {
t.Parallel()
tests := []struct {
in *Decimal
out []byte
shouldError bool
}{
{NewDecimal(250, -2), []byte("25000"), false},
{NewDecimal(2, 0), []byte("2"), false},
{NewDecimal(23, 0), []byte("23"), false},
{NewDecimal(234, 0), []byte("234"), false},
{NewDecimal(0, 1), []byte("0.0"), false},
{NewDecimal(1, 1), []byte("0.1"), false},
{NewDecimal(12, 1), []byte("1.2"), false},
{NewDecimal(0, 2), []byte("0.00"), false},
{NewDecimal(5, 2), []byte("0.05"), false},
{NewDecimal(55, 2), []byte("0.55"), false},
{NewDecimal(250, 2), []byte("2.50"), false},
{NewDecimal(4586, 2), []byte("45.86"), false},
{NewDecimal(-5504, 2), []byte("-55.04"), false},
{NewDecimal(0, 3), []byte("0.000"), false},
{NewDecimal(5, 3), []byte("0.005"), false},
{NewDecimal(55, 3), []byte("0.055"), false},
{NewDecimal(250, 3), []byte("0.250"), false},
{NewDecimal(4586, 3), []byte("4.586"), false},
{NewDecimal(45867, 3), []byte("45.867"), false},
{NewDecimal(-55043, 3), []byte("-55.043"), false},
{nil, nil, true},
}
for _, tt := range tests {
b, err := tt.in.MarshalText()
if tt.shouldError {
if err == nil {
t.Errorf("expected %+v.MarshalText() => to error, but it did not", tt.in)
}
} else {
if err != nil {
t.Errorf("expected %+v.MarshalText() => to not error, but it did with %s", tt.in, err)
}
}
if string(tt.out) != string(b) {
t.Errorf("%+v.MarshalText() => %s, want %s", tt.in, b, tt.out)
}
}
}
func TestDecimalCmp(t *testing.T) {
t.Parallel()
tests := []struct {
x, y *Decimal
out int
}{
{NewDecimal(250, -2), NewDecimal(250, -2), 0},
{NewDecimal(2, 0), NewDecimal(250, -2), -1},
{NewDecimal(500, 2), NewDecimal(50, 1), 0},
{NewDecimal(2500, -2), NewDecimal(250, -2), 1},
{NewDecimal(100, 2), NewDecimal(1, 0), 0},
}
for i, tt := range tests {
if out := tt.x.Cmp(tt.y); out != tt.out {
t.Errorf("%d: %+v.Cmp(%+v) => %d, want %d", i, tt.x, tt.y, out, tt.out)
}
}
}
func TestNewDecimalByCurrency(t *testing.T) {
tests := []struct {
name string
currency string
amount float64
expected *Decimal
}{
{
name: "Test EUR currency",
currency: "EUR",
amount: 10.50,
expected: NewDecimal(1050, 2),
},
{
name: "Test EUR currency, zero case",
currency: "EUR",
amount: 0,
expected: NewDecimal(0, 2),
},
{
name: "Test unknown (UKN) currency, default should be used",
currency: "UKN",
amount: 10.60,
expected: NewDecimal(1060, 2),
},
{
name: "Test CVE currency with zero decimal adjustment",
currency: "CVE",
amount: 150,
expected: NewDecimal(150, 0),
},
{
name: "Test BHD currency with 3 decimal adjustment points",
currency: "BHD",
amount: 150.050,
expected: NewDecimal(150050, 3),
},
{
name: "Test JPY currency with 0 decimal adjustment points",
currency: "JPY",
amount: 150.020,
expected: NewDecimal(150, 0),
},
{
name: "Test EUR currency with 2 decimal adjustment points is rounded",
currency: "EUR",
amount: 16.989999999999997,
expected: NewDecimal(1699, 2),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
d := NewDecimalByCurrency(tt.currency, tt.amount)
if !reflect.DeepEqual(d, tt.expected) {
t.Errorf("%s: got %d, want %d", tt.name, d, tt.expected)
}
})
}
}
func TestDecimal_Add(t *testing.T) {
tests := []struct {
name string
d1 *Decimal
d2 *Decimal
want *Decimal
wantErr bool
}{
{
name: "Add decimals of different scales",
d1: NewDecimal(1, 1),
d2: NewDecimal(1, 0),
wantErr: true,
},
{
name: "Add decimals of same scales",
d1: NewDecimal(6594, 2),
d2: NewDecimal(999, 2),
want: NewDecimal(7593, 2),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := tt.d1.Add(tt.d2)
if (err != nil) != tt.wantErr {
t.Errorf("Add() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("Add() got = %v, want %v", got, tt.want)
}
})
}
}
func TestDecimal_Subtract(t *testing.T) {
tests := []struct {
name string
d1 *Decimal
d2 *Decimal
want *Decimal
wantErr bool
}{
{
name: "Subtract decimals of different scales",
d1: NewDecimal(1, 1),
d2: NewDecimal(1, 0),
wantErr: true,
},
{
name: "Subtract decimals of same scales",
d1: NewDecimal(6594, 2),
d2: NewDecimal(999, 2),
want: NewDecimal(5595, 2),
},
{
name: "Subtract a bigger decimal from a smaller decimal",
d1: NewDecimal(999, 2),
d2: NewDecimal(6594, 2),
want: NewDecimal(-5595, 2),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := tt.d1.Subtract(tt.d2)
if (err != nil) != tt.wantErr {
t.Errorf("Subtract() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("Subtract() got = %v, want %v", got, tt.want)
}
})
}
}