From 8de9022c63b62d4562bc1c7774e4b67ee64d3837 Mon Sep 17 00:00:00 2001 From: The MMGen Project Date: Fri, 26 Sep 2025 10:40:23 +0000 Subject: [PATCH] CoinAmt.fmt(): reimplement using match statement, add test --- mmgen/amt.py | 20 +++++++++--------- test/modtest_d/amt.py | 47 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 57 insertions(+), 10 deletions(-) diff --git a/mmgen/amt.py b/mmgen/amt.py index f888d6bf..c5c1b03a 100755 --- a/mmgen/amt.py +++ b/mmgen/amt.py @@ -77,16 +77,16 @@ class CoinAmt(Decimal, Hilite, InitErrors): # abstract class cls.method_not_implemented() def fmt(self, iwidth=1, /, *, color=False, prec=None): # iwidth: width of the integer part - prec = prec or self.max_prec - if '.' in (s := str(self)): - a, b = s.split('.', 1) - return self.colorize( - a.rjust(iwidth) + '.' + b.ljust(prec)[:prec], # truncation, not rounding! - color = color) - else: - return self.colorize( - s.rjust(iwidth).ljust(iwidth+prec+1), - color = color) + match str(self).split('.', 1): + case [a, b]: + return self.colorize( + # we truncate instead of rounding: + a.rjust(iwidth) + '.' + b.ljust(prec or self.max_prec)[:prec or self.max_prec], + color = color) + case [a]: + return self.colorize( + a.rjust(iwidth) + ' ' + ''.ljust(prec or self.max_prec), + color = color) def hl(self, *, color=True): return self.colorize(str(self), color=color) diff --git a/test/modtest_d/amt.py b/test/modtest_d/amt.py index 064f2ba6..5dc5a821 100755 --- a/test/modtest_d/amt.py +++ b/test/modtest_d/amt.py @@ -37,6 +37,24 @@ def test_fee_spec(data): assert res.unit == unit, f' {res.unit} != {unit}' return True +def test_fmt(data): + protos = get_protos(data) + fs = ' {:5} {:18} {:<6} {:<5} {}' + vmsg(fs.format('PROTO', 'INPUT', 'IWIDTH', 'PREC', 'FORMATTED')) + for proto, amt, iwidth, prec, chk in data: + amt = protos[proto].coin_amt(amt) + args = (iwidth,) if iwidth else () + kwargs = {'prec': prec} if prec else {} + res = amt.fmt(*args, **kwargs) + vmsg(fs.format( + proto.upper(), + f'[{str(amt)}]', + 'None' if iwidth is None else iwidth, + 'None' if prec is None else prec, + f'[{res}]')) + assert res == chk, f'[{res}] != [{chk}]' + return True + class unit_tests: altcoin_deps = ('fee_spec_alt', 'to_unit_alt', 'token_amt') @@ -81,6 +99,35 @@ class unit_tests: ('eth', '3.07M', '3.07', 'Mwei'), ('xmr', '3.07a', '3.07', 'atomic'))) + def fmt(self, name, ut, desc='column formatting (LTC, BCH, ETH, XMR)'): + return test_fmt(( + ('btc', '1', None, None, '1 '), + ('btc', '1.2', None, None, '1.2 '), + ('btc', '1', 1, None, '1 '), + ('btc', '1.2', 1, None, '1.2 '), + ('btc', '12', None, None, '12 '), + ('btc', '12.3', None, None, '12.3 '), + ('btc', '12', 1, None, '12 '), + ('btc', '12.3', 1, None, '12.3 '), + ('btc', '12', 2, None, '12 '), + ('btc', '12.3', 2, None, '12.3 '), + ('btc', '12', 3, None, ' 12 '), + ('btc', '12.3', 3, None, ' 12.3 '), + ('ltc', '0', None, None, '0 '), + ('ltc', '0.00000001', None, None, '0.00000001'), + ('ltc', '0.00000001', 8, None, ' 0.00000001'), + ('xmr', '1.234567890123', None, None, '1.234567890123'), + ('xmr', '1', None, None, '1 '), + ('xmr', '1', None, 4, '1 '), + ('xmr', '123.456', None, None, '123.456 '), + ('xmr', '123.456', 2, None, '123.456 '), + ('xmr', '123.456', None, 4, '123.456 '), + ('xmr', '123.456', 2, 4, '123.456 '), + ('xmr', '1.234567890123', None, 4, '1.2345'), + ('xmr', '1.234567890123', 8, 4, ' 1.2345'), + ('xmr', '1', 8, 4, ' 1 '), + ('xmr', '111', 8, 4, ' 111 '))) + def token_amt(self, name, ut, desc='TokenAmt (ETH)'): for n, dec, unit, chk in ( (1234567, 6, 'atomic', '1.234567'),