Skip to content

Commit

Permalink
Make sure that Money.to_string! actually raises
Browse files Browse the repository at this point in the history
  • Loading branch information
kipcole9 committed Jun 2, 2019
1 parent 2b9b6a7 commit 7e627fe
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 3 deletions.
12 changes: 12 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
# Changelog for Cldr v3.4.3

This is the changelog for Cldr v3.4.3 released on June 2nd, 2019. For older changelogs please consult the release tag on [GitHub](https://github.com/kipcole9/cldr/tags)

### Bug Fixes

* Ensure `Money.to_string!/2` properly raises

* Add specs for `Money.to_string/2` and `Money.to_string!/2`

Thanks to @rodrigues for the report and PR.

# Changelog for Cldr v3.4.2

This is the changelog for Cldr v3.4.2 released on April 16th, 2019. For older changelogs please consult the release tag on [GitHub](https://github.com/kipcole9/cldr/tags)
Expand Down
9 changes: 7 additions & 2 deletions lib/money.ex
Original file line number Diff line number Diff line change
Expand Up @@ -610,6 +610,8 @@ defmodule Money do
{:ok, "1,234 US dollars"}
"""
@spec to_string(Money.t(), Keyword.t()) :: {:ok, String.t()} | {:error, {atom, String.t()}}

def to_string(%Money{} = money, options \\ []) do
default_options = [backend: Money.default_backend(), currency: money.currency]
options = Keyword.merge(default_options, options)
Expand Down Expand Up @@ -655,9 +657,12 @@ defmodule Money do
"1,234 US dollars"
"""
@spec to_string(Money.t(), Keyword.t()) :: String.t() | no_return()

def to_string!(%Money{} = money, options \\ []) do
with {:ok, string} <- Money.to_string(money, options) do
string
case to_string(money, options) do
{:ok, string} -> string
{:error, {exception, reason}} -> raise exception, reason
end
end

Expand Down
2 changes: 1 addition & 1 deletion mix.exs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
defmodule Money.Mixfile do
use Mix.Project

@version "3.4.2"
@version "3.4.3"

def project do
[
Expand Down
13 changes: 13 additions & 0 deletions test/money_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,19 @@ defmodule MoneyTest do
money = Money.new(1234, :USD)
assert Money.to_string(money) == {:ok, "$1,234.00"}
end

test "to_string works via protocol" do
money = Money.new(1234, :USD)
assert to_string(money) == "$1,234.00"
end

test "to_string! raises if there is an error" do
money = Money.new(1234, :USD)

assert_raise Cldr.FormatCompileError, fn ->
Money.to_string!(money, format: "0#")
end
end

test "adding two money structs with same currency" do
assert Money.add!(Money.new(:USD, 100), Money.new(:USD, 100)) == Money.new(:USD, 200)
Expand Down

0 comments on commit 7e627fe

Please sign in to comment.