-
Notifications
You must be signed in to change notification settings - Fork 0
/
money_test.go
133 lines (111 loc) · 4.4 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
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
package money
import (
"github.com/stretchr/testify/assert"
"testing"
)
func TestNewFromString(t *testing.T) {
// Test valid input with default currency
m, err := NewFromString("10.00")
assert.Nil(t, err, "NewFromString should not return an error for valid input")
assert.Equal(t, int64(1000), m.amountCents, "NewFromString should create a Money instance with the correct amountCents")
assert.Equal(t, USD, m.currency, "NewFromString should create a Money instance with the default currency (USD)")
// Test valid input with specified currency
m, err = NewFromString("10.00", EUR)
assert.Nil(t, err, "NewFromString should not return an error for valid input")
assert.Equal(t, int64(1000), m.amountCents, "NewFromString should create a Money instance with the correct amountCents")
assert.Equal(t, EUR, m.currency, "NewFromString should create a Money instance with the specified currency")
// Test invalid input
m, err = NewFromString("invalid")
assert.NotNil(t, err, "NewFromString should return an error for invalid input")
assert.Nil(t, m, "NewFromString should not return a Money instance for invalid input")
// Test invalid currency
m, err = NewFromString("10.00", "XYZ")
assert.NotNil(t, err, "NewFromString should return an error for an invalid currency")
assert.Nil(t, m, "NewFromString should not return a Money instance for an invalid currency")
}
func TestMustFromString(t *testing.T) {
// Test valid input with default currency
m := MustFromString("10.00")
assert.Equal(t, int64(1000), m.amountCents, "MustFromString should create a Money instance with the correct amountCents")
assert.Equal(t, USD, m.currency, "MustFromString should create a Money instance with the default currency (USD)")
// Test valid input with specified currency
m = MustFromString("10.00", EUR)
assert.Equal(t, int64(1000), m.amountCents, "MustFromString should create a Money instance with the correct amountCents")
assert.Equal(t, EUR, m.currency, "MustFromString should create a Money instance with the specified currency")
// Test invalid input (should panic)
assert.Panics(t, func() {
MustFromString("invalid")
}, "MustFromString should panic for invalid input")
// Test invalid currency (should panic)
assert.Panics(t, func() {
MustFromString("10.00", "XYZ")
}, "MustFromString should panic for an invalid currency")
}
func TestAdd(t *testing.T) {
m1 := MustFromString("10.00")
m2 := MustFromString("5.00")
expected := MustFromString("15.00")
result := m1.Add(m2)
assert.Equal(t, expected, result, "Add result should match expected")
}
func TestAddCentsInt(t *testing.T) {
m1 := MustFromString("10.00")
c := 500
expected := MustFromString("15.00")
result := m1.AddCentsInt(c)
assert.Equal(t, expected, result, "AddCentsInt result should match expected")
}
func TestAddDollarInt(t *testing.T) {
m1 := MustFromString("10.00")
d := 5
expected := MustFromString("15.00")
result := m1.AddDollarInt(d)
assert.Equal(t, expected, result, "AddDollarInt result should match expected")
}
func TestSubStr(t *testing.T) {
m1 := MustFromString("10.00")
expected := MustFromString("5.00")
result := m1.SubStr("5.00")
assert.Equal(t, expected, result, "SubStr result should match expected")
}
func TestSub(t *testing.T) {
m1 := MustFromString("10.00")
m2 := MustFromString("5.00")
expected := MustFromString("5.00")
result := m1.Sub(m2)
assert.Equal(t, expected, result, "Sub result should match expected")
}
func TestSubCentsInt(t *testing.T) {
m1 := MustFromString("10.00")
c := 500
expected := MustFromString("5.00")
result := m1.SubCentsInt(c)
assert.Equal(t, expected, result, "SubCentsInt result should match expected")
}
func TestSubDollarInt(t *testing.T) {
m1 := MustFromString("10.00")
d := 5
expected := MustFromString("5.00")
result := m1.SubDollarInt(d)
assert.Equal(t, expected, result, "SubDollarInt result should match expected")
}
func TestMul(t *testing.T) {
// Create a Money instance to multiply
m := MustFromString("10.00")
// Multiply the Money instance by a number
mul := m.Mul(2)
// Check that the result is as expected
if mul.ToCentsInt() != 2000 {
t.Errorf("Expected 2000 cents, got %d", mul.ToCentsInt())
}
}
func TestDiv(t *testing.T) {
// Create a Money instance to divide
m := MustFromString("10.00")
// Divide the Money instance by a number
div := m.Div(2)
// Check that the result is as expected
if div.ToCentsInt() != 500 {
t.Errorf("Expected 500 cents, got %d", div.ToCentsInt())
}
}