Files
templates/python/tests/test_legacycan.py

40 lines
1.7 KiB
Python

from protocan.legacycan import (
LegacyCanFormat, LegacyCanSource, PROJECTS, apply_packet, decode,
empty_register_bank, encode_command, encode_write, signed_word,
)
def test_catalog_matches_legacy_projects():
assert [project.name for project in PROJECTS] == [
"Буксир", "СЭДБМ", "Ледокол", "Бальзам", "23550", "23550.X",
"23550.2", "Янтарь", "23550 БСУ",
]
assert len(next(project for project in PROJECTS if project.name == "СЭДБМ").nodes) == 15
def test_rotating_three_word_codec_and_bank():
project = next(project for project in PROJECTS if project.name == "Бальзам")
node = project.nodes[0]
wire = encode_write(project.format, node.rx_id, 24, (41, 0xFFFE, 0x1234))
assert wire.data == bytes.fromhex("FF FE 12 34 E0 18 00 29")
packet = decode(project.format, node, wire.can_id, wire.data)
assert packet is not None
assert packet.present_values == ((24, 41), (25, 0xFFFE), (26, 0x1234))
assert packet.source is LegacyCanSource.FROM_DEVICE
bank = apply_packet(empty_register_bank(), packet, 7)
assert bank[25].value == 0xFFFE
assert bank[25].revision == 7
assert signed_word(bank[25].value) == -2
def test_address_in_identifier_codec_and_command():
project = next(project for project in PROJECTS if project.name == "23550.2")
node = project.nodes[2]
assert project.format is LegacyCanFormat.ADDRESS_IN_IDENTIFIER
wire = encode_write(project.format, node.tx_id, 17, (1, 2, 0xFFFF, 4))
assert wire.can_id & 0x7F == 17
assert wire.data == bytes.fromhex("00 01 00 02 FF FF 00 04")
command = encode_command(project, node, 7)
assert command.can_id & 0x7F == 127
assert command.data == b"\x00\x80"