""" 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 # noqa: F401 from math import ceil, floor 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 TestAddress(unittest.TestCase): def setUp(self): self.good_specs = ( ( "%MW300.04", (AddressType.M, AddressLength.W, 300, 4), "%MW300.04", "%MW00300.04", "400300.04", ), ( "%MW300.4", (AddressType.M, AddressLength.W, 300, 4), "%MW300.04", "%MW00300.04", "400300.04", ), ( "%IW65535.15", (AddressType.I, AddressLength.W, 65535, 15), "%IW65535.15", "%IW65535.15", "365535.15", ), ( "%I12345", (AddressType.I, None, 12345, None), "%I12345", "%I12345", "112345", ), ( "%M6440", (AddressType.M, None, 6440, None), "%M6440", "%M06440", "006440", ), ( "%IX12345", (AddressType.I, AddressLength.X, 12345, None), "%IX12345", "%IX12345", "112345", ), ( "%MX6440", (AddressType.M, AddressLength.X, 6440, None), "%MX6440", "%MX06440", "006440", ), ) # Semi-good specs are valid IEC but not Modbus addresses. self.semi_good_specs = ( ("q03", (AddressType.Q, None, 3, None), "%Q3", "%Q00003"), ( "%QW7676.12", (AddressType.Q, AddressLength.W, 7676, 12), "%QW7676.12", "%QW07676.12", ), ( "%kd0", (AddressType.K, AddressLength.D, 0, None), "%KD0", "%KD00000", ), ( "%sw751", (AddressType.S, AddressLength.W, 751, None), "%SW751", "%SW00751", ), ( "%s323", (AddressType.S, None, 323, None), "%S323", "%S00323", ), ) self.bad_specs = ( "goobledygook", "%n300.4", "40012.3", "%M70000", "%sw1000.17", "%QF10", ) self.bad_args = (("n", None, 300, 4), ("Q", "F", 10, None)) self.good_modbus = ( ( "400001", 6, False, (AddressType.M, AddressLength.W, 1, None), ), ( 400001, 6, False, (AddressType.M, AddressLength.W, 1, None), ), ( "400001", 5, False, (AddressType.M, AddressLength.W, 1, None), ), ( "40001", 5, False, (AddressType.M, AddressLength.W, 1, None), ), ( "40001", 6, False, (AddressType.M, None, 40001, None), ), ( "40001", 6, True, (AddressType.M, AddressLength.X, 40001, None), ), ("000012", 6, False, (AddressType.M, None, 12, None)), ("12", 6, False, (AddressType.M, None, 12, None)), ("12", 6, True, (AddressType.M, AddressLength.X, 12, None)), ("100000", 6, False, (AddressType.I, None, 0, None)), ( "100000", 6, True, (AddressType.I, AddressLength.X, 0, None), ), ( "300123", 6, False, (AddressType.I, AddressLength.W, 123, None), ), ) self.bad_modbus = ("200000", "goobledygook", 600000) 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) ) def test_init(self): for args in self.bad_args: with self.assertRaises(InvalidSpecError, msg=args): Address(*args) def test_from_string(self): for spec in self.good_specs + self.semi_good_specs: self.assertEqual( Address.from_string(spec[0]), Address(*spec[1]), spec[0] ) for spec in self.bad_specs: with self.assertRaises(InvalidSpecError, msg=spec): Address.from_string(spec) def test_from_modbus(self): for spec in self.good_modbus: self.assertEqual( Address.from_modbus(spec[0], spec[1], spec[2]), Address(*spec[3]), ) for spec in self.bad_modbus: with self.assertRaises(InvalidSpecError, msg=spec): Address.from_modbus(spec) def test_to_string(self): for spec in self.good_specs: self.assertEqual(str(Address(*spec[1])), spec[2], spec[0]) self.assertEqual(Address(*spec[1]).to_string(5), spec[3], spec[0]) def test_to_modbus(self): for spec in self.good_specs: self.assertEqual(Address(*spec[1]).to_modbus(6), spec[4], spec[0]) for spec in self.semi_good_specs: with self.assertRaises(ValueError, msg=spec[0]): Address(*spec[1]).to_modbus() # Test whether to_modbus errors when the address is to large to fit in # the specified number of digits. self.assertRaises( ValueError, Address(*self.good_specs[2][1]).to_modbus, 5 ) 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: 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]), 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") # noqa: F841 # 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: with self.assertRaises(InvalidSpecError, msg=spec[0]): String(spec[0]) if __name__ == "__main__": unittest.main(failfast=True)