Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add protomoney Sub fix #72

Merged
merged 2 commits into from
Aug 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions protomoney/math.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,23 @@ func Add(a, b *money.Money) *money.Money {
// It panics if a and b do not use the same currency.
func Sub(a, b *money.Money) *money.Money {
assertSameCurrency(a, b)
units := a.Units - b.Units
nanos := a.Nanos - b.Nanos

if units > 0 && nanos < 0 {
nanos = nanosPerUnit + nanos
units--
}

if units < 0 && nanos > 0 {
nanos = -(nanosPerUnit - nanos)
units++
}

m := &money.Money{
CurrencyCode: a.CurrencyCode,
Units: a.Units - b.Units,
Nanos: a.Nanos - b.Nanos,
Units: units,
Nanos: nanos,
}

normalizeInPlace(m)
Expand Down
44 changes: 30 additions & 14 deletions protomoney/math_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,20 +50,36 @@ var _ = Describe("func Add()", func() {
})

var _ = Describe("func Sub()", func() {
It("returns a - b", func() {
a := &money.Money{CurrencyCode: "XYZ", Units: 1, Nanos: 230000000}
b := &money.Money{CurrencyCode: "XYZ", Units: 3, Nanos: 450000000}
x := &money.Money{CurrencyCode: "XYZ", Units: -2, Nanos: -220000000}
Expect(Sub(a, b)).To(Equal(x))
})

It("panics if the amounts do not have the same currency", func() {
Expect(func() {
a := &money.Money{CurrencyCode: "XYZ"}
b := &money.Money{CurrencyCode: "ABC"}
Sub(a, b)
}).To(PanicWith("can not operate on amounts in differing currencies (XYZ vs ABC)"))
})
DescribeTable(
"returns a - b",
func(a, b, x *money.Money) {
Expect(Sub(a, b)).To(Equal(x))
},
Entry(
"negative",
&money.Money{CurrencyCode: "XYZ", Units: 1, Nanos: 230000000},
&money.Money{CurrencyCode: "XYZ", Units: 3, Nanos: 450000000},
&money.Money{CurrencyCode: "XYZ", Units: -2, Nanos: -220000000},
),
Entry(
"positive",
&money.Money{CurrencyCode: "XYZ", Units: 3, Nanos: 450000000},
&money.Money{CurrencyCode: "XYZ", Units: 1, Nanos: 230000000},
&money.Money{CurrencyCode: "XYZ", Units: 2, Nanos: 220000000},
),
Entry(
"positive unit and negative nanos are normalized",
&money.Money{CurrencyCode: "XYZ", Units: 40, Nanos: 0},
&money.Money{CurrencyCode: "XYZ", Units: 0, Nanos: 450000000},
&money.Money{CurrencyCode: "XYZ", Units: 39, Nanos: 550000000},
),
Entry(
"negative unit and positive nanos are normalized",
&money.Money{CurrencyCode: "XYZ", Units: 1, Nanos: 200000000},
&money.Money{CurrencyCode: "XYZ", Units: 2, Nanos: 100000000},
&money.Money{CurrencyCode: "XYZ", Units: 0, Nanos: -900000000},
),
)
})

var _ = Describe("func Sum()", func() {
Expand Down