reports: Balance.format() accepts None as a format.
This commit is contained in:
parent
2c44cc8f50
commit
d8df34ebaf
2 changed files with 12 additions and 1 deletions
|
@ -113,7 +113,7 @@ class Balance(Mapping[str, data.Amount]):
|
|||
return self._all_amounts(operator.le, 0)
|
||||
|
||||
def format(self,
|
||||
fmt: str='#,#00.00 ¤¤',
|
||||
fmt: Optional[str]='#,#00.00 ¤¤',
|
||||
sep: str=', ',
|
||||
empty: str="Zero balance",
|
||||
) -> str:
|
||||
|
@ -122,6 +122,9 @@ class Balance(Mapping[str, data.Amount]):
|
|||
If the balance is zero, returns ``empty``. Otherwise, returns a string
|
||||
with each amount in the balance formatted as ``fmt``, separated by
|
||||
``sep``.
|
||||
|
||||
If you set ``fmt`` to None, amounts will be formatted according to the
|
||||
user's locale. The default format is Beancount's input format.
|
||||
"""
|
||||
amounts = [amount for amount in self.values() if amount.number]
|
||||
if not amounts:
|
||||
|
|
|
@ -22,6 +22,8 @@ import pytest
|
|||
|
||||
from . import testutil
|
||||
|
||||
import babel.numbers
|
||||
|
||||
from conservancy_beancount.reports import core
|
||||
|
||||
DEFAULT_STRINGS = [
|
||||
|
@ -191,6 +193,12 @@ def test_format_sep(sep):
|
|||
balance = core.Balance(amounts)
|
||||
assert balance.format(sep=sep) == expected
|
||||
|
||||
def test_format_none():
|
||||
amounts = testutil.balance_map(BRL=65000)
|
||||
balance = core.Balance(amounts)
|
||||
expected = babel.numbers.format_currency(65000, 'BRL')
|
||||
assert balance.format(None) == expected
|
||||
|
||||
@pytest.mark.parametrize('empty', [
|
||||
"N/A",
|
||||
"Zero",
|
||||
|
|
Loading…
Reference in a new issue