Skip to content

Commit

Permalink
Added test for the modified bytes_from_dict to test the support for p…
Browse files Browse the repository at this point in the history
…rocessing nested structures
  • Loading branch information
msfur committed May 7, 2024
1 parent 4fad990 commit fc4b02b
Showing 1 changed file with 66 additions and 0 deletions.
66 changes: 66 additions & 0 deletions tests/test_ads.py
Original file line number Diff line number Diff line change
Expand Up @@ -781,6 +781,72 @@ def test_bytes_from_dict(self) -> None:
with self.assertRaises(KeyError):
pyads.bytes_from_dict(OrderedDict(), structure_def)

# tests for known values
substructure_def = (
("rVar", pyads.PLCTYPE_LREAL, 1),
("sVar", pyads.PLCTYPE_STRING, 2, 35),
("wsVar", pyads.PLCTYPE_WSTRING, 2, 10),
("rVar1", pyads.PLCTYPE_REAL, 4),
("iVar", pyads.PLCTYPE_DINT, 5),
("iVar1", pyads.PLCTYPE_INT, 3),
("ivar2", pyads.PLCTYPE_UDINT, 6),
("iVar3", pyads.PLCTYPE_UINT, 7),
("iVar4", pyads.PLCTYPE_BYTE, 1),
("iVar5", pyads.PLCTYPE_SINT, 1),
("iVar6", pyads.PLCTYPE_USINT, 1),
("bVar", pyads.PLCTYPE_BOOL, 4),
("iVar7", pyads.PLCTYPE_WORD, 1),
("iVar8", pyads.PLCTYPE_DWORD, 1),
)
subvalues = OrderedDict(
[
("rVar", 1.11),
("sVar", ["Hello", "World"]),
("wsVar", ["foo", "bar"]),
("rVar1", [2.25, 2.25, 2.5, 2.75]),
("iVar", [3, 4, 5, 6, 7]),
("iVar1", [8, 9, 10]),
("ivar2", [11, 12, 13, 14, 15, 16]),
("iVar3", [17, 18, 19, 20, 21, 22, 23]),
("iVar4", 24),
("iVar5", 25),
("iVar6", 26),
("bVar", [True, False, True, False]),
("iVar7", 27),
("iVar8", 28),
]
)
# fmt: off
subbytes_list = [195, 245, 40, 92, 143, 194, 241, 63, 72, 101, 108, 108, 111,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 87, 111, 114, 108, 100, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 102, 0, 111, 0, 111, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 98, 0, 97, 0, 114,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 16,
64, 0, 0, 16, 64, 0, 0, 32, 64, 0, 0, 48, 64, 3, 0, 0, 0, 4,
0, 0, 0, 5, 0, 0, 0, 6, 0, 0, 0, 7, 0, 0, 0, 8, 0, 9, 0, 10,
0, 11, 0, 0, 0, 12, 0, 0, 0, 13, 0, 0, 0, 14, 0, 0, 0, 15, 0,
0, 0, 16, 0, 0, 0, 17, 0, 18, 0, 19, 0, 20, 0, 21, 0, 22, 0,
23, 0, 24, 25, 26, 1, 0, 1, 0, 27, 0, 28, 0, 0, 0]

# test structure with array of nested structure
structure_def = (
('iVar9', pyads.PLCTYPE_USINT, 1),
('structVar', substructure_def, 2),
)
values = OrderedDict(
[
("iVar9", 29),
("structVar", [subvalues, subvalues,]),
]
)
# fmt: off
bytes_list = [29] + subbytes_list + subbytes_list

# fmt: on
self.assertEqual(bytes_list, pyads.bytes_from_dict(values, structure_def))

def test_dict_slice_generator(self):
"""test _dict_slice_generator function."""
test_dict = {
Expand Down

0 comments on commit fc4b02b

Please sign in to comment.