Skip to content

Commit

Permalink
Fix Money#allocate for enumerable of all zeros
Browse files Browse the repository at this point in the history
  • Loading branch information
Sija committed Aug 11, 2021
1 parent 017a12e commit 530e5ac
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 0 deletions.
6 changes: 6 additions & 0 deletions spec/money/allocate_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@ require "../spec_helper"

describe Money::Allocate do
describe "#allocate" do
context "with all zeros" do
it "allocates evenly" do
Money.us_dollar(100).allocate([0, 0]).map(&.cents).should eq [50, 50]
end
end

it "takes no action when one gets all" do
Money.us_dollar(5).allocate({1.0}).map(&.cents).should eq [5]
end
Expand Down
4 changes: 4 additions & 0 deletions src/money/money/allocate.cr
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ struct Money
allocations = allocations_from_splits(splits)
raise ArgumentError.new("Splits add to more then 100%") if allocations > 1.0

if splits.all?(&.zero?)
allocations = splits.size.to_f
end

amounts, left_over = amounts_from_splits(allocations, splits)
delta = left_over > 0 ? 1 : -1
size = amounts.size
Expand Down

0 comments on commit 530e5ac

Please sign in to comment.