ut_devtools.py 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. #!/usr/bin/env python3
  2. """
  3. test.unit_tests_d.ut_devtools: devtools unit tests for the MMGen suite
  4. """
  5. import os,json
  6. from mmgen.util import msg
  7. from mmgen.devtools import print_diff,get_ndiff,print_stack_trace,pmsg_r,pmsg,Pmsg
  8. from . import unit_tests_base
  9. textA = """
  10. def main():
  11. a = 1
  12. b = 2
  13. c = 3
  14. """.lstrip()
  15. textB = """
  16. def main():
  17. a = 1
  18. b = 0
  19. c = 3
  20. """.lstrip()
  21. jsonA = open('test/ref/ethereum/tracking-wallet-v1.json').read()
  22. dataB = json.loads(jsonA)
  23. dataB['coin'] = 'ETC'
  24. jsonB = json.dumps(dataB)
  25. text_data = (
  26. (textA, textB, 'a/main.py', 'b/main.py', False, 'text: one line difference'),
  27. ('', textB, 'a/main.py', 'b/main.py', False, 'text: first file empty'),
  28. (textA, textA, 'a/main.py', 'b/main.py', False, 'text: identical files'),
  29. ('', '', 'a/empty.txt', 'b/empty.txt', False, 'text: empty files'),
  30. )
  31. json_data = (
  32. (jsonA, jsonB, 'a/data.json', 'b/data.json', True, 'json: one difference'),
  33. ('{}', jsonB, 'a/data.json', 'b/data.json', True, 'json: first file empty'),
  34. (jsonA, jsonA, 'a/data.json', 'b/data.json', True, 'json: identical files'),
  35. ('{}', '{}', 'a/data.json', 'b/data.json', True, 'json: empty files'),
  36. )
  37. def print_hdr(hdr):
  38. print('{a} {b} {c}'.format(
  39. a = '-' * ((78 - len(hdr))//2),
  40. b = hdr,
  41. c = '-' * ((78 - len(hdr))//2 + (len(hdr) % 2)) ))
  42. # TODO: add data checks
  43. class unit_tests(unit_tests_base):
  44. silence_output = True
  45. def _post_subtest(self,name,subname,ut):
  46. print('-' * 80 + '\n')
  47. def diff(self,name,ut):
  48. for data in text_data + json_data:
  49. print_hdr(data[-1])
  50. print_diff(*data[:-1])
  51. return True
  52. def ndiff(self,name,ut):
  53. for data in text_data:
  54. print_hdr(data[-1])
  55. print('\n'.join(get_ndiff(*data[:2])))
  56. return True
  57. def stack_trace(self,name,ut):
  58. print_hdr('stack trace')
  59. print_stack_trace('Test',fh_list=[open(os.devnull,'w')],trim=0)
  60. return True
  61. def obj_pmsg(self,name,ut):
  62. from mmgen.protocol import init_proto
  63. from mmgen.seed import Seed
  64. from mmgen.addrlist import AddrList
  65. from ..include.common import cfg
  66. print_hdr('MMGenObject.pmsg()')
  67. AddrList(
  68. cfg = cfg,
  69. proto = init_proto( cfg, 'btc' ),
  70. seed = Seed(cfg,seed_bin=bytes.fromhex('bead'*16)),
  71. addr_idxs = '1',
  72. mmtype = 'B',
  73. skip_chksum = True ).pmsg(color='green')
  74. return True
  75. def pmsg(self,name,ut):
  76. colors = (None,'red','green','yellow','blue','purple')
  77. msg('\npmsg_r():')
  78. for color in colors:
  79. pmsg_r({'color':color},color=color)
  80. msg('\n\npmsg():')
  81. for color in colors:
  82. pmsg({'color':color},color=color)
  83. msg('\nPmsg():')
  84. for color in colors:
  85. Pmsg({'color':color},color=color)
  86. return True