1 """Based on https://raw.githubusercontent.com/micropython/micropython-lib/cfa1b9cce0c93a3115bbff3886c9bbcddd9e8047/unittest/unittest.py """
 
   5 class SkipTest(Exception):
 
  10 raiseBaseException = True
 
  13 class AssertRaisesContext:
 
  14     def __init__(self, exc):
 
  20     def __exit__(self, exc_type, exc_value, tb):
 
  22             assert False, "%r not raised" % self.expected
 
  23         if issubclass(exc_type, self.expected):
 
  29     def fail(self, msg=""):
 
  32     def assertEqual(self, x, y, msg=""):
 
  34             msg = "%r vs (expected) %r" % (x, y)
 
  37     def assertNotEqual(self, x, y, msg=""):
 
  39             msg = "%r not expected to be equal %r" % (x, y)
 
  42     def assertAlmostEqual(self, x, y, places=None, msg="", delta=None):
 
  45         if delta is not None and places is not None:
 
  46             raise TypeError("specify delta or places not both")
 
  49             if abs(x - y) <= delta:
 
  52                 msg = "%r != %r within %r delta" % (x, y, delta)
 
  56             if round(abs(y - x), places) == 0:
 
  59                 msg = "%r != %r within %r places" % (x, y, places)
 
  63     def assertNotAlmostEqual(self, x, y, places=None, msg="", delta=None):
 
  64         if delta is not None and places is not None:
 
  65             raise TypeError("specify delta or places not both")
 
  68             if not (x == y) and abs(x - y) > delta:
 
  71                 msg = "%r == %r within %r delta" % (x, y, delta)
 
  75             if not (x == y) and round(abs(y - x), places) != 0:
 
  78                 msg = "%r == %r within %r places" % (x, y, places)
 
  82     def assertIs(self, x, y, msg=""):
 
  84             msg = "%r is not %r" % (x, y)
 
  87     def assertIsNot(self, x, y, msg=""):
 
  89             msg = "%r is %r" % (x, y)
 
  90         assert x is not y, msg
 
  92     def assertIsNone(self, x, msg=""):
 
  94             msg = "%r is not None" % x
 
  97     def assertIsNotNone(self, x, msg=""):
 
  99             msg = "%r is None" % x
 
 100         assert x is not None, msg
 
 102     def assertTrue(self, x, msg=""):
 
 104             msg = "Expected %r to be True" % x
 
 107     def assertFalse(self, x, msg=""):
 
 109             msg = "Expected %r to be False" % x
 
 112     def assertIn(self, x, y, msg=""):
 
 114             msg = "Expected %r to be in %r" % (x, y)
 
 117     def assertIsInstance(self, x, y, msg=""):
 
 118         assert isinstance(x, y), msg
 
 120     def assertRaises(self, exc, func=None, *args, **kwargs):
 
 122             return AssertRaisesContext(exc)
 
 125             func(*args, **kwargs)
 
 126             assert False, "%r not raised" % exc
 
 127         except Exception as e:
 
 128             if isinstance(e, exc):
 
 135         # We just replace original fun with _inner
 
 144 def skipIf(cond, msg):
 
 150 def skipUnless(cond, msg):
 
 160     def addTest(self, cls):
 
 161         self.tests.append(cls)
 
 165     def run(self, suite):
 
 167         for c in suite.tests:
 
 170         print("Ran %d tests\n" % res.testsRun)
 
 171         if res.failuresNum > 0 or res.errorsNum > 0:
 
 172             print("FAILED (failures=%d, errors=%d)" % (res.failuresNum, res.errorsNum))
 
 175             if res.skippedNum > 0:
 
 176                 msg += " (%d skipped)" % res.skippedNum
 
 189     def wasSuccessful(self):
 
 190         return self.errorsNum == 0 and self.failuresNum == 0
 
 194 def run_class(c, test_result):
 
 196     set_up = getattr(o, "setUp", lambda: None)
 
 197     tear_down = getattr(o, "tearDown", lambda: None)
 
 199         if name.startswith("test"):
 
 200             print("%s (%s) ..." % (name, c.__qualname__), end="")
 
 204                 test_result.testsRun += 1
 
 207             except SkipTest as e:
 
 208                 print(" skipped:", e.args[0])
 
 209                 test_result.skippedNum += 1
 
 210             except Exception as e:  # user exception
 
 216                     test_result.failuresNum += 1
 
 218             except BaseException as e:  # system exception
 
 220                 if raiseBaseException:
 
 224                     test_result.failuresNum += 1
 
 230 def main(module="__main__"):
 
 235                 isinstance(c, object)
 
 236                 and isinstance(c, type)
 
 237                 and issubclass(c, TestCase)
 
 241     m = __import__(module)  # changed to permit non-top-level testing modules
 
 243     for c in test_cases(m):
 
 245     runner = TestRunner()
 
 246     result = runner.run(suite)