190 lines
6.6 KiB
Python
190 lines
6.6 KiB
Python
"""
|
|
AuDaGe IEC tests
|
|
|
|
name: test_iec.py
|
|
by: Josh Lucas
|
|
date: 2026-09-14
|
|
|
|
desc: Unit tests for the AuDaGe IEC module.
|
|
"""
|
|
|
|
import unittest
|
|
import warnings
|
|
from importlib import resources
|
|
from math import ceil, floor
|
|
|
|
import output
|
|
|
|
from AuDaGe.iec import *
|
|
|
|
|
|
class TestIEC(unittest.TestCase):
|
|
"""Module-level tests."""
|
|
|
|
def setUp(self):
|
|
self.elementary_data_types = [
|
|
("BOOL", DataUnit.BIT, 1),
|
|
("CHAR", DataUnit.BIT, 8),
|
|
("INT", DataUnit.BIT, 16),
|
|
("BOOL", DataUnit.WORD, 1),
|
|
("ARRAY [1..10, 0..7] OF INT", DataUnit.WORD, 80),
|
|
("STRING[10]", DataUnit.WORD, 5),
|
|
]
|
|
self.udts = [
|
|
("KNOWN_UDT", DataUnit.WORD, 10),
|
|
("UNKNOWN_UDT", DataUnit.WORD, UNKNOWN_LENGTH),
|
|
]
|
|
self.udt_lengths = {"KNOWN_UDT": 160}
|
|
|
|
def test_get_data_length(self):
|
|
with warnings.catch_warnings():
|
|
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
|
|
),
|
|
data_type[2],
|
|
data_type[0],
|
|
)
|
|
|
|
|
|
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 = [
|
|
("ARRAY [0..3] OF INT", DataUnit.WORD, ceil, 4),
|
|
("ARRAY [0..0] OF REAL", DataUnit.WORD, ceil, 2),
|
|
("ARRAY [0..3, 0..3] OF INT", DataUnit.WORD, ceil, 16),
|
|
("array[15..20] of bool", DataUnit.BIT, ceil, 6),
|
|
("ARRAY [0..1, 0..2, 0..3, 0..4] OF INT", DataUnit.WORD, ceil, 120),
|
|
("arr[0..0]int", DataUnit.BYTE, ceil, 2),
|
|
("ARRAY [0..3] OF UDT", DataUnit.BIT, ceil, -1),
|
|
]
|
|
self.warning_array_specs = [
|
|
("array[6..12] of bool", DataUnit.WORD, ceil, 1),
|
|
("array[1..8] of bool", DataUnit.WORD, floor, 0),
|
|
(" \tarray[12..1234] of bool \t", DataUnit.WORD, floor, 76),
|
|
]
|
|
self.error_array_specs = (
|
|
"fleebleflorp", # Nonsense
|
|
"arry [0..5] of char", # Misspelling
|
|
"array [..6] of word", # Missing index
|
|
"array [1..2 of int", # Missing bracket
|
|
"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:
|
|
self.assertRaises(InvalidSpecError, 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]),
|
|
spec[3],
|
|
spec[0],
|
|
)
|
|
|
|
def test_make_definition(self):
|
|
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")
|
|
definition.to_csv(
|
|
resources.files(output).joinpath(
|
|
f"array_definition_{str.strip(spec[0])}.csv"
|
|
)
|
|
)
|
|
|
|
|
|
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),
|
|
]
|
|
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),
|
|
]
|
|
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 error_string_specs:
|
|
self.assertRaises(InvalidSpecError, String, spec[0])
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main(failfast=True)
|