1 """Based on https://raw.githubusercontent.com/micropython/micropython-lib/cfa1b9cce0c93a3115bbff3886c9bbcddd9e8047/unittest/unittest.py """
 
   3 class SkipTest(Exception):
 
   7 raiseBaseException = True
 
   9 class AssertRaisesContext:
 
  11     def __init__(self, exc):
 
  17     def __exit__(self, exc_type, exc_value, tb):
 
  19             assert False, "%r not raised" % self.expected
 
  20         if issubclass(exc_type, self.expected):
 
  27     def fail(self, msg=''):
 
  30     def assertEqual(self, x, y, msg=''):
 
  32             msg = "%r vs (expected) %r" % (x, y)
 
  35     def assertNotEqual(self, x, y, msg=''):
 
  37             msg = "%r not expected to be equal %r" % (x, y)
 
  40     def assertAlmostEqual(self, x, y, places=None, msg='', delta=None):
 
  43         if delta is not None and places is not None:
 
  44             raise TypeError("specify delta or places not both")
 
  47             if abs(x - y) <= delta:
 
  50                 msg = '%r != %r within %r delta' % (x, y, delta)
 
  54             if round(abs(y-x), places) == 0:
 
  57                 msg = '%r != %r within %r places' % (x, y, places)
 
  61     def assertNotAlmostEqual(self, x, y, places=None, msg='', delta=None):
 
  62         if delta is not None and places is not None:
 
  63             raise TypeError("specify delta or places not both")
 
  66             if not (x == y) and abs(x - y) > delta:
 
  69                 msg = '%r == %r within %r delta' % (x, y, delta)
 
  73             if not (x == y) and round(abs(y-x), places) != 0:
 
  76                 msg = '%r == %r within %r places' % (x, y, places)
 
  80     def assertIs(self, x, y, msg=''):
 
  82             msg = "%r is not %r" % (x, y)
 
  85     def assertIsNot(self, x, y, msg=''):
 
  87             msg = "%r is %r" % (x, y)
 
  88         assert x is not y, msg
 
  90     def assertIsNone(self, x, msg=''):
 
  92             msg = "%r is not None" % x
 
  95     def assertIsNotNone(self, x, msg=''):
 
  97             msg = "%r is None" % x
 
  98         assert x is not None, msg
 
 100     def assertTrue(self, x, msg=''):
 
 102             msg = "Expected %r to be True" % x
 
 105     def assertFalse(self, x, msg=''):
 
 107             msg = "Expected %r to be False" % x
 
 110     def assertIn(self, x, y, msg=''):
 
 112             msg = "Expected %r to be in %r" % (x, y)
 
 115     def assertIsInstance(self, x, y, msg=''):
 
 116         assert isinstance(x, y), msg
 
 118     def assertRaises(self, exc, func=None, *args, **kwargs):
 
 120             return AssertRaisesContext(exc)
 
 123             func(*args, **kwargs)
 
 124             assert False, "%r not raised" % exc
 
 125         except Exception as e:
 
 126             if isinstance(e, exc):
 
 134         # We just replace original fun with _inner
 
 140 def skipIf(cond, msg):
 
 145 def skipUnless(cond, msg):
 
 154     def addTest(self, cls):
 
 155         self.tests.append(cls)
 
 158     def run(self, suite):
 
 160         for c in suite.tests:
 
 163         print("Ran %d tests\n" % res.testsRun)
 
 164         if res.failuresNum > 0 or res.errorsNum > 0:
 
 165             print("FAILED (failures=%d, errors=%d)" % (res.failuresNum, res.errorsNum))
 
 168             if res.skippedNum > 0:
 
 169                 msg += " (%d skipped)" % res.skippedNum
 
 181     def wasSuccessful(self):
 
 182         return self.errorsNum == 0 and self.failuresNum == 0
 
 185 def run_class(c, test_result):
 
 187     set_up = getattr(o, "setUp", lambda: None)
 
 188     tear_down = getattr(o, "tearDown", lambda: None)
 
 190         if name.startswith("test"):
 
 191             print("%s (%s) ..." % (name, c.__qualname__), end="")
 
 195                 test_result.testsRun += 1
 
 198             except SkipTest as e:
 
 199                 print(" skipped:", e.args[0])
 
 200                 test_result.skippedNum += 1
 
 201             except Exception as e: # user exception
 
 207                     test_result.failuresNum += 1
 
 209             except BaseException as e: # system exception
 
 211                 if raiseBaseException:
 
 215                     test_result.failuresNum += 1
 
 221 def main(module="__main__"):
 
 225             if isinstance(c, object) and isinstance(c, type) and issubclass(c, TestCase):
 
 228     m = __import__(module) # changed to permit non-top-level testing modules
 
 230     for c in test_cases(m):
 
 232     runner = TestRunner()
 
 233     result = runner.run(suite)