-
Notifications
You must be signed in to change notification settings - Fork 1
/
money_test.go
91 lines (71 loc) · 1.41 KB
/
money_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
package money
import (
"testing"
"github.com/umran/decimal"
)
func TestNew(t *testing.T) {
amount := int64(20000)
m := New(amount, USD)
if m.Amount() != amount {
t.Error("unexpected amount")
}
}
func TestCurrency(t *testing.T) {
m := New(2000, USD)
if m.Currency().Code() != USD.Code() {
t.Error("unexpected currency")
t.Log(m.Currency().Code())
}
}
func TestAdd(t *testing.T) {
m1 := New(2000, USD)
m2 := New(1000, USD)
result, _ := m1.Add(m2)
if result.Amount() != 3000 {
t.Error("unexpected result")
t.Log(result.Amount())
}
}
func TestSub(t *testing.T) {
m1 := New(2000, USD)
m2 := New(1000, USD)
result, _ := m1.Sub(m2)
if result.Amount() != 1000 {
t.Error("unexpected result")
t.Log(result.Amount())
}
}
func TestMul(t *testing.T) {
m := New(100, USD)
result := m.Mul(decimal.New(3333333, -7))
if result.Amount() != 33 {
t.Error("unexpected result")
t.Log(result.Amount())
}
}
func TestInvalidAdd(t *testing.T) {
m1 := New(20000, USD)
m2 := New(10000, CAD)
_, err := m1.Add(m2)
if err != ErrCurrencyCodeMismatch {
t.Error("unexpected error")
t.Log(err)
}
}
func TestInvalidSub(t *testing.T) {
USDA := &Currency{
code: "USD",
exponent: 2,
}
USDB := &Currency{
code: "USD",
exponent: 3,
}
m1 := New(20000, USDA)
m2 := New(10000, USDB)
_, err := m1.Sub(m2)
if err != ErrCurrencyExponentMismatch {
t.Error("unexpected error")
t.Log(err)
}
}