-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaccounting_test.go
179 lines (143 loc) · 7.61 KB
/
accounting_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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
package accounting
import (
"fmt"
"math/big"
"runtime"
"testing"
"github.com/cockroachdb/apd"
"github.com/shopspring/decimal"
)
func AssertEqual(t *testing.T, x, y string) {
if x != y {
t.Error("Expected ", y, ", got ", x)
}
}
func init() {
fmt.Printf("version: %s", runtime.Version())
}
func TestAccounting_SetFormat(t *testing.T) {
accounting := DefaultAccounting("$", 2)
accounting.SetFormat("%v %s")
AssertEqual(t, accounting.FormatMoney(123456789.213123), "123,456,789.21 $")
}
func TestAccounting_SetFormatZero(t *testing.T) {
accounting := DefaultAccounting("$", 2)
accounting.SetFormatZero("%s --")
AssertEqual(t, accounting.FormatMoney(0), "$ --")
}
func TestAccounting_SetFormatNegative(t *testing.T) {
accounting := DefaultAccounting("$", 2)
accounting.SetFormatNegative("%s(%v)")
AssertEqual(t, accounting.FormatMoney(-123456789.213123), "$(123,456,789.21)")
}
func TestAccounting_SetThousandSeparator(t *testing.T) {
accounting := DefaultAccounting("$", 2)
accounting.SetThousandSeparator("'")
AssertEqual(t, accounting.FormatMoney(123456789.213123), "$123'456'789.21")
}
func TestAccounting_SetDecimalSeparator(t *testing.T) {
accounting := DefaultAccounting("$", 2)
accounting.SetDecimalSeparator("'")
AssertEqual(t, accounting.FormatMoney(123456789.213123), "$123,456,789'21")
}
func TestFormatMoney(t *testing.T) {
accounting := DefaultAccounting("$", 2)
AssertEqual(t, accounting.FormatMoney(123456789.213123), "$123,456,789.21")
AssertEqual(t, accounting.FormatMoney(12345678), "$12,345,678.00")
AssertEqual(t, accounting.FormatMoney(-12345678), "-$12,345,678.00")
AssertEqual(t, accounting.FormatMoney(0), "$0.00")
AssertEqual(t, accounting.FormatMoney(big.NewRat(77777777, 3)), "$25,925,925.67")
AssertEqual(t, accounting.FormatMoney(big.NewRat(-77777777, 3)), "-$25,925,925.67")
AssertEqual(t, accounting.FormatMoney(apd.New(499999, -2)), "$4,999.99")
AssertEqual(t, accounting.FormatMoney(apd.New(500000, 0)), "$500,000.00")
AssertEqual(t, accounting.FormatMoney(decimal.New(499999, -2)), "$4,999.99")
AssertEqual(t, accounting.FormatMoney(decimal.New(500000, 0)), "$500,000.00")
accounting = NewAccounting("$", 0, ",", ".", "%s %v", "-%s %v", "%s %v")
AssertEqual(t, accounting.FormatMoney(123456789.213123), "$ 123,456,789")
AssertEqual(t, accounting.FormatMoney(12345678), "$ 12,345,678")
AssertEqual(t, accounting.FormatMoney(-12345678), "-$ 12,345,678")
AssertEqual(t, accounting.FormatMoney(0), "$ 0")
accounting2 := Accounting{Symbol: "€", Precision: 2, Thousand: ".", Decimal: ","}
AssertEqual(t, accounting2.FormatMoney(4999.99), "€4.999,99")
accounting2 = Accounting{Symbol: "£ ", Precision: 0}
AssertEqual(t, accounting2.FormatMoney(500000), "£ 500,000")
accounting2 = Accounting{Symbol: "GBP", Precision: 0,
Format: "%s %v", FormatNegative: "%s (%v)", FormatZero: "%s --"}
AssertEqual(t, accounting2.FormatMoney(1000000), "GBP 1,000,000")
AssertEqual(t, accounting2.FormatMoney(-5000), "GBP (5,000)")
AssertEqual(t, accounting2.FormatMoney(0), "GBP --")
accounting2 = Accounting{Symbol: "GBP", Precision: 2,
Format: "%s %v", FormatNegative: "%s (%v)", FormatZero: "%s --"}
AssertEqual(t, accounting2.FormatMoney(1000000), "GBP 1,000,000.00")
AssertEqual(t, accounting2.FormatMoney(-5000), "GBP (5,000.00)")
AssertEqual(t, accounting2.FormatMoney(0), "GBP --")
accounting2 = Accounting{Symbol: "€", Precision: 2,
Decimal: ",", FormatZero: "0.-"}
AssertEqual(t, accounting2.FormatMoney(0), "0.-")
}
func TestFormatMoneyInt(t *testing.T) {
accounting := Accounting{Symbol: "$", Precision: 2}
AssertEqual(t, accounting.FormatMoneyInt(12345678), "$12,345,678.00")
accounting = Accounting{Symbol: "€", Precision: 2, Thousand: ".", Decimal: ","}
AssertEqual(t, accounting.FormatMoneyInt(4999), "€4.999,00")
accounting = Accounting{Symbol: "£ ", Precision: 0}
AssertEqual(t, accounting.FormatMoneyInt(500000), "£ 500,000")
accounting = Accounting{Symbol: "GBP", Precision: 0,
Format: "%s %v", FormatNegative: "%s (%v)", FormatZero: "%s --"}
AssertEqual(t, accounting.FormatMoneyInt(1000000), "GBP 1,000,000")
AssertEqual(t, accounting.FormatMoneyInt(-5000), "GBP (5,000)")
AssertEqual(t, accounting.FormatMoneyInt(0), "GBP --")
}
func TestFormatMoneyFloat64(t *testing.T) {
accounting := Accounting{Symbol: "$", Precision: 2}
AssertEqual(t, accounting.FormatMoneyFloat64(123456789.213123), "$123,456,789.21")
accounting = Accounting{Symbol: "€", Precision: 2, Thousand: ".", Decimal: ","}
AssertEqual(t, accounting.FormatMoneyFloat64(4999.99), "€4.999,99")
accounting = Accounting{Symbol: "£ ", Precision: 0}
AssertEqual(t, accounting.FormatMoneyFloat64(500000.0), "£ 500,000")
accounting = Accounting{Symbol: "GBP", Precision: 0,
Format: "%s %v", FormatNegative: "%s (%v)", FormatZero: "%s --"}
AssertEqual(t, accounting.FormatMoneyFloat64(1000000.0), "GBP 1,000,000")
AssertEqual(t, accounting.FormatMoneyFloat64(-5000.0), "GBP (5,000)")
AssertEqual(t, accounting.FormatMoneyFloat64(0.0), "GBP --")
}
func TestFormatMoneyBigRat(t *testing.T) {
accounting := Accounting{Symbol: "$", Precision: 2}
AssertEqual(t, accounting.FormatMoneyBigRat(big.NewRat(77777777, 3)), "$25,925,925.67")
AssertEqual(t, accounting.FormatMoneyBigRat(big.NewRat(-77777777, 3)), "-$25,925,925.67")
accounting = Accounting{Symbol: "€", Precision: 2, Thousand: ".", Decimal: ","}
AssertEqual(t, accounting.FormatMoneyBigRat(big.NewRat(77777777, 3)), "€25.925.925,67")
accounting = Accounting{Symbol: "£ ", Precision: 0}
AssertEqual(t, accounting.FormatMoneyBigRat(big.NewRat(77777777, 3)), "£ 25,925,926")
accounting = Accounting{Symbol: "GBP", Precision: 0,
Format: "%s %v", FormatNegative: "%s (%v)", FormatZero: "%s --"}
AssertEqual(t, accounting.FormatMoneyBigRat(big.NewRat(77777777, 3)), "GBP 25,925,926")
AssertEqual(t, accounting.FormatMoneyBigRat(big.NewRat(-77777777, 3)), "GBP (25,925,926)")
AssertEqual(t, accounting.FormatMoneyBigRat(big.NewRat(0, 3)), "GBP --")
}
func TestFormatMoneyBigDecimal(t *testing.T) {
accounting := Accounting{Symbol: "$", Precision: 2}
AssertEqual(t, accounting.FormatMoneyBigDecimal(apd.New(123456789213123, -6)), "$123,456,789.21")
accounting = Accounting{Symbol: "€", Precision: 2, Thousand: ".", Decimal: ","}
AssertEqual(t, accounting.FormatMoneyBigDecimal(apd.New(499999, -2)), "€4.999,99")
accounting = Accounting{Symbol: "£ ", Precision: 0}
AssertEqual(t, accounting.FormatMoneyBigDecimal(apd.New(500000, 0)), "£ 500,000")
accounting = Accounting{Symbol: "GBP", Precision: 0,
Format: "%s %v", FormatNegative: "%s (%v)", FormatZero: "%s --"}
AssertEqual(t, accounting.FormatMoneyBigDecimal(apd.New(1000000, 0)), "GBP 1,000,000")
AssertEqual(t, accounting.FormatMoneyBigDecimal(apd.New(-5000, 0)), "GBP (5,000)")
AssertEqual(t, accounting.FormatMoneyBigDecimal(apd.New(0, 0)), "GBP --")
}
func TestFormatMoneyDecimal(t *testing.T) {
accounting := Accounting{Symbol: "$", Precision: 2}
AssertEqual(t, accounting.FormatMoneyDecimal(decimal.New(123456789213123, -6)), "$123,456,789.21")
accounting = Accounting{Symbol: "€", Precision: 2, Thousand: ".", Decimal: ","}
AssertEqual(t, accounting.FormatMoneyDecimal(decimal.New(499999, -2)), "€4.999,99")
accounting = Accounting{Symbol: "£ ", Precision: 0}
AssertEqual(t, accounting.FormatMoneyDecimal(decimal.New(500000, 0)), "£ 500,000")
accounting = Accounting{Symbol: "GBP", Precision: 0,
Format: "%s %v", FormatNegative: "%s (%v)", FormatZero: "%s --"}
AssertEqual(t, accounting.FormatMoneyDecimal(decimal.New(1000000, 0)), "GBP 1,000,000")
AssertEqual(t, accounting.FormatMoneyDecimal(decimal.New(-5000, 0)), "GBP (5,000)")
AssertEqual(t, accounting.FormatMoneyDecimal(decimal.New(0, 0)), "GBP --")
}