Rewrite iec_type module to support converting to OPC UA types and to harmonize better with the rest of the project.
This commit is contained in:
@@ -30,7 +30,7 @@ class TestIEC(unittest.TestCase):
|
||||
]
|
||||
self.udts = [
|
||||
("KNOWN_UDT", DataUnit.WORD, 10),
|
||||
("UNKNOWN_UDT", DataUnit.WORD, UNKNOWN_LENGTH),
|
||||
("UNKNOWN_UDT", DataUnit.BIT, UNKNOWN_LENGTH),
|
||||
]
|
||||
self.udt_lengths = {"KNOWN_UDT": 160}
|
||||
|
||||
@@ -39,9 +39,9 @@ class TestIEC(unittest.TestCase):
|
||||
warnings.simplefilter("ignore")
|
||||
for data_type in self.elementary_data_types + self.udts:
|
||||
self.assertEqual(
|
||||
get_data_length(
|
||||
data_type[0], data_type[1], self.udt_lengths
|
||||
),
|
||||
IECType.from_string(
|
||||
data_type[0], self.udt_lengths
|
||||
).get_length(data_type[1]),
|
||||
data_type[2],
|
||||
data_type[0],
|
||||
)
|
||||
@@ -196,7 +196,7 @@ class TestAddress(unittest.TestCase):
|
||||
def test_eq(self):
|
||||
"""Test that the __eq__ method returns False for non-Address `other`."""
|
||||
self.assertNotEqual(
|
||||
Address(*self.good_specs[0][1]), IECType("test", IECClass.BINARY)
|
||||
Address(*self.good_specs[0][1]), IECType("test", IECClass.BINARY, 1)
|
||||
)
|
||||
|
||||
def test_init(self):
|
||||
@@ -241,47 +241,6 @@ class TestAddress(unittest.TestCase):
|
||||
)
|
||||
|
||||
|
||||
class TestIECType(unittest.TestCase):
|
||||
def test_set_length(self):
|
||||
test_type = IECType(
|
||||
"WORD", IECClass.BINARY, length=16, unit=DataUnit.BIT
|
||||
)
|
||||
self.assertEqual(test_type.get_length(DataUnit.WORD), 1)
|
||||
test_type.set_length(4, unit=DataUnit.BYTE)
|
||||
self.assertEqual(test_type.get_length(DataUnit.BIT), 32)
|
||||
|
||||
|
||||
class TestElementaryTypes(unittest.TestCase):
|
||||
def test_iter(self):
|
||||
count = 0
|
||||
for data_type in ElementaryTypes:
|
||||
count += 1
|
||||
self.assertNotEqual(count, 0)
|
||||
self.assertEqual(count, len(ElementaryTypes.__members__))
|
||||
|
||||
def test_contains(self):
|
||||
valid_types = [
|
||||
("bool", True),
|
||||
("INT", True),
|
||||
("STRING[20]", False),
|
||||
("string", True),
|
||||
("array [0..5] of int", False),
|
||||
]
|
||||
invalid_types = ["float", "UDT", "ARRAY [0..5] OF UDT"]
|
||||
for data_type in valid_types:
|
||||
# Test that all the valid_types return True for a permissive check.
|
||||
self.assertTrue(data_type[0] in ElementaryTypes, data_type)
|
||||
# Test that derived types return False for a strict check.
|
||||
self.assertEqual(
|
||||
ElementaryTypes.contains(
|
||||
data_type[0], include_string=False, include_array=False
|
||||
),
|
||||
data_type[1],
|
||||
)
|
||||
for data_type in invalid_types:
|
||||
self.assertFalse(data_type in ElementaryTypes, data_type)
|
||||
|
||||
|
||||
class TestArray(unittest.TestCase):
|
||||
def setUp(self):
|
||||
self.good_array_specs = [
|
||||
@@ -306,27 +265,13 @@ class TestArray(unittest.TestCase):
|
||||
"array [5..1] of dint", # Lower bound greater than upper bound
|
||||
)
|
||||
|
||||
def test_array(self):
|
||||
"""Test that Array raises DataUnitMismatchWarning when the length is not
|
||||
evenly divisible and InvalidSpecError for invalid specs.
|
||||
"""
|
||||
for spec in self.warning_array_specs:
|
||||
self.assertWarns(
|
||||
DataUnitMismatchWarning,
|
||||
Array(spec[0], round_function=spec[2]).get_length,
|
||||
spec[1],
|
||||
)
|
||||
for spec in self.error_array_specs:
|
||||
with self.assertRaises(InvalidSpecError, msg=spec):
|
||||
Array(spec)
|
||||
|
||||
def test_get_length(self):
|
||||
"""Test get_length for both good and warning array specs."""
|
||||
with warnings.catch_warnings():
|
||||
warnings.simplefilter("ignore")
|
||||
for spec in self.good_array_specs + self.warning_array_specs:
|
||||
self.assertEqual(
|
||||
Array(spec[0], round_function=spec[2]).get_length(spec[1]),
|
||||
IECType.from_string(spec[0]).get_length(spec[1], spec[2]),
|
||||
spec[3],
|
||||
spec[0],
|
||||
)
|
||||
@@ -335,48 +280,35 @@ class TestArray(unittest.TestCase):
|
||||
with warnings.catch_warnings():
|
||||
warnings.simplefilter("ignore")
|
||||
for spec in self.good_array_specs + self.warning_array_specs:
|
||||
array = Array(spec[0])
|
||||
definition = array.make_definition("test_name", "test_comment") # noqa: F841
|
||||
# definition.to_csv(
|
||||
# resources.files(output).joinpath(
|
||||
# f"array_definition_{str.strip(spec[0])}.csv"
|
||||
# )
|
||||
# )
|
||||
array = IECType.from_string(spec[0])
|
||||
definition = array.array_expansion("test_name", "test_comment") # noqa: F841
|
||||
|
||||
|
||||
class TestString(unittest.TestCase):
|
||||
def test_string_length(self):
|
||||
good_string_specs = [
|
||||
("STRING[20]", DataUnit.WORD, ceil, 10),
|
||||
("string [80]\t", DataUnit.BYTE, ceil, 80),
|
||||
(" string [10] ", DataUnit.BIT, ceil, 80),
|
||||
]
|
||||
warning_string_specs = [
|
||||
("\tSTRING[15]", DataUnit.WORD, ceil, 8),
|
||||
("STRING [15]", DataUnit.WORD, floor, 7),
|
||||
("STRING[20]", IECType("STRING[20]", IECClass.CHAR, 160)),
|
||||
("string [80]\t", IECType("string [80]\t", IECClass.CHAR, 640)),
|
||||
(" string [10] ", IECType(" string [10] ", IECClass.CHAR, 80)),
|
||||
("\tSTRING[15]", IECType("\tSTRING[15]", IECClass.CHAR, 120)),
|
||||
("STRING [15]", IECType("STRING [15]", IECClass.CHAR, 120)),
|
||||
]
|
||||
error_string_specs = [
|
||||
("STRNG[20]", DataUnit.WORD, ceil, 10),
|
||||
("STRING10]", DataUnit.WORD, ceil, 5),
|
||||
("string[q]", DataUnit.BYTE, ceil, 0),
|
||||
("string[]", DataUnit.BIT, ceil, 0),
|
||||
("string[0]", DataUnit.BIT, ceil, 0),
|
||||
"STRNG[20]",
|
||||
"STRING10]",
|
||||
"string[q]",
|
||||
"string[]",
|
||||
"string[0]",
|
||||
]
|
||||
with warnings.catch_warnings():
|
||||
warnings.simplefilter("ignore")
|
||||
for spec in good_string_specs + warning_string_specs:
|
||||
self.assertEqual(
|
||||
String(spec[0], spec[2]).get_length(spec[1]),
|
||||
spec[3],
|
||||
spec[0],
|
||||
)
|
||||
for spec in warning_string_specs:
|
||||
self.assertWarns(
|
||||
RuntimeWarning, String(spec[0], spec[2]).get_length, spec[1]
|
||||
for spec in good_string_specs:
|
||||
self.assertEqual(
|
||||
IECType.from_string(spec[0]),
|
||||
spec[1],
|
||||
spec[0],
|
||||
)
|
||||
for spec in error_string_specs:
|
||||
with self.assertRaises(InvalidSpecError, msg=spec[0]):
|
||||
String(spec[0])
|
||||
with self.assertRaises(InvalidSpecError, msg=spec):
|
||||
IECType.from_string(spec, allow_unknown=False)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
||||
Reference in New Issue
Block a user