You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
225 lines
5.7 KiB
225 lines
5.7 KiB
![]()
7 years ago
|
from trezor.utils import ensure
|
||
![]()
6 years ago
|
from utest import assert_async
|
||
![]()
7 years ago
|
|
||
![]()
7 years ago
|
|
||
![]()
8 years ago
|
class SkipTest(Exception):
|
||
|
pass
|
||
|
|
||
|
|
||
|
class AssertRaisesContext:
|
||
|
|
||
|
def __init__(self, exc):
|
||
|
self.expected = exc
|
||
|
|
||
|
def __enter__(self):
|
||
|
return self
|
||
|
|
||
|
def __exit__(self, exc_type, exc_value, tb):
|
||
|
if exc_type is None:
|
||
![]()
7 years ago
|
ensure(False, "%r not raised" % self.expected)
|
||
![]()
8 years ago
|
if issubclass(exc_type, self.expected):
|
||
|
return True
|
||
|
return False
|
||
|
|
||
|
|
||
|
class TestCase:
|
||
|
|
||
|
def fail(self, msg=''):
|
||
![]()
7 years ago
|
ensure(False, msg)
|
||
![]()
8 years ago
|
|
||
|
def assertEqual(self, x, y, msg=''):
|
||
|
if not msg:
|
||
|
msg = "%r vs (expected) %r" % (x, y)
|
||
![]()
7 years ago
|
ensure(x == y, msg)
|
||
![]()
8 years ago
|
|
||
|
def assertNotEqual(self, x, y, msg=''):
|
||
|
if not msg:
|
||
|
msg = "%r not expected to be equal %r" % (x, y)
|
||
![]()
7 years ago
|
ensure(x != y, msg)
|
||
![]()
8 years ago
|
|
||
|
def assertAlmostEqual(self, x, y, places=None, msg='', delta=None):
|
||
|
if x == y:
|
||
|
return
|
||
|
if delta is not None and places is not None:
|
||
|
raise TypeError("specify delta or places not both")
|
||
|
|
||
|
if delta is not None:
|
||
|
if abs(x - y) <= delta:
|
||
|
return
|
||
|
if not msg:
|
||
|
msg = '%r != %r within %r delta' % (x, y, delta)
|
||
|
else:
|
||
|
if places is None:
|
||
|
places = 7
|
||
![]()
7 years ago
|
if round(abs(y - x), places) == 0:
|
||
![]()
8 years ago
|
return
|
||
|
if not msg:
|
||
|
msg = '%r != %r within %r places' % (x, y, places)
|
||
|
|
||
![]()
7 years ago
|
ensure(False, msg)
|
||
![]()
8 years ago
|
|
||
|
def assertNotAlmostEqual(self, x, y, places=None, msg='', delta=None):
|
||
|
if delta is not None and places is not None:
|
||
|
raise TypeError("specify delta or places not both")
|
||
|
|
||
|
if delta is not None:
|
||
|
if not (x == y) and abs(x - y) > delta:
|
||
|
return
|
||
|
if not msg:
|
||
|
msg = '%r == %r within %r delta' % (x, y, delta)
|
||
|
else:
|
||
|
if places is None:
|
||
|
places = 7
|
||
![]()
7 years ago
|
if not (x == y) and round(abs(y - x), places) != 0:
|
||
![]()
8 years ago
|
return
|
||
|
if not msg:
|
||
|
msg = '%r == %r within %r places' % (x, y, places)
|
||
|
|
||
![]()
7 years ago
|
ensure(False, msg)
|
||
![]()
8 years ago
|
|
||
|
def assertIs(self, x, y, msg=''):
|
||
|
if not msg:
|
||
|
msg = "%r is not %r" % (x, y)
|
||
![]()
7 years ago
|
ensure(x is y, msg)
|
||
![]()
8 years ago
|
|
||
|
def assertIsNot(self, x, y, msg=''):
|
||
|
if not msg:
|
||
|
msg = "%r is %r" % (x, y)
|
||
![]()
7 years ago
|
ensure(x is not y, msg)
|
||
![]()
8 years ago
|
|
||
|
def assertIsNone(self, x, msg=''):
|
||
|
if not msg:
|
||
|
msg = "%r is not None" % x
|
||
![]()
7 years ago
|
ensure(x is None, msg)
|
||
![]()
8 years ago
|
|
||
|
def assertIsNotNone(self, x, msg=''):
|
||
|
if not msg:
|
||
|
msg = "%r is None" % x
|
||
![]()
7 years ago
|
ensure(x is not None, msg)
|
||
![]()
8 years ago
|
|
||
|
def assertTrue(self, x, msg=''):
|
||
|
if not msg:
|
||
|
msg = "Expected %r to be True" % x
|
||
![]()
7 years ago
|
ensure(x, msg)
|
||
![]()
8 years ago
|
|
||
|
def assertFalse(self, x, msg=''):
|
||
|
if not msg:
|
||
|
msg = "Expected %r to be False" % x
|
||
![]()
7 years ago
|
ensure(not x, msg)
|
||
![]()
8 years ago
|
|
||
|
def assertIn(self, x, y, msg=''):
|
||
|
if not msg:
|
||
|
msg = "Expected %r to be in %r" % (x, y)
|
||
![]()
7 years ago
|
ensure(x in y, msg)
|
||
![]()
8 years ago
|
|
||
|
def assertIsInstance(self, x, y, msg=''):
|
||
![]()
7 years ago
|
ensure(isinstance(x, y), msg)
|
||
![]()
8 years ago
|
|
||
|
def assertRaises(self, exc, func=None, *args, **kwargs):
|
||
|
if func is None:
|
||
|
return AssertRaisesContext(exc)
|
||
|
try:
|
||
|
func(*args, **kwargs)
|
||
![]()
7 years ago
|
ensure(False, "%r not raised" % exc)
|
||
![]()
8 years ago
|
except Exception as e:
|
||
|
if isinstance(e, exc):
|
||
|
return
|
||
|
raise
|
||
|
|
||
![]()
6 years ago
|
def assertListEqual(self, x, y, msg=''):
|
||
|
if len(x) != len(y):
|
||
|
if not msg:
|
||
|
msg = "List lengths not equal"
|
||
|
ensure(False, msg)
|
||
|
|
||
|
for i in range(len(x)):
|
||
|
self.assertEqual(x[i], y[i], msg)
|
||
|
|
||
![]()
8 years ago
|
|
||
|
def skip(msg):
|
||
|
def _decor(fun):
|
||
|
# We just replace original fun with _inner
|
||
|
def _inner(self):
|
||
|
raise SkipTest(msg)
|
||
|
return _inner
|
||
|
return _decor
|
||
|
|
||
|
|
||
|
def skipUnless(cond, msg):
|
||
|
if cond:
|
||
|
return lambda x: x
|
||
|
return skip(msg)
|
||
|
|
||
|
|
||
|
class TestSuite:
|
||
![]()
8 years ago
|
|
||
![]()
8 years ago
|
def __init__(self):
|
||
|
self.tests = []
|
||
![]()
8 years ago
|
|
||
![]()
8 years ago
|
def addTest(self, cls):
|
||
|
self.tests.append(cls)
|
||
|
|
||
![]()
8 years ago
|
|
||
![]()
8 years ago
|
class TestRunner:
|
||
![]()
8 years ago
|
|
||
![]()
8 years ago
|
def run(self, suite):
|
||
|
res = TestResult()
|
||
|
for c in suite.tests:
|
||
|
run_class(c, res)
|
||
|
return res
|
||
|
|
||
![]()
8 years ago
|
|
||
![]()
8 years ago
|
class TestResult:
|
||
![]()
8 years ago
|
|
||
![]()
8 years ago
|
def __init__(self):
|
||
|
self.errorsNum = 0
|
||
|
self.failuresNum = 0
|
||
|
self.skippedNum = 0
|
||
|
self.testsRun = 0
|
||
|
|
||
|
def wasSuccessful(self):
|
||
|
return self.errorsNum == 0 and self.failuresNum == 0
|
||
|
|
||
![]()
8 years ago
|
|
||
![]()
8 years ago
|
def run_class(c, test_result):
|
||
|
o = c()
|
||
|
set_up = getattr(o, "setUp", lambda: None)
|
||
|
tear_down = getattr(o, "tearDown", lambda: None)
|
||
|
print('class', c.__qualname__)
|
||
|
for name in dir(o):
|
||
|
if name.startswith("test"):
|
||
![]()
6 years ago
|
is_async = name.startswith("test_async")
|
||
![]()
8 years ago
|
print(' ', name, end=' ...')
|
||
![]()
8 years ago
|
m = getattr(o, name)
|
||
|
try:
|
||
|
set_up()
|
||
|
test_result.testsRun += 1
|
||
![]()
6 years ago
|
if is_async:
|
||
|
assert_async(m(), [(None, StopIteration()), ])
|
||
|
else:
|
||
|
m()
|
||
![]()
8 years ago
|
tear_down()
|
||
|
print(" ok")
|
||
|
except SkipTest as e:
|
||
|
print(" skipped:", e.args[0])
|
||
|
test_result.skippedNum += 1
|
||
|
|
||
|
|
||
|
def main(module="__main__"):
|
||
|
def test_cases(m):
|
||
|
for tn in dir(m):
|
||
|
c = getattr(m, tn)
|
||
|
if isinstance(c, object) and isinstance(c, type) and issubclass(c, TestCase):
|
||
|
yield c
|
||
|
|
||
|
m = __import__(module)
|
||
|
suite = TestSuite()
|
||
|
for c in test_cases(m):
|
||
|
suite.addTest(c)
|
||
|
runner = TestRunner()
|
||
|
result = runner.run(suite)
|
||
|
msg = "Ran %d tests" % result.testsRun
|
||
|
if result.skippedNum > 0:
|
||
|
msg += " (%d skipped)" % result.skippedNum
|
||
|
print(msg)
|