forked from manuelstofer/braintree-go
-
Notifications
You must be signed in to change notification settings - Fork 1
/
decimal_test.go
107 lines (94 loc) · 2.67 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
// +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
}{
{NewDecimal(250, -2), []byte("25000")},
{NewDecimal(2, 0), []byte("2")},
{NewDecimal(23, 0), []byte("23")},
{NewDecimal(234, 0), []byte("234")},
{NewDecimal(0, 1), []byte("0.0")},
{NewDecimal(1, 1), []byte("0.1")},
{NewDecimal(12, 1), []byte("1.2")},
{NewDecimal(0, 2), []byte("0.00")},
{NewDecimal(5, 2), []byte("0.05")},
{NewDecimal(55, 2), []byte("0.55")},
{NewDecimal(250, 2), []byte("2.50")},
{NewDecimal(4586, 2), []byte("45.86")},
{NewDecimal(-5504, 2), []byte("-55.04")},
{NewDecimal(0, 3), []byte("0.000")},
{NewDecimal(5, 3), []byte("0.005")},
{NewDecimal(55, 3), []byte("0.055")},
{NewDecimal(250, 3), []byte("0.250")},
{NewDecimal(4586, 3), []byte("4.586")},
{NewDecimal(45867, 3), []byte("45.867")},
{NewDecimal(-55043, 3), []byte("-55.043")},
}
for _, tt := range tests {
b, err := tt.in.MarshalText()
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)
}
}
}