From 7487256e70e735df85b0a986368883d9f1c91504 Mon Sep 17 00:00:00 2001 From: The MMGen Project Date: Mon, 25 Sep 2023 13:25:02 +0000 Subject: [PATCH] util.py: fmt_list(): add data conversion support, add test --- mmgen/util.py | 24 ++++++++++++---------- test/unit_tests_d/ut_util.py | 39 +++++++++++++++++++++++++++++++++++- 2 files changed, 51 insertions(+), 12 deletions(-) diff --git a/mmgen/util.py b/mmgen/util.py index 9cfe49ba..ef49595b 100755 --- a/mmgen/util.py +++ b/mmgen/util.py @@ -174,19 +174,21 @@ def fmt(s,indent='',strip_char=None,append='\n'): "de-indent multiple lines of text, or indent with specified string" return indent + ('\n'+indent).join([l.strip(strip_char) for l in s.strip().splitlines()]) + append -def fmt_list(iterable,fmt='dfl',indent=''): +def fmt_list(iterable,fmt='dfl',indent='',conv=None): "pretty-format a list" - sep,lq,rq = { - 'utf8': ("“, ”", "“", "”"), - 'dfl': ("', '", "'", "'"), - 'bare': (' ', '', '' ), - 'no_quotes': (', ', '', '' ), - 'no_spc': ("','", "'", "'"), - 'min': (",", "'", "'"), - 'col': ('\n'+indent, indent, '' ), - 'list': ('\n- '+indent, '- '+indent, '' ), + _conv,sep,lq,rq = { + 'dfl': ( str, ", ", "'", "'"), + 'utf8': ( str, ", ", "“", "”"), + 'bare': ( repr, " ", "", ""), + 'no_quotes': ( str, ", ", "", ""), + 'no_spc': ( str, ",", "'", "'"), + 'min': ( str, ",", "", ""), + 'repr': ( repr, ", ", "", ""), + 'csv': ( repr, ",", "", ""), + 'col': ( str, "\n", "", ""), }[fmt] - return lq + sep.join(str(i) for i in iterable) + rq + conv = conv or _conv + return indent + (sep+indent).join(lq+conv(e)+rq for e in iterable) def list_gen(*data): """ diff --git a/test/unit_tests_d/ut_util.py b/test/unit_tests_d/ut_util.py index 057de93e..ff8caab3 100755 --- a/test/unit_tests_d/ut_util.py +++ b/test/unit_tests_d/ut_util.py @@ -5,10 +5,47 @@ test.unit_tests_d.ut_misc: utility unit tests for the MMGen suite """ from ..include.common import vmsg -from mmgen.util import list_gen +from mmgen.util import fmt_list,list_gen class unit_tests: + def fmt_list(self,name,ut): + + samples = { + 'pids': [18234,18444,19324], + 'vardata': [None,True,1234,'sample string'], + } + chks = { + 'vardata': { + None: "'None', 'True', '1234', 'sample string'", + 'dfl': "'None', 'True', '1234', 'sample string'", + 'utf8': "“None”, “True”, “1234”, “sample string”", + 'bare': "None True 1234 'sample string'", + 'no_quotes': "None, True, 1234, sample string", + 'no_spc': "'None','True','1234','sample string'", + 'min': "None,True,1234,sample string", + 'repr': "None, True, 1234, 'sample string'", + 'csv': "None,True,1234,'sample string'", + 'col': " + None\n + True\n + 1234\n + sample string", + } + + } + + col1_w = max(len(str(e)) for e in list(chks.values())[0]) + 1 + + for name,sample in samples.items(): + vmsg(cyan(f'Input: {sample}')) + for fmt,chk in list(chks.values())[0].items(): + spc = '\n' if fmt in ('col','list') else ' ' + indent = ' + ' if fmt == 'col' else '' + res = fmt_list(sample,fmt=fmt,indent=indent) if fmt else fmt_list(sample,indent=indent) + vmsg(f' {str(fmt)+":":{col1_w}}{spc}{res}') + if name in chks: + assert res == chks[name][fmt], f'{res} != {chks[name][fmt]}' + + vmsg('') + return True + def list_gen(self,name,ut): res = list_gen( ['a'],