-
Notifications
You must be signed in to change notification settings - Fork 6
/
money_test.go
156 lines (123 loc) · 3.76 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
153
154
155
156
package money
import (
"testing"
)
func TestFormatWithFloatValue(t *testing.T) {
actual := Format(10.00)
expected := "$10.00"
if actual != expected {
t.Errorf("Expected %s but got %s", expected, actual)
}
}
func TestFormatWithNegativeValue(t *testing.T) {
actual := Format(-10)
expected := "-$10.00"
if actual != expected {
t.Errorf("Expected %s but got %s", expected, actual)
}
}
func TestWithFloatNegativeValue(t *testing.T) {
actual := Format(-10.00)
expected := "-$10.00"
if actual != expected {
t.Errorf("Expected %s but got %s", expected, actual)
}
}
func TestFormatWithDefaults(t *testing.T) {
currency := Format(10)
expected := "$10.00"
if currency != expected {
t.Errorf("Expected %s but got %s", expected, currency)
}
}
func TestFormatWithCurrency(t *testing.T) {
currency := Format(10, Options{"with_currency": true})
expected := "$10.00 USD"
if currency != expected {
t.Errorf("Expected %s but got %s", expected, currency)
}
}
func TestFormatWithCurrencyWhenCanadian(t *testing.T) {
currency := Format(10, Options{"currency": "CAD", "with_currency": true})
expected := "$10.00 CAD"
if currency != expected {
t.Errorf("Expected %s but got %s", expected, currency)
}
}
func TestFormatWhenJapanese(t *testing.T) {
currency := Format(10, Options{"currency": "JPY"})
expected := "¥10"
if currency != expected {
t.Errorf("Expected %s but got %s", expected, currency)
}
}
func TestFormatWhenJapaneseOverThousand(t *testing.T) {
currency := Format(1000, Options{"currency": "JPY"})
expected := "¥1,000"
if currency != expected {
t.Errorf("Expected %s but got %s", expected, currency)
}
}
func TestFormatWhenJapaneseOverTenThousand(t *testing.T) {
currency := Format(10000, Options{"currency": "JPY"})
expected := "¥10,000"
if currency != expected {
t.Errorf("Expected %s but got %s", expected, currency)
}
}
func TestFormatWhenJapaneseOverHundredThousand(t *testing.T) {
currency := Format(100000, Options{"currency": "JPY"})
expected := "¥100,000"
if currency != expected {
t.Errorf("Expected %s but got %s", expected, currency)
}
}
func TestFormatWhenJapaneseOverMillion(t *testing.T) {
currency := Format(1000000, Options{"currency": "JPY"})
expected := "¥1,000,000"
if currency != expected {
t.Errorf("Expected %s but got %s", expected, currency)
}
}
func TestFormatWithoutCents(t *testing.T) {
currency := Format(10, Options{"with_cents": false})
expected := "$10"
if currency != expected {
t.Errorf("Expected %s but got %s", expected, currency)
}
}
func TestAddSymbol(t *testing.T) {
q := "10.00"
if afn := addSymbol(q, currencies["AFN"], defaults()); afn != "10.00؋" {
t.Errorf("Expected euro symbol to be placed after quantity, but got %s", afn)
}
if afn := addSymbol(q, currencies["AFN"], Options{"with_symbol_space": true}); afn != "10.00 ؋" {
t.Errorf("Expected euro symbol to be placed after quantity and a space, but got %s", afn)
}
if usd := addSymbol(q, currencies["USD"], defaults()); usd != "$10.00" {
t.Errorf("Expected dollar symbol to be placed before quantity, but got %s", usd)
}
if usd := addSymbol(q, currencies["USD"], Options{"with_symbol_space": true}); usd != "$ 10.00" {
t.Errorf("Expected dollar symbol to be placed before quantity and a space, but got %s", usd)
}
}
func TestSeparateThousands(t *testing.T) {
values := map[string]string{
"1": "1",
"12": "12",
"123": "123",
"1234": "1,234",
"12345": "12,345",
"69310": "69,310",
"123456": "123,456",
"1234567": "1,234,567",
"12345678": "12,345,678",
"123456789": "123,456,789",
"1234567891": "1,234,567,891",
}
for value, expected := range values {
if v := separateThousands(value, ","); v != expected {
t.Errorf("Expected %s to be %s", v, expected)
}
}
}