From 1810b50b09623282b2f422f9d135bf20682700e9 Mon Sep 17 00:00:00 2001 From: The MMGen Project Date: Thu, 24 Oct 2019 18:30:23 +0000 Subject: [PATCH] unit_tests.py: new UnitTestHelpers class --- test/unit_tests.py | 21 ++++++++++++++++++++- test/unit_tests_d/ut_baseconv.py | 2 +- test/unit_tests_d/ut_bip39.py | 2 +- test/unit_tests_d/ut_indexed_dict.py | 2 +- test/unit_tests_d/ut_scrypt.py | 2 +- test/unit_tests_d/ut_seedsplit.py | 2 +- test/unit_tests_d/ut_subseed.py | 2 +- test/unit_tests_d/ut_tx_deserialize.py | 2 +- 8 files changed, 27 insertions(+), 8 deletions(-) diff --git a/test/unit_tests.py b/test/unit_tests.py index f3fde726..a437b3e5 100755 --- a/test/unit_tests.py +++ b/test/unit_tests.py @@ -57,6 +57,25 @@ start_time = int(time.time()) if opt.list: Die(0,' '.join(all_tests)) +class UnitTestHelpers(object): + + @classmethod + def process_bad_data(cls,data): + desc_w = max(len(e[0]) for e in data) + exc_w = max(len(e[1]) for e in data) + for (desc,exc_chk,emsg_chk,func) in data: + try: + vmsg_r(' {:{w}}'.format(desc+':',w=desc_w+1)) + func() + except Exception as e: + exc = type(e).__name__ + emsg = e.args[0] + vmsg(' {:{w}} [{}]'.format(exc,emsg,w=exc_w)) + assert exc == exc_chk,'{!r}: incorrect exception type (expected {!r})'.format(exc,exc_chk) + assert emsg_chk in emsg,'{!r}: incorrect error msg (should contain {!r}'.format(emsg,emsg_chk) + else: + rdie(3,"\nillegal action '{}' failed to raise exception {!r}".format(desc,exc_chk)) + try: for test in cmd_args: if test not in all_tests: @@ -67,7 +86,7 @@ try: modname = 'test.unit_tests_d.ut_{}'.format(test) mod = importlib.import_module(modname) gmsg('Running unit test {}'.format(test)) - if not mod.unit_test().run_test(test): + if not mod.unit_test().run_test(test,UnitTestHelpers): rdie(1,'Unit test {!r} failed'.format(test)) del mod diff --git a/test/unit_tests_d/ut_baseconv.py b/test/unit_tests_d/ut_baseconv.py index b657369b..27826947 100755 --- a/test/unit_tests_d/ut_baseconv.py +++ b/test/unit_tests_d/ut_baseconv.py @@ -97,7 +97,7 @@ class unit_test(object): ), } - def run_test(self,name): + def run_test(self,name,ut): msg_r('Testing base conversion routines...') diff --git a/test/unit_tests_d/ut_bip39.py b/test/unit_tests_d/ut_bip39.py index a25bef45..a0092bcb 100755 --- a/test/unit_tests_d/ut_bip39.py +++ b/test/unit_tests_d/ut_bip39.py @@ -83,7 +83,7 @@ class unit_test(object): ) ) - def run_test(self,name): + def run_test(self,name,ut): msg_r('Testing BIP39 conversion routines...') qmsg('') diff --git a/test/unit_tests_d/ut_indexed_dict.py b/test/unit_tests_d/ut_indexed_dict.py index cb6596cd..ccb6c93f 100755 --- a/test/unit_tests_d/ut_indexed_dict.py +++ b/test/unit_tests_d/ut_indexed_dict.py @@ -7,7 +7,7 @@ from mmgen.common import * class unit_test(object): - def run_test(self,name): + def run_test(self,name,ut): bad_msg = ( 'initializing values via constructor', 'reassignment to existing key', 'item deletion', diff --git a/test/unit_tests_d/ut_scrypt.py b/test/unit_tests_d/ut_scrypt.py index 2944c73e..5d463a9a 100755 --- a/test/unit_tests_d/ut_scrypt.py +++ b/test/unit_tests_d/ut_scrypt.py @@ -7,7 +7,7 @@ from test.common import * class unit_test(object): - def run_test(self,name): + def run_test(self,name,ut): import time msg_r('Testing password hashing...') diff --git a/test/unit_tests_d/ut_seedsplit.py b/test/unit_tests_d/ut_seedsplit.py index eda56c17..b4a7f613 100755 --- a/test/unit_tests_d/ut_seedsplit.py +++ b/test/unit_tests_d/ut_seedsplit.py @@ -7,7 +7,7 @@ from mmgen.common import * class unit_test(object): - def run_test(self,name): + def run_test(self,name,ut): from mmgen.seed import Seed,SeedShareList from mmgen.obj import SeedShareIdx diff --git a/test/unit_tests_d/ut_subseed.py b/test/unit_tests_d/ut_subseed.py index a7b2ff0e..da9bb3cf 100755 --- a/test/unit_tests_d/ut_subseed.py +++ b/test/unit_tests_d/ut_subseed.py @@ -7,7 +7,7 @@ from mmgen.common import * class unit_test(object): - def run_test(self,name): + def run_test(self,name,ut): from mmgen.seed import Seed from mmgen.obj import SubSeedIdxRange diff --git a/test/unit_tests_d/ut_tx_deserialize.py b/test/unit_tests_d/ut_tx_deserialize.py index 88a623ed..51426474 100755 --- a/test/unit_tests_d/ut_tx_deserialize.py +++ b/test/unit_tests_d/ut_tx_deserialize.py @@ -13,7 +13,7 @@ class unit_test(object): if not self.core_repo_root: die(1,'The environmental variable CORE_REPO_ROOT must be set before running this test') - def run_test(self,name): + def run_test(self,name,ut): def test_tx(txhex,desc,n):