ut_devtools.py 2.6 KB

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