amaranth/nmigen/test/tools.py
whitequark 664b4bcb3a hdl.dsl: cases wider than switch test value are unreachable.
In 3083c1d6 they were erroneously fixed via truncation.
2019-01-13 08:51:49 +00:00

46 lines
1.4 KiB
Python

import re
import unittest
import warnings
from contextlib import contextmanager
from ..hdl.ast import *
__all__ = ["FHDLTestCase"]
class FHDLTestCase(unittest.TestCase):
def assertRepr(self, obj, repr_str):
obj = Statement.wrap(obj)
def prepare_repr(repr_str):
repr_str = re.sub(r"\s+", " ", repr_str)
repr_str = re.sub(r"\( (?=\()", "(", repr_str)
repr_str = re.sub(r"\) (?=\))", ")", repr_str)
return repr_str.strip()
self.assertEqual(prepare_repr(repr(obj)), prepare_repr(repr_str))
@contextmanager
def assertRaises(self, exception, msg=None):
with super().assertRaises(exception) as cm:
yield
if msg is not None:
# WTF? unittest.assertRaises is completely broken.
self.assertEqual(str(cm.exception), msg)
@contextmanager
def assertRaisesRegex(self, exception, regex=None):
with super().assertRaises(exception) as cm:
yield
if regex is not None:
# unittest.assertRaisesRegex also seems broken...
self.assertRegex(str(cm.exception), regex)
@contextmanager
def assertWarns(self, category, msg=None):
with warnings.catch_warnings(record=True) as warns:
yield
self.assertEqual(len(warns), 1)
self.assertEqual(warns[0].category, category)
if msg is not None:
self.assertEqual(str(warns[0].message), msg)