Skip to content

Commit

Permalink
Make show and print identical; add writemime text/plain support.
Browse files Browse the repository at this point in the history
  • Loading branch information
TotalVerb committed Dec 6, 2015
1 parent d204efd commit a234b83
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 25 deletions.
11 changes: 6 additions & 5 deletions doc/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,13 @@ The usual arithmetic operators are available.
Displaying Currencies
---------------------

There are two ways to display a :class:`Monetary` value, :func:`show` and
:func:`print`. The representation used by :func:`print` is more compact, but the
representation used by :func:`show` is more user-friendly. To get the
:func:`show` representation into a string, use the :func:`sprint` function::
There are two ways to display a :class:`Monetary` value. The representation used
by :func:`show` and :func:`print` is the same, and is fairly compact. For a
richer display, use :func:`writemime`, which has a more user-friendly
representation. To get the :func:`writemime` representation into a string, use
the :func:`sprint` function::

sprint(show, 100USD) # "100.00 USD"
sprint(writemime, "text/plain", 100USD) # "100.00 USD"

Baskets
-------
Expand Down
10 changes: 5 additions & 5 deletions src/basket.jl
Original file line number Diff line number Diff line change
Expand Up @@ -109,17 +109,17 @@ function Base.done(b::Basket, s)
iszero(v) && done(b, s)
end
end
function Base.print(io::IO, b::Basket)
write(io, "$(typeof(b).name)([")
function Base.show(io::IO, b::Basket)
write(io, "$(typeof(b))([")
write(io, join(b, ","))
print(io, "])")
end
function Base.show(io::IO, b::Basket)
function Base.writemime(io::IO, ::MIME"text/plain", b::Basket)
len = length(b)
write(io, "$len-currency $(typeof(b).name):")
write(io, "$len-currency $(typeof(b)):")
for val in b
write(io, "\n ")
show(io, val)
writemime(io, "text/plain", val)
end
end

Expand Down
8 changes: 4 additions & 4 deletions src/monetary.jl
Original file line number Diff line number Diff line change
Expand Up @@ -93,11 +93,11 @@ end

function Base.show(io::IO, m::Monetary)
cur = currency(m)
print(io, "$(curdisplay(m.amt, decimals(cur))) $cur")
print(io, int(m) / 10.0^decimals(cur))
print(io, cur)
end

function Base.print(io::IO, m::Monetary)
function Base.writemime(io::IO, ::MIME"text/plain", m::Monetary)
cur = currency(m)
print(io, int(m) / 10.0^decimals(cur))
print(io, cur)
print(io, "$(curdisplay(m.amt, decimals(cur))) $cur")
end
6 changes: 3 additions & 3 deletions test/currencies.jl
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
# Custom currencies
@usingcustomcurrency xbt "Bitcoin (100 satoshi unit)" 2

@test contains(sprint(show, 10xbt), "xbt")
@test contains(sprint(show, 10xbt), "10.00")
@test contains(sprint(writemime, "text/plain", 10xbt), "xbt")
@test contains(sprint(writemime, "text/plain", 10xbt), "10.00")
@test 10xbt - 5xbt == 5xbt
@test StaticBasket([10xbt, 10USD]) - 10USD == 10xbt

custom = newcurrency!(:custom, "Custom Currency", 6)

@test sprint(show, 20custom) == "20.000000 custom"
@test sprint(writemime, "text/plain", 20custom) == "20.000000 custom"
@test string(20custom) == "20.0custom"
@test 10custom / 10000000 == 0.000001custom

Expand Down
21 changes: 13 additions & 8 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -76,14 +76,19 @@ rates_d = ExchangeRateTable(
@test_throws KeyError valuate(rates_a, :JPY, 100USD)

# Display
@test contains(sprint(show, 1USD), "1.00")
@test contains(sprint(show, 1JPY), "1")
@test contains(sprint(show, StaticBasket([1USD, 1CAD])), "CAD")
@test contains(sprint(show, DynamicBasket([1USD, 1CAD])), "USD")
@test contains(sprint(show, StaticBasket([100USD, 200EUR])), "200.00 EUR")
@test !contains(sprint(show, StaticBasket([1USD, 1CAD, -1CAD])), "CAD")
@test sprint(show, 1USD) == "1.00 USD"
@test sprint(show, -1USD) == "−1.00 USD"
@test contains(sprint(writemime, "text/plain", 1USD), "1.00")
@test contains(sprint(writemime, "text/plain", 1JPY), "1")
@test contains(
sprint(writemime, "text/plain", StaticBasket([1USD, 1CAD])), "CAD")
@test contains(
sprint(writemime, "text/plain", DynamicBasket([1USD, 1CAD])), "USD")
@test contains(
sprint(writemime, "text/plain", StaticBasket([100USD, 200EUR])),
"200.00 EUR")
@test !contains(
sprint(writemime, "text/plain", StaticBasket([1USD, 1CAD, -1CAD])), "CAD")
@test sprint(writemime, "text/plain", 1USD) == "1.00 USD"
@test sprint(writemime, "text/plain", -1USD) == "−1.00 USD"

@test string(1USD) == "1.0USD"
@test string(0.01USD) == "0.01USD"
Expand Down

0 comments on commit a234b83

Please sign in to comment.