forked from braintree-go/braintree-go
-
Notifications
You must be signed in to change notification settings - Fork 1
/
decimal_test.go
86 lines (76 loc) · 2.06 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
package braintree
import (
"reflect"
"testing"
)
func TestDecimalUnmarshalText(t *testing.T) {
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) {
tests := []struct {
in *Decimal
out []byte
}{
{NewDecimal(250, -2), []byte("25000")},
{NewDecimal(2, 0), []byte("2")},
{NewDecimal(0, 2), []byte("0.00")},
{NewDecimal(250, 2), []byte("2.50")},
{NewDecimal(4586, 2), []byte("45.86")},
{NewDecimal(-5504, 2), []byte("-55.04")},
}
for _, tt := range tests {
b, err := tt.in.MarshalText()
if err != nil {
t.Errorf("expected %+v.MarshaText() => to not error, but it did with %s", tt.in, err)
}
if string(tt.out) != string(b) {
t.Errorf("%+v.MarshaText() => %s, want %s", tt.in, b, tt.out)
}
}
}
func TestDecimalCmp(t *testing.T) {
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)
}
}
}