hdl.ast: implement ValueCastable.

Closes RFC issue #355.
This commit is contained in:
awygle 2020-11-05 16:20:54 -08:00 committed by GitHub
parent 0ef01b1282
commit 06c734992f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 96 additions and 1 deletions

View file

@ -1025,6 +1025,52 @@ class UserValueTestCase(FHDLTestCase):
self.assertEqual(uv.lower_count, 1)
class MockValueCastableChanges(ValueCastable):
def __init__(self, width=0):
self.width = width
@ValueCastable.lowermethod
def as_value(self):
return Signal(self.width)
class MockValueCastableNotDecorated(ValueCastable):
def __init__(self):
pass
def as_value(self):
return Signal()
class MockValueCastableNoOverride(ValueCastable):
def __init__(self):
pass
class ValueCastableTestCase(FHDLTestCase):
def test_not_decorated(self):
with self.assertRaisesRegex(TypeError,
r"^Class 'MockValueCastableNotDecorated' deriving from `ValueCastable` must decorate the `as_value` "
r"method with the `ValueCastable.lowermethod` decorator$"):
vc = MockValueCastableNotDecorated()
def test_no_override(self):
with self.assertRaisesRegex(TypeError,
r"^Class 'MockValueCastableNoOverride' deriving from `ValueCastable` must override the `as_value` "
r"method$"):
vc = MockValueCastableNoOverride()
def test_memoized(self):
vc = MockValueCastableChanges(1)
sig1 = vc.as_value()
vc.width = 2
sig2 = vc.as_value()
self.assertIs(sig1, sig2)
vc.width = 3
sig3 = Value.cast(vc)
self.assertIs(sig1, sig3)
class SampleTestCase(FHDLTestCase):
def test_const(self):
s = Sample(1, 1, "sync")