ut_util.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. #!/usr/bin/env python3
  2. """
  3. test.modtest_d.ut_misc: utility unit tests for the MMGen suite
  4. """
  5. from mmgen.color import cyan
  6. from mmgen.util import fmt_list, fmt_dict, list_gen
  7. from ..include.common import vmsg
  8. class unit_tests:
  9. def fmt_list(self, name, ut):
  10. samples = {
  11. 'pids': [18234, 18444, 19324],
  12. 'vardata': [None, True, 1234, 'sample string'],
  13. }
  14. chks = {
  15. 'vardata': {
  16. None: "'None', 'True', '1234', 'sample string'",
  17. 'dfl': "'None', 'True', '1234', 'sample string'",
  18. 'utf8': "“None”, “True”, “1234”, “sample string”",
  19. 'bare': "None True 1234 'sample string'",
  20. 'no_quotes': "None, True, 1234, sample string",
  21. 'no_spc': "'None','True','1234','sample string'",
  22. 'min': "None,True,1234,sample string",
  23. 'repr': "None, True, 1234, 'sample string'",
  24. 'csv': "None,True,1234,'sample string'",
  25. 'col': " + None\n + True\n + 1234\n + sample string",
  26. }
  27. }
  28. col1_w = max(len(str(e)) for e in list(chks.values())[0]) + 1
  29. for _name, sample in samples.items():
  30. vmsg(cyan(f'Input: {sample}'))
  31. for fmt in list(chks.values())[0]:
  32. spc = '\n' if fmt in ('col', 'list') else ' '
  33. indent = ' + ' if fmt == 'col' else ''
  34. res = fmt_list(sample, fmt=fmt, indent=indent) if fmt else fmt_list(sample, indent=indent)
  35. vmsg(f' {str(fmt)+":":{col1_w}}{spc}{res}')
  36. if _name in chks:
  37. assert res == chks[_name][fmt], f'{res} != {chks[_name][fmt]}'
  38. vmsg('')
  39. return True
  40. def fmt_dict(self, name, ut):
  41. samples = {
  42. 'url': {
  43. 'name': 'Example',
  44. 'desc': 'Sample URL',
  45. 'rank': 1,
  46. 'error': None,
  47. 'url': 'https://example.com/foobar.html',
  48. },
  49. 'choice': {
  50. 'c': 'curl',
  51. 'a': 'aiohttp',
  52. 'r': 'requests',
  53. },
  54. 'cmdline': {
  55. 'cmd': ['ls', '-l'],
  56. 'text': 'foo bar',
  57. 'stdin': None,
  58. 'offset': 123,
  59. 'env': {},
  60. }
  61. }
  62. chks = {
  63. 'cmdline': {
  64. None: "'cmd' (['ls', '-l']), 'text' (foo bar), 'stdin' (None), 'offset' (123), 'env' ({})",
  65. 'dfl': "'cmd' (['ls', '-l']), 'text' (foo bar), 'stdin' (None), 'offset' (123), 'env' ({})",
  66. 'square': "'cmd' [['ls', '-l']], 'text' [foo bar], 'stdin' [None], 'offset' [123], 'env' [{}]",
  67. 'equal': "'cmd'=['ls', '-l'], 'text'=foo bar, 'stdin'=None, 'offset'=123, 'env'={}",
  68. 'equal_spaced': "'cmd' = ['ls', '-l'], 'text' = foo bar, 'stdin' = None, 'offset' = 123, 'env' = {}",
  69. 'kwargs': "cmd=['ls', '-l'], text='foo bar', stdin=None, offset=123, env={}",
  70. 'colon': "cmd:['ls', '-l'], text:'foo bar', stdin:None, offset:123, env:{}",
  71. }
  72. }
  73. col1_w = max(len(str(e)) for e in list(chks.values())[0]) + 1
  74. for _name, sample in samples.items():
  75. vmsg(cyan(f'Input: {sample}'))
  76. for fmt in list(chks.values())[0]:
  77. res = fmt_dict(sample, fmt=fmt) if fmt else fmt_dict(sample)
  78. vmsg(f' {str(fmt)+":":{col1_w}} {res}')
  79. if _name in chks:
  80. assert res == chks[_name][fmt], f'{res} != {chks[_name][fmt]}'
  81. vmsg('')
  82. return True
  83. def list_gen(self, name, ut):
  84. res = list_gen(
  85. ['a'],
  86. ['b', False],
  87. ['c', 'x'],
  88. ['d', int],
  89. ['e', None, 1, 'f', isinstance(7, int)],
  90. ['g', 'h', 0],
  91. [None],
  92. [0],
  93. [False],
  94. )
  95. chk = ['a', 'c', 'd', 'e', None, 1, 'f', None, 0, False]
  96. vmsg('=> ' + str(res))
  97. assert res == chk, f'{res} != {chk}'
  98. vmsg('')
  99. return True