Skip to content

Commit 530e5ac

Browse files
committed
Fix Money#allocate for enumerable of all zeros
1 parent 017a12e commit 530e5ac

File tree

2 files changed

+10
-0
lines changed

2 files changed

+10
-0
lines changed

spec/money/allocate_spec.cr

+6
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@ require "../spec_helper"
22

33
describe Money::Allocate do
44
describe "#allocate" do
5+
context "with all zeros" do
6+
it "allocates evenly" do
7+
Money.us_dollar(100).allocate([0, 0]).map(&.cents).should eq [50, 50]
8+
end
9+
end
10+
511
it "takes no action when one gets all" do
612
Money.us_dollar(5).allocate({1.0}).map(&.cents).should eq [5]
713
end

src/money/money/allocate.cr

+4
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@ struct Money
3838
allocations = allocations_from_splits(splits)
3939
raise ArgumentError.new("Splits add to more then 100%") if allocations > 1.0
4040

41+
if splits.all?(&.zero?)
42+
allocations = splits.size.to_f
43+
end
44+
4145
amounts, left_over = amounts_from_splits(allocations, splits)
4246
delta = left_over > 0 ? 1 : -1
4347
size = amounts.size

0 commit comments

Comments
 (0)