""" 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 math import ceil, floor from AuDaGe.iec import * class TestIEC(unittest.TestCase): """Module-level tests.""" def test_get_data_length(self): data_types = [ ("BOOL", DataUnit.BIT, 1), ("CHAR", DataUnit.BIT, 8), ("INT", DataUnit.BIT, 16), ("BOOL", DataUnit.WORD, 1), ("ARRAY [0..10] OF INT", DataUnit.WORD, 11), ("STRING[10]", DataUnit.WORD, 5), ("UNKNOWN_UDT", DataUnit.WORD, -1), ] for data_type in data_types: self.assertEqual( get_data_length(data_type[0], data_type[1]), data_type[2], data_type ) def test_iec_definitions(self): definitions = iec_definitions() self.assertTrue(definitions.is_valid()) self.assertEqual(definitions.shape, (len(IECTypes.__members__), 7)) class TestIECArray(unittest.TestCase): def test_array_length(self): good_array_specs = [ ("ARRAY [0..3] OF INT", DataUnit.WORD, ceil, 4), ("array[15..20] of bool", DataUnit.BIT, ceil, 6), ] warning_array_specs = [ ("array[15..20] of bool", DataUnit.WORD, ceil, 1), ("array[15..20] of bool", DataUnit.WORD, floor, 0), ] with warnings.catch_warnings(): warnings.simplefilter("ignore") self.assertTrue( all( IECArray(spec[0]).get_length(spec[1], spec[2]) == spec[3] for spec in good_array_specs + warning_array_specs ) ) for spec in warning_array_specs: self.assertWarns( RuntimeWarning, IECArray(spec[0]).get_length, spec[1], spec[2] ) def test_array_spec_is_valid(self): valid_array_specs = ( "ARRAY [0..3] OF INT", # Uppercase, space, two dots "array[12...1234] of bool", # Lowercase, no space, three dots " \tarray[12..1234] of bool \t", # Superfluous whitespace "arr[0..0]int", # Minimal unambiguous specification, bounds equal ) invalid_array_specs = ( "fleebleflorp", # Nonsense "arry [0..5] of char", # Misspelling "array [..6] of word", # Missing index "array [1..2 of int", # Missing bracket "array [1..7] of float", # Invalid data type "array [5..1] of dint", # Lower bound greater than upper bound ) self.assertTrue(all(IECArray(spec).is_valid() for spec in valid_array_specs)) for spec in invalid_array_specs: self.assertRaises(ValueError, IECArray, spec) class TestIECString(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[]", DataUnit.BYTE, ceil, 0), ] with warnings.catch_warnings(): warnings.simplefilter("ignore") self.assertTrue( all( IECString(spec[0]).get_length(spec[1], spec[2]) == spec[3] for spec in good_string_specs + warning_string_specs ) ) for spec in warning_string_specs: self.assertWarns( RuntimeWarning, IECString(spec[0]).get_length, spec[1], spec[2] ) for spec in error_string_specs: self.assertRaises(ValueError, IECString, spec[0]) if __name__ == "__main__": unittest.main(failfast=True)