money_test.go   F
last analyzed

Size/Duplication

Total Lines 335
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
cc 78
eloc 201
dl 0
loc 335
rs 2.16
c 0
b 0
f 0

17 Methods

Rating   Name   Duplication   Size   Complexity  
A money.TestMoney_LessThan 0 16 4
A money.TestMoney_Allocate2 0 6 3
A money.TestMoney_New 0 15 4
B money.TestMoney_Allocate 0 24 6
A money.TestMoney_Amount 0 11 3
A money.TestMoney_GreaterThanOrEqual 0 17 4
A money.TestMoney_GreaterThan 0 16 4
A money.TestMoney_Equals 0 18 5
A money.TestMoney_Currency 0 5 2
A money.TestMoney_Add2 0 7 3
F money.TestMoney_Comparison 0 33 15
A money.TestMoney_Add 0 23 5
A money.TestMoney_Subtract 0 23 5
A money.TestMoney_Multiply 0 18 4
A money.TestMoney_LessThanOrEqual 0 17 4
A money.TestMoney_Subtract2 0 7 3
A money.TestMoney_Display 0 14 4
1
package money
2
3
import (
4
	"reflect"
5
	"testing"
6
)
7
8
func TestMoney_New(t *testing.T) {
9
	m := New(1, EUR())
10
11
	if m.Amount().Value() != 1 {
12
		t.Errorf("Expected %d got %d", 1, m.Amount().Value())
13
	}
14
15
	if m.Currency().code != "EUR" {
16
		t.Errorf("Expected currency %s got %s", "EUR", m.Currency().code)
17
	}
18
19
	m = New(-1, USD())
20
21
	if m.Amount().Value() != -1 {
22
		t.Errorf("Expected %d got %d", 1, m.Amount().Value())
23
	}
24
}
25
26
func TestMoney_Amount(t *testing.T) {
27
	m := New(100, USD())
28
29
	if m.Amount().Value() != 100 {
30
		t.Errorf("Expected %d got %d", 1, m.Amount())
31
	}
32
33
	m = New(-100, USD())
34
35
	if m.Amount().Value() != -100 {
36
		t.Errorf("Expected %d got %d", 1, m.Amount())
37
	}
38
}
39
40
func TestMoney_Currency(t *testing.T) {
41
	m := New(1, EUR())
42
43
	if m.Currency().code != "EUR" {
44
		t.Errorf("Expected currency %s got %s", "EUR", m.Currency().code)
45
	}
46
}
47
48
func TestMoney_Equals(t *testing.T) {
49
	m := New(0, EUR())
50
	tcs := []struct {
51
		amount   int64
52
		expected bool
53
	}{
54
		{-1, false},
55
		{0, true},
56
		{1, false},
57
	}
58
59
	for _, tc := range tcs {
60
		om := New(tc.amount, EUR())
61
		r, err := m.Equals(om)
62
63
		if err != nil || r != tc.expected {
64
			t.Errorf("Expected %d Equals %d == %t got %t", m.Amount().Value(),
65
				om.Amount().Value(), tc.expected, r)
66
		}
67
	}
68
}
69
70
func TestMoney_GreaterThan(t *testing.T) {
71
	m := New(10, EUR())
72
	tcs := []struct {
73
		money    *Money
74
		expected bool
75
	}{
76
		{New(0, EUR()), true},
77
		{New(100, EUR()), false},
78
	}
79
80
	for _, tc := range tcs {
81
		r, _ := m.GreaterThan(tc.money)
82
83
		if r != tc.expected {
84
			t.Errorf("Expected %d to be greater than %d, expect %t got %t", m.Amount().Value(),
85
				tc.money.Amount().Value(), tc.expected, r)
86
		}
87
	}
88
}
89
90
func TestMoney_GreaterThanOrEqual(t *testing.T) {
91
	m := New(10, EUR())
92
	tcs := []struct {
93
		money    *Money
94
		expected bool
95
	}{
96
		{New(0, EUR()), true},
97
		{New(10, EUR()), true},
98
		{New(100, EUR()), false},
99
	}
100
101
	for _, tc := range tcs {
102
		r, _ := m.GreaterThanOrEqual(tc.money)
103
104
		if r != tc.expected {
105
			t.Errorf("Expected %d to be greater than or equal %d, expect %t got %t", m.Amount().Value(),
106
				tc.money.Amount().Value(), tc.expected, r)
107
		}
108
	}
109
}
110
111
func TestMoney_LessThan(t *testing.T) {
112
	m := New(10, EUR())
113
	tcs := []struct {
114
		money    *Money
115
		expected bool
116
	}{
117
		{New(0, EUR()), false},
118
		{New(100, EUR()), true},
119
	}
120
121
	for _, tc := range tcs {
122
		r, _ := m.LessThan(tc.money)
123
124
		if r != tc.expected {
125
			t.Errorf("Expected %d to be less than %d, expect %t got %t", m.Amount().Value(),
126
				tc.money.Amount().Value(), tc.expected, r)
127
		}
128
	}
129
}
130
131
func TestMoney_LessThanOrEqual(t *testing.T) {
132
	m := New(10, EUR())
133
	tcs := []struct {
134
		money    *Money
135
		expected bool
136
	}{
137
		{New(0, EUR()), false},
138
		{New(10, EUR()), true},
139
		{New(100, EUR()), true},
140
	}
141
142
	for _, tc := range tcs {
143
		r, _ := m.LessThanOrEqual(tc.money)
144
145
		if r != tc.expected {
146
			t.Errorf("Expected %d to be less than or equal %d, expect %t got %t", m.Amount().Value(),
147
				tc.money.Amount().Value(), tc.expected, r)
148
		}
149
	}
150
}
151
152
func TestMoney_Add(t *testing.T) {
153
	tcs := []struct {
154
		amount1  int64
155
		amount2  int64
156
		expected int64
157
	}{
158
		{5, 5, 10},
159
		{10, 5, 15},
160
		{1, -1, 0},
161
	}
162
163
	for _, tc := range tcs {
164
		m := New(tc.amount1, EUR())
165
		om := New(tc.amount2, EUR())
166
		r, err := m.Add(om)
167
168
		if err != nil {
169
			t.Error(err)
170
		}
171
172
		if r.Amount().Value() != tc.expected {
173
			t.Errorf("Expected %d + %d = %d got %d", tc.amount1, tc.amount2,
174
				tc.expected, r.Amount().Value())
175
		}
176
	}
177
178
}
179
180
func TestMoney_Add2(t *testing.T) {
181
	m := New(100, EUR())
182
	dm := New(100, USD())
183
	r, err := m.Add(dm)
184
185
	if r != nil || err == nil {
186
		t.Error("Expected err")
187
	}
188
}
189
190
func TestMoney_Subtract(t *testing.T) {
191
	tcs := []struct {
192
		amount1  int64
193
		amount2  int64
194
		expected int64
195
	}{
196
		{5, 5, 0},
197
		{10, 5, 5},
198
		{1, -1, 2},
199
	}
200
201
	for _, tc := range tcs {
202
		m := New(tc.amount1, EUR())
203
		om := New(tc.amount2, EUR())
204
		r, err := m.Subtract(om)
205
206
		if err != nil {
207
			t.Error(err)
208
		}
209
210
		if r.Amount().Value() != tc.expected {
211
			t.Errorf("Expected %d - %d = %d got %d", tc.amount1, tc.amount2,
212
				tc.expected, r.Amount().Value())
213
		}
214
	}
215
}
216
217
func TestMoney_Subtract2(t *testing.T) {
218
	m := New(100, EUR())
219
	dm := New(100, USD())
220
	r, err := m.Subtract(dm)
221
222
	if r != nil || err == nil {
223
		t.Error("Expected err")
224
	}
225
}
226
227
func TestMoney_Multiply(t *testing.T) {
228
	tcs := []struct {
229
		amount     int64
230
		multiplier int64
231
		expected   int64
232
	}{
233
		{5, 5, 25},
234
		{10, 5, 50},
235
		{1, -1, -1},
236
		{1, 0, 0},
237
	}
238
239
	for _, tc := range tcs {
240
		m := New(tc.amount, EUR())
241
		r := m.Multiply(tc.multiplier).Amount().Value()
242
243
		if r != tc.expected {
244
			t.Errorf("Expected %d * %d = %d got %d", tc.amount, tc.multiplier, tc.expected, r)
245
		}
246
	}
247
}
248
249
func TestMoney_Allocate(t *testing.T) {
250
	tcs := []struct {
251
		amount   int64
252
		ratios   []int
253
		expected []int64
254
	}{
255
		{100, []int{50, 50}, []int64{50, 50}},
256
		{100, []int{30, 30, 30}, []int64{34, 33, 33}},
257
		{200, []int{1, 1, 1}, []int64{67, 67, 66}},
258
		{5, []int{3, 7}, []int64{2, 3}},
259
	}
260
261
	for _, tc := range tcs {
262
		m := New(tc.amount, EUR())
263
		var rs []int64
264
		split, _ := m.Allocate(tc.ratios...)
265
266
		for _, party := range split {
267
			rs = append(rs, party.Amount().Value())
268
		}
269
270
		if !reflect.DeepEqual(tc.expected, rs) {
271
			t.Errorf("Expected allocation of %d for ratios %v to be %v got %v", tc.amount, tc.ratios,
272
				tc.expected, rs)
273
		}
274
	}
275
}
276
277
func TestMoney_Allocate2(t *testing.T) {
278
	m := New(100, EUR())
279
	r, err := m.Allocate()
280
281
	if r != nil || err == nil {
282
		t.Error("Expected err")
283
	}
284
}
285
286
func TestMoney_Comparison(t *testing.T) {
287
	dollar := New(100, USD())
288
	twoDollar := New(200, USD())
289
	twoEuro := New(200, EUR())
290
291
	if r, err := dollar.GreaterThan(twoDollar); err != nil || r {
292
		t.Errorf("Expected %d Greater Than %d == %t got %t", dollar.Amount().Value(),
293
			twoDollar.Amount().Value(), false, r)
294
	}
295
296
	if r, err := dollar.LessThan(twoDollar); err != nil || !r {
297
		t.Errorf("Expected %d Less Than %d == %t got %t", dollar.Amount().Value(),
298
			twoDollar.Amount().Value(), true, r)
299
	}
300
301
	if r, err := dollar.LessThan(twoEuro); err == nil || r {
302
		t.Error("Expected err")
303
	}
304
305
	if r, err := dollar.GreaterThan(twoEuro); err == nil || r {
306
		t.Error("Expected err")
307
	}
308
309
	if r, err := dollar.Equals(twoEuro); err == nil || r {
310
		t.Error("Expected err")
311
	}
312
313
	if r, err := dollar.LessThanOrEqual(twoEuro); err == nil || r {
314
		t.Error("Expected err")
315
	}
316
317
	if r, err := dollar.GreaterThanOrEqual(twoEuro); err == nil || r {
318
		t.Error("Expected err")
319
	}
320
}
321
322
func TestMoney_Display(t *testing.T) {
323
	euro := New(100, EUR())
324
	if r := euro.Display(); r != "€1,00" {
325
		t.Errorf("Expected money display to be %s got %s", "€1,00", r)
326
	}
327
328
	dollar := New(-200000, USD())
329
	if r := dollar.Display(); r != "-$2,000.00" {
330
		t.Errorf("Expected money display to be %s got %s", "-$2,000.00", r)
331
	}
332
333
	cent := New(1, EUR())
334
	if r := cent.Display(); r != "€0,01" {
335
		t.Errorf("Expected money display to be %s got %s", "€0,01", r)
336
	}
337
}
338