Init Commit: Moved bproto to seperate repo
This commit is contained in:
98
test/compiler/test_field.py
Normal file
98
test/compiler/test_field.py
Normal file
@@ -0,0 +1,98 @@
|
||||
import sys
|
||||
sys.path.append('src/')
|
||||
|
||||
from protocol_components.dtypes import BprotoFieldBaseType, BprotoArrayPolicyViolationError, BprotoUnknownDataTypeError
|
||||
from protocol_components.field import Field, FactoryField, FieldBitfield, FieldBitfieldRef
|
||||
from protocol_components.bitfields import Bitfield
|
||||
from nameHandling.base import ComponentName
|
||||
|
||||
|
||||
def test_field_object():
|
||||
field = Field("field1", 3, BprotoFieldBaseType.UINT64, 4)
|
||||
|
||||
assert field.name == "field1"
|
||||
assert field.pos == 3
|
||||
assert field.type == BprotoFieldBaseType.UINT64
|
||||
assert field.array_size == 4
|
||||
|
||||
assert field.get_identifier() == "field1"
|
||||
assert field.get_name() == "field1"
|
||||
assert field.get_type_name() == "field"
|
||||
assert field.get_size_bytes() == 8 * 4
|
||||
assert field.get_size_bits() == 64 * 4
|
||||
|
||||
|
||||
def test_field_factory():
|
||||
factory = FactoryField()
|
||||
|
||||
field: Field = factory.assemble("field1", 3, BprotoFieldBaseType.UINT64.value, 4, None)
|
||||
|
||||
assert isinstance(field, Field)
|
||||
assert field.name == "field1"
|
||||
assert field.pos == 3
|
||||
assert field.type == BprotoFieldBaseType.UINT64
|
||||
assert field.array_size == 4
|
||||
|
||||
assert isinstance(field.name, ComponentName)
|
||||
assert isinstance(field.get_identifier(), ComponentName)
|
||||
assert isinstance(field.get_name(), ComponentName)
|
||||
assert isinstance(field.get_type_name(), str)
|
||||
|
||||
|
||||
def test_field_factory_errors_invalid_datatype():
|
||||
factory = FactoryField()
|
||||
try:
|
||||
factory.assemble(
|
||||
"field1", 3, "junkDataType", 4, None
|
||||
)
|
||||
assert False
|
||||
except BprotoUnknownDataTypeError:
|
||||
assert True
|
||||
except Exception as e:
|
||||
assert False, f"Unexpected exception: {e}"
|
||||
|
||||
|
||||
def test_field_factory_errors_invalid_array_policy():
|
||||
factory = FactoryField()
|
||||
try:
|
||||
factory.assemble(
|
||||
"field1", 1, BprotoFieldBaseType.BITFIELD, 4, None
|
||||
)
|
||||
assert False
|
||||
except BprotoArrayPolicyViolationError:
|
||||
assert True
|
||||
except Exception as e:
|
||||
assert False, f"Unexpected exception: {e}"
|
||||
|
||||
|
||||
def test_field_factory_bitfield():
|
||||
factory = FactoryField()
|
||||
|
||||
field: FieldBitfield = factory.assemble(
|
||||
"field1", 3, BprotoFieldBaseType.BITFIELD, 1,
|
||||
Bitfield("test")
|
||||
)
|
||||
|
||||
assert isinstance(field, FieldBitfield)
|
||||
assert field.name == "field1"
|
||||
assert field.pos == 3
|
||||
assert field.type == BprotoFieldBaseType.BITFIELD
|
||||
assert field.array_size == 1
|
||||
assert isinstance(field.bitfield, Bitfield)
|
||||
assert field.bitfield.get_name() == "test"
|
||||
|
||||
|
||||
def test_field_factory_bitfield_ref():
|
||||
factory = FactoryField()
|
||||
|
||||
field: FieldBitfield = factory.assemble(
|
||||
"field1", 3, BprotoFieldBaseType.BITFIELD, 1,
|
||||
"test"
|
||||
)
|
||||
|
||||
assert isinstance(field, FieldBitfieldRef)
|
||||
assert field.name == "field1"
|
||||
assert field.pos == 3
|
||||
assert field.type == BprotoFieldBaseType.BITFIELD
|
||||
assert field.array_size == 1
|
||||
assert field.ref == "test"
|
||||
Reference in New Issue
Block a user