Skip to content

Commit

Permalink
Add a normalization step to protect code from irregularities
Browse files Browse the repository at this point in the history
  • Loading branch information
marick committed Apr 7, 2021
1 parent aa88376 commit 60726fa
Showing 1 changed file with 46 additions and 0 deletions.
46 changes: 46 additions & 0 deletions test/hillel_budget_test.exs
Expand Up @@ -18,6 +18,46 @@ defmodule HillelBudgetTest do
%{total_limit: total_limit}
end

# After beginning to work on categories, I decided I didn't want to
# have to keep hassling with counts, so add a "normalization" step
# to clean the data.
#
# Besides, I'm guessing this will make things easier later on.

def normalize(bill) when is_map(bill) do
cost = bill.cost
categories = Map.get(bill, :categories, [])
count = Map.get(bill, :count, 1)

Enum.map(1..count, fn _ ->
%{cost: cost, categories: categories}
end)
end

def normalize(bills) when is_list(bills) do
Enum.flat_map(bills, &normalize/1)
end

describe "normalization" do
test "normalizing a bill" do
expect = fn bill, expected ->
assert normalize(bill) == expected
end

%{cost: "c"} |> expect.([%{cost: "c", categories: []}])
%{cost: "c", categories: ["a"]} |> expect.([%{cost: "c", categories: ["a"]}])
%{cost: "c", categories: ["a", "b"], count: 2}
|> expect.([%{cost: "c", categories: ["a", "b"]},
%{cost: "c", categories: ["a", "b"]}])
end

test "normalizing bills" do
actual = normalize([%{cost: 20, count: 2}])
assert actual == [%{cost: 20, categories: []},
%{cost: 20, categories: []}]
end
end

describe "total budget" do
test "total budget boundaries" do
bill = [%{cost: 50}]
Expand All @@ -32,4 +72,10 @@ defmodule HillelBudgetTest do
refute can_afford?(budget(59), bill)
end
end

describe "interaction of limits" do
@tag :skip
test "category with total"
end

end

0 comments on commit 60726fa

Please sign in to comment.