forked from mike-works/elixir-fundamentals
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcurrency.exs
26 lines (23 loc) · 835 Bytes
/
currency.exs
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
formatter = fn(symbol) ->
fn
0 -> "0"
num when num < 0 -> "(#{symbol} #{abs(num)})"
num -> "#{symbol} #{num}"
end
end
##### TESTS #####
test = fn to_run, expected, description ->
require Logger
result = to_run.()
try do
^expected = result
Logger.info "PASSED: #{description}"
rescue
_ in MatchError ->
Logger.error "FAILED: #{description}\n\tFOUND: #{result}\n\tEXPECTED: #{expected}"
end
end
test.(fn -> is_function(formatter.("$")) end, true, "formatter should return a function")
test.(fn -> formatter.("$").(0) end, "0", "0 should be formatted as \"0\"")
test.(fn -> formatter.("€").(125) end, "€ 125", "€ formatter should print 125 formatted as \"€ 125\"")
test.(fn -> formatter.("$").(-1125) end, "($ 1125)", "$ formatter should print -1125 formatted as \"($ 1125)\"")