1 # SPDX-FileCopyrightText: 2021 Melissa LeBlanc-Williams for Adafruit Industries
 
   3 # SPDX-License-Identifier: MIT
 
   4 """Based on https://raw.githubusercontent.com/micropython/micropython-lib/cfa1b9cce0c93a3115bbff3886c9bbcddd9e8047/unittest/unittest.py """
 
   8 class SkipTest(Exception):
 
  12 raiseException = False
 
  13 raiseBaseException = True
 
  16 class AssertRaisesContext:
 
  17     def __init__(self, exc):
 
  23     def __exit__(self, exc_type, exc_value, tb):
 
  25             assert False, "%r not raised" % self.expected
 
  26         if issubclass(exc_type, self.expected):
 
  32     def fail(self, msg=""):
 
  35     def assertEqual(self, x, y, msg=""):
 
  37             msg = "%r vs (expected) %r" % (x, y)
 
  40     def assertNotEqual(self, x, y, msg=""):
 
  42             msg = "%r not expected to be equal %r" % (x, y)
 
  45     def assertAlmostEqual(self, x, y, places=None, msg="", delta=None):
 
  48         if delta is not None and places is not None:
 
  49             raise TypeError("specify delta or places not both")
 
  52             if abs(x - y) <= delta:
 
  55                 msg = "%r != %r within %r delta" % (x, y, delta)
 
  59             if round(abs(y - x), places) == 0:
 
  62                 msg = "%r != %r within %r places" % (x, y, places)
 
  66     def assertNotAlmostEqual(self, x, y, places=None, msg="", delta=None):
 
  67         if delta is not None and places is not None:
 
  68             raise TypeError("specify delta or places not both")
 
  71             if not (x == y) and abs(x - y) > delta:
 
  74                 msg = "%r == %r within %r delta" % (x, y, delta)
 
  78             if not (x == y) and round(abs(y - x), places) != 0:
 
  81                 msg = "%r == %r within %r places" % (x, y, places)
 
  85     def assertIs(self, x, y, msg=""):
 
  87             msg = "%r is not %r" % (x, y)
 
  90     def assertIsNot(self, x, y, msg=""):
 
  92             msg = "%r is %r" % (x, y)
 
  93         assert x is not y, msg
 
  95     def assertIsNone(self, x, msg=""):
 
  97             msg = "%r is not None" % x
 
 100     def assertIsNotNone(self, x, msg=""):
 
 102             msg = "%r is None" % x
 
 103         assert x is not None, msg
 
 105     def assertTrue(self, x, msg=""):
 
 107             msg = "Expected %r to be True" % x
 
 110     def assertFalse(self, x, msg=""):
 
 112             msg = "Expected %r to be False" % x
 
 115     def assertIn(self, x, y, msg=""):
 
 117             msg = "Expected %r to be in %r" % (x, y)
 
 120     def assertIsInstance(self, x, y, msg=""):
 
 121         assert isinstance(x, y), msg
 
 123     def assertRaises(self, exc, func=None, *args, **kwargs):
 
 125             return AssertRaisesContext(exc)
 
 128             func(*args, **kwargs)
 
 129             assert False, "%r not raised" % exc
 
 130         except Exception as e:
 
 131             if isinstance(e, exc):
 
 138         # We just replace original fun with _inner
 
 147 def skipIf(cond, msg):
 
 153 def skipUnless(cond, msg):
 
 163     def addTest(self, cls):
 
 164         self.tests.append(cls)
 
 168     def run(self, suite):
 
 170         for c in suite.tests:
 
 173         print("Ran %d tests\n" % res.testsRun)
 
 174         if res.failuresNum > 0 or res.errorsNum > 0:
 
 175             print("FAILED (failures=%d, errors=%d)" % (res.failuresNum, res.errorsNum))
 
 178             if res.skippedNum > 0:
 
 179                 msg += " (%d skipped)" % res.skippedNum
 
 192     def wasSuccessful(self):
 
 193         return self.errorsNum == 0 and self.failuresNum == 0
 
 197 def run_class(c, test_result):
 
 199     set_up = getattr(o, "setUp", lambda: None)
 
 200     tear_down = getattr(o, "tearDown", lambda: None)
 
 202         if name.startswith("test"):
 
 203             print("%s (%s) ..." % (name, c.__qualname__), end="")
 
 207                 test_result.testsRun += 1
 
 210             except SkipTest as e:
 
 211                 print(" skipped:", e.args[0])
 
 212                 test_result.skippedNum += 1
 
 213             except Exception as e:  # user exception
 
 219                     test_result.failuresNum += 1
 
 221             except BaseException as e:  # system exception
 
 223                 if raiseBaseException:
 
 227                     test_result.failuresNum += 1
 
 233 def main(module="__main__"):
 
 238                 isinstance(c, object)
 
 239                 and isinstance(c, type)
 
 240                 and issubclass(c, TestCase)
 
 244     m = __import__(module)  # changed to permit non-top-level testing modules
 
 246     for c in test_cases(m):
 
 248     runner = TestRunner()
 
 249     result = runner.run(suite)