Browse Source

unit_tests.py: new UnitTestHelpers class

The MMGen Project 5 years ago
parent
commit
1810b50b09

+ 20 - 1
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
 

+ 1 - 1
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...')
 

+ 1 - 1
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('')

+ 1 - 1
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',

+ 1 - 1
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...')

+ 1 - 1
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
 

+ 1 - 1
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
 

+ 1 - 1
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):