hdl.ast: allow typed int enums in Value.cast.

This commit is contained in:
Arusekk 2023-01-22 22:37:49 +01:00 committed by Catherine
parent 91d4513682
commit 58a0c68279
3 changed files with 31 additions and 4 deletions

View file

@ -23,6 +23,12 @@ class StringEnum(Enum):
BAR = "b"
class TypedEnum(int, Enum):
FOO = 1
BAR = 2
BAZ = 3
class ShapeTestCase(FHDLTestCase):
def test_make(self):
s1 = Shape()
@ -199,6 +205,11 @@ class ValueTestCase(FHDLTestCase):
self.assertIsInstance(e2, Const)
self.assertEqual(e2.shape(), signed(2))
def test_cast_typedenum(self):
e1 = Value.cast(TypedEnum.FOO)
self.assertIsInstance(e1, Const)
self.assertEqual(e1.shape(), unsigned(2))
def test_cast_enum_wrong(self):
with self.assertRaisesRegex(TypeError,
r"^Only enumerations with integer values can be used as value shapes$"):
@ -781,6 +792,15 @@ class CatTestCase(FHDLTestCase):
warnings.filterwarnings(action="error", category=SyntaxWarning)
Cat(0, 1, 1, 0)
def test_enum(self):
class Color(Enum):
RED = 1
BLUE = 2
with warnings.catch_warnings():
warnings.filterwarnings(action="error", category=SyntaxWarning)
c = Cat(Color.RED, Color.BLUE)
self.assertEqual(repr(c), "(cat (const 2'd1) (const 2'd2))")
def test_int_wrong(self):
with self.assertWarnsRegex(SyntaxWarning,
r"^Argument #1 of Cat\(\) is a bare integer 2 used in bit vector context; "