mirror of
https://github.com/lowobservable/coax.git
synced 2026-04-30 21:49:34 +00:00
Refactor execute to take command word
This commit is contained in:
@@ -4,7 +4,7 @@ from unittest.mock import Mock
|
||||
import context
|
||||
|
||||
from coax import PollResponse, KeystrokePollResponse, ProtocolError
|
||||
from coax.protocol import Command, Status, TerminalId, _execute_read_command, _execute_write_command, _pack_command_word, _unpack_data_words, _unpack_data_word
|
||||
from coax.protocol import Command, Status, TerminalId, _execute_read_command, _execute_write_command, _pack_command_word, _unpack_command_word, _unpack_data_words, _unpack_data_word
|
||||
|
||||
class PollResponseTestCase(unittest.TestCase):
|
||||
def test_is_power_on_reset_complete(self):
|
||||
@@ -69,39 +69,49 @@ class ExecuteReadCommandTestCase(unittest.TestCase):
|
||||
|
||||
def test(self):
|
||||
# Arrange
|
||||
command_word = _pack_command_word(Command.READ_TERMINAL_ID)
|
||||
|
||||
self.interface.execute = Mock(return_value=[0b0000000010])
|
||||
|
||||
# Act and assert
|
||||
self.assertEqual(_execute_read_command(self.interface, Command.READ_TERMINAL_ID), bytes.fromhex('00'))
|
||||
self.assertEqual(_execute_read_command(self.interface, command_word), bytes.fromhex('00'))
|
||||
|
||||
def test_allow_trta_response(self):
|
||||
# Arrange
|
||||
command_word = _pack_command_word(Command.POLL)
|
||||
|
||||
self.interface.execute = Mock(return_value=[0b0000000000])
|
||||
|
||||
# Act and assert
|
||||
self.assertEqual(_execute_read_command(self.interface, Command.POLL, allow_trta_response=True, trta_value='TRTA'), 'TRTA')
|
||||
self.assertEqual(_execute_read_command(self.interface, command_word, allow_trta_response=True, trta_value='TRTA'), 'TRTA')
|
||||
|
||||
def test_disable_unpack_data_words(self):
|
||||
# Arrange
|
||||
command_word = _pack_command_word(Command.POLL)
|
||||
|
||||
self.interface.execute = Mock(return_value=[0b1111111110])
|
||||
|
||||
# Act and assert
|
||||
self.assertEqual(_execute_read_command(self.interface, Command.POLL, unpack_data_words=False), [0b1111111110])
|
||||
self.assertEqual(_execute_read_command(self.interface, command_word, unpack_data_words=False), [0b1111111110])
|
||||
|
||||
def test_unexpected_response_length(self):
|
||||
# Arrange
|
||||
command_word = _pack_command_word(Command.READ_TERMINAL_ID)
|
||||
|
||||
self.interface.execute = Mock(return_value=[])
|
||||
|
||||
# Act and assert
|
||||
with self.assertRaisesRegex(ProtocolError, 'Expected 1 word READ_TERMINAL_ID response'):
|
||||
_execute_read_command(self.interface, Command.READ_TERMINAL_ID)
|
||||
_execute_read_command(self.interface, command_word)
|
||||
|
||||
def test_timeout_is_passed_to_interface(self):
|
||||
# Arrange
|
||||
command_word = _pack_command_word(Command.READ_TERMINAL_ID)
|
||||
|
||||
self.interface.execute = Mock(return_value=[0b0000000010])
|
||||
|
||||
# Act
|
||||
_execute_read_command(self.interface, Command.READ_TERMINAL_ID, timeout=10)
|
||||
_execute_read_command(self.interface, command_word, timeout=10)
|
||||
|
||||
# Assert
|
||||
self.assertEqual(self.interface.execute.call_args[1].get('timeout'), 10)
|
||||
@@ -112,43 +122,72 @@ class ExecuteWriteCommandTestCase(unittest.TestCase):
|
||||
|
||||
def test(self):
|
||||
# Arrange
|
||||
command_word = _pack_command_word(Command.WRITE_DATA)
|
||||
|
||||
self.interface.execute = Mock(return_value=[0b0000000000])
|
||||
|
||||
# Act and assert
|
||||
_execute_write_command(self.interface, Command.WRITE_DATA, bytes.fromhex('de ad be ef'))
|
||||
_execute_write_command(self.interface, command_word, bytes.fromhex('de ad be ef'))
|
||||
|
||||
def test_unexpected_response_length(self):
|
||||
# Arrange
|
||||
command_word = _pack_command_word(Command.WRITE_DATA)
|
||||
|
||||
self.interface.execute = Mock(return_value=[])
|
||||
|
||||
# Act and assert
|
||||
with self.assertRaisesRegex(ProtocolError, 'Expected 1 word WRITE_DATA response'):
|
||||
_execute_write_command(self.interface, Command.WRITE_DATA, bytes.fromhex('de ad be ef'))
|
||||
_execute_write_command(self.interface, command_word, bytes.fromhex('de ad be ef'))
|
||||
|
||||
def test_not_trta_response(self):
|
||||
# Arrange
|
||||
command_word = _pack_command_word(Command.WRITE_DATA)
|
||||
|
||||
self.interface.execute = Mock(return_value=[0b0000000010])
|
||||
|
||||
# Act and assert
|
||||
with self.assertRaisesRegex(ProtocolError, 'Expected TR/TA response'):
|
||||
_execute_write_command(self.interface, Command.WRITE_DATA, bytes.fromhex('de ad be ef'))
|
||||
_execute_write_command(self.interface, command_word, bytes.fromhex('de ad be ef'))
|
||||
|
||||
def test_timeout_is_passed_to_interface(self):
|
||||
# Arrange
|
||||
command_word = _pack_command_word(Command.WRITE_DATA)
|
||||
|
||||
self.interface.execute = Mock(return_value=[0b0000000000])
|
||||
|
||||
# Assert
|
||||
_execute_write_command(self.interface, Command.WRITE_DATA, bytes.fromhex('de ad be ef'), timeout=10)
|
||||
_execute_write_command(self.interface, command_word, bytes.fromhex('de ad be ef'), timeout=10)
|
||||
|
||||
# Assert
|
||||
self.assertEqual(self.interface.execute.call_args[1].get('timeout'), 10)
|
||||
|
||||
class PackCommandWordTestCase(unittest.TestCase):
|
||||
def test_without_address(self):
|
||||
self.assertEqual(_pack_command_word(Command.POLL_ACK), 0b001000101)
|
||||
self.assertEqual(_pack_command_word(Command.POLL_ACK), 0b0001000101)
|
||||
|
||||
def test_with_address(self):
|
||||
self.assertEqual(_pack_command_word(Command.POLL_ACK, address=3), 0b111000101)
|
||||
self.assertEqual(_pack_command_word(Command.POLL_ACK, address=7), 0b1111000101)
|
||||
|
||||
class UnpackCommandWordTestCase(unittest.TestCase):
|
||||
def test_without_address(self):
|
||||
# Act
|
||||
(address, command) = _unpack_command_word(0b0001000101)
|
||||
|
||||
# Assert
|
||||
self.assertEqual(address, 0)
|
||||
self.assertEqual(command, Command.POLL_ACK)
|
||||
|
||||
def test_with_address(self):
|
||||
# Act
|
||||
(address, command) = _unpack_command_word(0b1111000101)
|
||||
|
||||
# Assert
|
||||
self.assertEqual(address, 7)
|
||||
self.assertEqual(command, Command.POLL_ACK)
|
||||
|
||||
def test_command_bit_not_set_error(self):
|
||||
with self.assertRaisesRegex(ProtocolError, 'Word does not have command bit set'):
|
||||
_unpack_command_word(0b0001000100)
|
||||
|
||||
class UnpackDataWordsTestCase(unittest.TestCase):
|
||||
def test(self):
|
||||
|
||||
Reference in New Issue
Block a user