Browse Source

util.py: new fmt_dict() function, add test

The MMGen Project 1 year ago
parent
commit
e1c9756250
4 changed files with 65 additions and 3 deletions
  1. 1 1
      mmgen/data/release_date
  2. 1 1
      mmgen/data/version
  3. 14 0
      mmgen/util.py
  4. 49 1
      test/unit_tests_d/ut_util.py

+ 1 - 1
mmgen/data/release_date

@@ -1 +1 @@
-July 2023
+September 2023

+ 1 - 1
mmgen/data/version

@@ -1 +1 @@
-14.0.dev1
+14.0.dev2

+ 14 - 0
mmgen/util.py

@@ -190,6 +190,20 @@ def fmt_list(iterable,fmt='dfl',indent='',conv=None):
 	conv = conv or _conv
 	return indent + (sep+indent).join(lq+conv(e)+rq for e in iterable)
 
+def fmt_dict(mapping,fmt='dfl',kconv=None,vconv=None):
+	"pretty-format a dict"
+	kc,vc,sep,fs = {
+		'dfl':           ( str, str,  ", ",  "'{}' ({})" ),
+		'square':        ( str, str,  ", ",  "'{}' [{}]" ),
+		'equal':         ( str, str,  ", ",  "'{}'={}" ),
+		'equal_spaced':  ( str, str,  ", ",  "'{}' = {}" ),
+		'kwargs':        ( str, repr, ", ",  "{}={}" ),
+		'colon':         ( str, repr, ", ",  "{}:{}" ),
+	}[fmt]
+	kconv = kconv or kc
+	vconv = vconv or vc
+	return sep.join(fs.format(kconv(k),vconv(v)) for k,v in mapping.items())
+
 def list_gen(*data):
 	"""
 	Generate a list from an arg tuple of sublists

+ 49 - 1
test/unit_tests_d/ut_util.py

@@ -5,7 +5,8 @@ test.unit_tests_d.ut_misc: utility unit tests for the MMGen suite
 """
 
 from ..include.common import vmsg
-from mmgen.util import fmt_list,list_gen
+from mmgen.util import fmt_list,fmt_dict,list_gen
+from mmgen.color import cyan
 
 class unit_tests:
 
@@ -46,6 +47,53 @@ class unit_tests:
 		vmsg('')
 		return True
 
+	def fmt_dict(self,name,ut):
+		samples = {
+			'url': {
+				'name':'Example',
+				'desc':'Sample URL',
+				'rank': 1,
+				'error': None,
+				'url':'https://example.com/foobar.html',
+			},
+			'choice': {
+				'c': 'curl',
+				'a': 'aiohttp',
+				'r': 'requests',
+			},
+			'cmdline': {
+				'cmd': ["ls","-l"],
+				'text': 'foo bar',
+				'stdin': None,
+				'offset': 123,
+				'env': {},
+			}
+		}
+		chks = {
+		'cmdline': {
+			None:           "'cmd' (['ls', '-l']), 'text' (foo bar), 'stdin' (None), 'offset' (123), 'env' ({})",
+			'dfl':          "'cmd' (['ls', '-l']), 'text' (foo bar), 'stdin' (None), 'offset' (123), 'env' ({})",
+			'square':       "'cmd' [['ls', '-l']], 'text' [foo bar], 'stdin' [None], 'offset' [123], 'env' [{}]",
+			'equal':        "'cmd'=['ls', '-l'], 'text'=foo bar, 'stdin'=None, 'offset'=123, 'env'={}",
+			'equal_spaced': "'cmd' = ['ls', '-l'], 'text' = foo bar, 'stdin' = None, 'offset' = 123, 'env' = {}",
+			'kwargs':       "cmd=['ls', '-l'], text='foo bar', stdin=None, offset=123, env={}",
+			'colon':        "cmd:['ls', '-l'], text:'foo bar', stdin:None, offset:123, env:{}",
+		}
+		}
+
+		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():
+				res = fmt_dict(sample,fmt=fmt) if fmt else fmt_dict(sample)
+				vmsg(f'  {str(fmt)+":":{col1_w}} {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'],