ut_util.py 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. #!/usr/bin/env python3
  2. """
  3. test.unit_tests_d.ut_misc: utility unit tests for the MMGen suite
  4. """
  5. from ..include.common import vmsg
  6. from mmgen.util import fmt_list,list_gen
  7. class unit_tests:
  8. def fmt_list(self,name,ut):
  9. samples = {
  10. 'pids': [18234,18444,19324],
  11. 'vardata': [None,True,1234,'sample string'],
  12. }
  13. chks = {
  14. 'vardata': {
  15. None: "'None', 'True', '1234', 'sample string'",
  16. 'dfl': "'None', 'True', '1234', 'sample string'",
  17. 'utf8': "“None”, “True”, “1234”, “sample string”",
  18. 'bare': "None True 1234 'sample string'",
  19. 'no_quotes': "None, True, 1234, sample string",
  20. 'no_spc': "'None','True','1234','sample string'",
  21. 'min': "None,True,1234,sample string",
  22. 'repr': "None, True, 1234, 'sample string'",
  23. 'csv': "None,True,1234,'sample string'",
  24. 'col': " + None\n + True\n + 1234\n + sample string",
  25. }
  26. }
  27. col1_w = max(len(str(e)) for e in list(chks.values())[0]) + 1
  28. for name,sample in samples.items():
  29. vmsg(cyan(f'Input: {sample}'))
  30. for fmt,chk in list(chks.values())[0].items():
  31. spc = '\n' if fmt in ('col','list') else ' '
  32. indent = ' + ' if fmt == 'col' else ''
  33. res = fmt_list(sample,fmt=fmt,indent=indent) if fmt else fmt_list(sample,indent=indent)
  34. vmsg(f' {str(fmt)+":":{col1_w}}{spc}{res}')
  35. if name in chks:
  36. assert res == chks[name][fmt], f'{res} != {chks[name][fmt]}'
  37. vmsg('')
  38. return True
  39. def list_gen(self,name,ut):
  40. res = list_gen(
  41. ['a'],
  42. ['b', 1==2],
  43. ['c', 'x'],
  44. ['d', int],
  45. ['e', None, 1, 'f', isinstance(7,int)],
  46. ['g', 'h', 0],
  47. [None],
  48. [0],
  49. [False],
  50. )
  51. chk = ['a', 'c', 'd', 'e', None, 1, 'f', None, 0, False]
  52. vmsg('=> ' + str(res))
  53. assert res == chk, f'{res} != {chk}'
  54. vmsg('')
  55. return True