generated from dogmatiq/template-go
-
Notifications
You must be signed in to change notification settings - Fork 1
/
math_test.go
216 lines (192 loc) · 5.42 KB
/
math_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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
package dosh_test
import (
. "github.com/dogmatiq/dosh"
. "github.com/onsi/ginkgo"
. "github.com/onsi/ginkgo/extensions/table"
. "github.com/onsi/gomega"
"github.com/shopspring/decimal"
)
var _ = Describe("type Amount (math methods)", func() {
Describe("func Abs()", func() {
DescribeTable(
"it returns an amount with the absolute magnitude",
func(dec string, expect string) {
a := FromString("XYZ", dec).Abs()
x := FromString("XYZ", expect)
Expect(a.EqualTo(x)).To(BeTrue())
},
Entry("zero", "0", "0"),
Entry("positive", "1.23", "1.23"),
Entry("negative", "-1.23", "1.23"),
)
})
Describe("func Neg()", func() {
DescribeTable(
"it returns an amount with the inverse magnitude",
func(dec string, expect string) {
a := FromString("XYZ", dec).Neg()
x := FromString("XYZ", expect)
Expect(a.EqualTo(x)).To(BeTrue())
},
Entry("zero", "0", "0"),
Entry("positive", "1.23", "-1.23"),
Entry("negative", "-1.23", "1.23"),
)
})
Describe("func Add()", func() {
It("returns a + b", func() {
a := FromString("XYZ", "1.23")
b := FromString("XYZ", "3.45")
x := FromString("XYZ", "4.68")
Expect(a.Add(b).EqualTo(x)).To(BeTrue())
})
It("panics if the amounts do not have the same currency", func() {
Expect(func() {
a := FromString("XYZ", "1")
b := FromString("ABC", "1")
a.Add(b)
}).To(PanicWith("can not operate on amounts in differing currencies (XYZ vs ABC)"))
})
})
Describe("func Sub()", func() {
It("returns a - b", func() {
a := FromString("XYZ", "1.23")
b := FromString("XYZ", "3.45")
x := FromString("XYZ", "-2.22")
Expect(a.Sub(b).EqualTo(x)).To(BeTrue())
})
It("panics if the amounts do not have the same currency", func() {
Expect(func() {
a := FromString("XYZ", "1")
b := FromString("ABC", "1")
a.Sub(b)
}).To(PanicWith("can not operate on amounts in differing currencies (XYZ vs ABC)"))
})
})
Describe("func MulScalar()", func() {
It("returns a * b", func() {
a := FromString("XYZ", "1.23")
b := decimal.RequireFromString("3.45")
x := FromString("XYZ", "4.2435")
Expect(a.MulScalar(b).EqualTo(x)).To(BeTrue())
})
})
Describe("func Div()", func() {
It("returns a / b", func() {
a := FromString("XYZ", "1.23")
b := FromString("XYZ", "0.5")
x := decimal.RequireFromString("2.46")
Expect(a.Div(b).Equal(x)).To(BeTrue())
})
It("panics if the amounts do not have the same currency", func() {
Expect(func() {
a := FromString("XYZ", "1")
b := FromString("ABC", "1")
a.Div(b)
}).To(PanicWith("can not operate on amounts in differing currencies (XYZ vs ABC)"))
})
It("panics when dividing by zero", func() {
Expect(func() {
Amount{}.Div(Amount{})
}).To(PanicWith("decimal division by 0"))
})
})
Describe("func DivScalar()", func() {
It("returns a / b", func() {
a := FromString("XYZ", "1.23")
b := decimal.RequireFromString("0.5")
x := FromString("XYZ", "2.46")
Expect(a.DivScalar(b).EqualTo(x)).To(BeTrue())
})
It("panics when dividing by zero", func() {
Expect(func() {
Amount{}.DivScalar(decimal.Decimal{})
}).To(PanicWith("decimal division by 0"))
})
})
Describe("func Mod()", func() {
It("returns a % b", func() {
a := FromString("XYZ", "1.23")
b := FromString("XYZ", "0.5")
x := decimal.RequireFromString("0.23")
Expect(a.Mod(b).Equal(x)).To(BeTrue())
})
It("panics if the amounts do not have the same currency", func() {
Expect(func() {
a := FromString("XYZ", "1")
b := FromString("ABC", "1")
a.Mod(b)
}).To(PanicWith("can not operate on amounts in differing currencies (XYZ vs ABC)"))
})
It("panics when dividing by zero", func() {
Expect(func() {
Amount{}.Mod(Amount{})
}).To(PanicWith("decimal division by 0"))
})
})
Describe("func ModScalar()", func() {
It("returns a % b", func() {
a := FromString("XYZ", "1.23")
b := decimal.RequireFromString("0.5")
x := FromString("XYZ", "0.23")
Expect(a.ModScalar(b).EqualTo(x)).To(BeTrue())
})
It("panics when dividing by zero", func() {
Expect(func() {
Amount{}.ModScalar(decimal.Decimal{})
}).To(PanicWith("decimal division by 0"))
})
})
})
var _ = Describe("func Sum()", func() {
It("returns the sum of all amounts", func() {
Expect(
Sum(
FromString("XYZ", "1"),
FromString("XYZ", "2"),
FromString("XYZ", "3"),
).EqualTo(
FromString("XYZ", "6"),
),
).To(BeTrue())
})
It("panics if no amounts are provided", func() {
Expect(func() {
Sum()
}).To(PanicWith("at least one amount must be provided"))
})
It("panics if the amounts do not have the same currency", func() {
Expect(func() {
Sum(
FromString("XYZ", "1"),
FromString("ABC", "1"),
)
}).To(PanicWith("can not operate on amounts in differing currencies (XYZ vs ABC)"))
})
})
var _ = Describe("func Avg()", func() {
It("returns the average (mean) of all values", func() {
Expect(
Avg(
FromString("XYZ", "1"),
FromString("XYZ", "2"),
FromString("XYZ", "3"),
).EqualTo(
FromString("XYZ", "2"),
),
).To(BeTrue())
})
It("panics if no amounts are provided", func() {
Expect(func() {
Avg()
}).To(PanicWith("at least one amount must be provided"))
})
It("panics if the amounts do not have the same currency", func() {
Expect(func() {
Avg(
FromString("XYZ", "1"),
FromString("ABC", "1"),
)
}).To(PanicWith("can not operate on amounts in differing currencies (XYZ vs ABC)"))
})
})