forked from martinusso/money
-
Notifications
You must be signed in to change notification settings - Fork 0
/
money_test.go
152 lines (134 loc) · 3.11 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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package money
import (
"testing"
)
func TestAbsolute(t *testing.T) {
values := map[float64]float64{
1.99: 1.99,
42.987: 42.99,
-12345.9: 12345.90,
-1234567890.934: 1234567890.93,
-1: 1.00,
}
for k, v := range values {
got := New(k, USD).Absolute()
if got != v {
t.Errorf("Expected '%f', got '%f'", v, got)
}
}
}
func TestCurrency(t *testing.T) {
expected := "$ 123,456.79"
if got := New(0123456.789, USD).Currency(); got != expected {
t.Errorf("Expected '%s', got '%s'", expected, got)
}
expected = "R$ 123.456,74"
if got := New(0123456.742, BRL).Currency(); got != expected {
t.Errorf("Expected '%s', got '%s'", expected, got)
}
expected = "R$ 1,99"
if got := New(01.994, BRL).Currency(); got != expected {
t.Errorf("Expected '%s', got '%s'", expected, got)
}
}
func TestFormatted(t *testing.T) {
expected := "123,456.79"
if got := New(0123456.789, USD).Formatted(); got != expected {
t.Errorf("Expected '%s', got '%s'", expected, got)
}
expected = "123.456,74"
if got := New(0123456.742, BRL).Formatted(); got != expected {
t.Errorf("Expected '%s', got '%s'", expected, got)
}
}
func TestFormat(t *testing.T) {
got := New(0123456.789, BRL).Format('.', ',')
expected := "123.456,79"
if got != expected {
t.Errorf("Expected '%s', got '%s'", expected, got)
}
}
func TestCalculator(t *testing.T) {
m := New(12345.9, USD)
// subtract
got := m.Subtract(42)
expected := 12303.9
if got != expected {
t.Errorf("Expected '%f', got '%f'", expected, got)
}
// sum
got = m.Sum(57.954)
expected = 12361.85
if got != expected {
t.Errorf("Expected '%f', got '%f'", expected, got)
}
}
func TestCompare(t *testing.T) {
values := map[int]map[float64]float64{
-1: {
2.00: 2.005,
},
0: {
1.99: 1.989,
2.0: 1.999,
3.0: 3.004,
},
1: {
2.0: 1.99,
3.005: 3.0,
},
}
for c, v := range values {
for v1, v2 := range v {
got := New(v1, USD).Compare(v2)
if got != c {
t.Errorf("Expected '%d', got '%d' ('%f', '%f')", c, got, v1, v2)
}
}
}
}
func TestEquals(t *testing.T) {
v1 := 1.99
v2 := 1.9888
if !New(v1, USD).Equals(v2) {
t.Errorf("Should be equals ('%f', '%f')", v1, v2)
}
}
func TestGreaterThan(t *testing.T) {
v1 := 1.998
v2 := 1.99
if !New(v1, USD).GreaterThan(v2) {
t.Errorf("Should be greater than ('%f', '%f')", v1, v2)
}
}
func TestGreaterThanOrEqual(t *testing.T) {
v1 := 2.00
v2 := 1.99
if !New(v1, USD).GreaterThanOrEqual(v2) {
t.Errorf("Should be greater than or equal ('%f', '%f')", v1, v2)
}
v1 = 1.9898
v2 = 1.99
if !New(v1, USD).GreaterThanOrEqual(v2) {
t.Errorf("Should be greater than or equal ('%f', '%f')", v1, v2)
}
}
func TestLessThan(t *testing.T) {
v1 := 1.98
v2 := 1.99
if !New(v1, USD).LessThan(v2) {
t.Errorf("Should be less than ('%f', '%f')", v1, v2)
}
}
func TestLessThanOrEqual(t *testing.T) {
v1 := 123.45
v2 := 123.456
if !New(v1, USD).LessThanOrEqual(v2) {
t.Errorf("Should be less than or equal ('%f', '%f')", v1, v2)
}
v1 = 3.98
v2 = 3.978
if !New(v1, USD).LessThanOrEqual(v2) {
t.Errorf("Should be less than or equal ('%f', '%f')", v1, v2)
}
}