ut_devtools.py 2.5 KB

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