Major IEC module refactor.
This commit is contained in:
@@ -10,81 +10,148 @@ 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 test_get_data_length(self):
|
||||
data_types = [
|
||||
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),
|
||||
("UNKNOWN_UDT", DataUnit.WORD, -1),
|
||||
]
|
||||
for data_type in data_types:
|
||||
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(
|
||||
get_data_length(data_type[0], data_type[1]), data_type[2], data_type[0]
|
||||
ElementaryTypes.contains(
|
||||
data_type[0], include_string=False, include_array=False
|
||||
),
|
||||
data_type[1],
|
||||
)
|
||||
|
||||
def test_iec_definitions(self):
|
||||
definitions = iec_definitions()
|
||||
self.assertTrue(definitions.is_valid())
|
||||
self.assertEqual(definitions.shape, (len(IECTypes.__members__), 7))
|
||||
for data_type in invalid_types:
|
||||
self.assertFalse(data_type in ElementaryTypes, data_type)
|
||||
|
||||
|
||||
class TestIECArray(unittest.TestCase):
|
||||
def test_array_length(self):
|
||||
good_array_specs = [
|
||||
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),
|
||||
]
|
||||
warning_array_specs = [
|
||||
("array[15..20] of bool", DataUnit.WORD, ceil, 1),
|
||||
("array[15..20] of bool", DataUnit.WORD, floor, 0),
|
||||
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),
|
||||
]
|
||||
with warnings.catch_warnings():
|
||||
warnings.simplefilter("ignore")
|
||||
for spec in good_array_specs + warning_array_specs:
|
||||
self.assertEqual(
|
||||
IECArray(spec[0]).get_length(spec[1], spec[2]), spec[3], spec[0]
|
||||
)
|
||||
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 = (
|
||||
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 [1..7] of float", # Invalid data type
|
||||
"array [5..1] of dint", # Lower bound greater than upper bound
|
||||
)
|
||||
for spec in valid_array_specs:
|
||||
self.assertTrue(IECArray(spec).is_valid(), spec)
|
||||
for spec in invalid_array_specs:
|
||||
self.assertRaises(ValueError, IECArray, spec)
|
||||
|
||||
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 TestIECString(unittest.TestCase):
|
||||
class TestString(unittest.TestCase):
|
||||
def test_string_length(self):
|
||||
good_string_specs = [
|
||||
("STRING[20]", DataUnit.WORD, ceil, 10),
|
||||
@@ -98,20 +165,24 @@ class TestIECString(unittest.TestCase):
|
||||
error_string_specs = [
|
||||
("STRNG[20]", DataUnit.WORD, ceil, 10),
|
||||
("STRING10]", DataUnit.WORD, ceil, 5),
|
||||
("string[]", DataUnit.BYTE, ceil, 0),
|
||||
("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(
|
||||
IECString(spec[0]).get_length(spec[1], spec[2]), spec[3], spec[0]
|
||||
String(spec[0], spec[2]).get_length(spec[1]),
|
||||
spec[3],
|
||||
spec[0],
|
||||
)
|
||||
for spec in warning_string_specs:
|
||||
self.assertWarns(
|
||||
RuntimeWarning, IECString(spec[0]).get_length, spec[1], spec[2]
|
||||
RuntimeWarning, String(spec[0], spec[2]).get_length, spec[1]
|
||||
)
|
||||
for spec in error_string_specs:
|
||||
self.assertRaises(ValueError, IECString, spec[0])
|
||||
self.assertRaises(InvalidSpecError, String, spec[0])
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
||||
Reference in New Issue
Block a user