This repository has been archived by the owner on Aug 17, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
currency_test.go
118 lines (98 loc) · 2.22 KB
/
currency_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
package money_test
import (
"testing"
"github.com/deixis/money"
)
func TestParseCurrency(t *testing.T) {
t.Parallel()
table := []struct {
input string
expect money.Currency
err error
}{
{input: "CHF", expect: "CHF"},
{input: " chf ", expect: "CHF"},
{input: "cHf ", expect: "CHF"},
{input: "USD", expect: "USD"},
{input: "XXX", expect: money.Currency("XXX")},
}
for i, test := range table {
res, err := money.ParseCurrency(test.input)
if err != nil {
if test.err != err {
t.Errorf("#%d - expect error %s, but got %s - %s", i, test.err, err, test.input)
}
continue
}
if test.expect != res {
t.Errorf("#%d - expect %s, but got %s - %s", i, test.expect, res, test.input)
}
}
}
func TestCurency_UnmarshalJSON(t *testing.T) {
t.Parallel()
table := []struct {
input money.Currency
}{
{input: "CHF"},
{input: "USD"},
{input: "CNY"},
}
for i, test := range table {
data, err := test.input.MarshalJSON()
if err != nil {
t.Fatal(err)
}
var res money.Currency
if err := res.UnmarshalJSON(data); err != nil {
t.Fatal(err)
}
if test.input != res {
t.Errorf("#%d - expect %s, but got %s", i, test.input, res)
}
}
}
func TestCurency_GobEncode(t *testing.T) {
t.Parallel()
table := []struct {
input money.Currency
}{
{input: "CHF"},
{input: "USD"},
{input: "CNY"},
}
for i, test := range table {
data, err := test.input.GobEncode()
if err != nil {
t.Fatal(err)
}
var res money.Currency
if err := res.GobDecode(data); err != nil {
t.Fatal(err)
}
if test.input != res {
t.Errorf("#%d - expect %s, but got %s", i, test.input, res)
}
}
}
func Test_UnoficialCurrency(t *testing.T) {
t.Parallel()
table := []struct {
input money.Currency
}{
{input: "ETH"},
{input: "USDC"},
{input: "DAI"},
}
for i, test := range table {
_, err := money.ParseCurrency(test.input.String())
if err != money.ErrInvalidCurrency {
t.Fatalf("#%d - expect unoficial currency to fail when not registered", i)
}
money.RegisterUnoficialCurrency(test.input.String())
_, err = money.ParseCurrency(test.input.String())
if err != nil {
t.Errorf("#%d - expect unoficial currency to be valid when registered, but got %s", i, err)
}
}
}