Browse Source

util.py: fmt_list(): add data conversion support, add test

The MMGen Project 1 year ago
parent
commit
7487256e70
2 changed files with 51 additions and 12 deletions
  1. 13 11
      mmgen/util.py
  2. 38 1
      test/unit_tests_d/ut_util.py

+ 13 - 11
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):
 	"""

+ 38 - 1
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'],