SerialInterface Unit Tests¶
As documented in Serial Interface Level, SerialInterface provides functions to open, close, and read
from serial ports using Asio.
The SerialInterface tests use a SerialInterfaceTest class which derives from SerialInterface
specialized in SerialPortMock, which mocks asio::serial_port.
Since
SerialInterfaceTestcreates itsSerialPortMockin the constructor, no expectations can be set to that object. For this reason,SerialInterfaceTestprovides aset_serial_port()public member function that can be used to substitute theSerialPortMockinstance with one on which expectations have been set.To be able to construct this
SerialPortMock, a getterio_service()is also provided.Some tests need to mock
SerialPort::read_some()(asio::serial_port::read_some()) so thatSerialInterface::read_line()returns a specificstd::string. To that end,SerialInterfacewraps the call toSerialPort::read_some()with aread_char(), whichSerialInterface::read_line()calls to perform the actual read from the port. Since for unit testing purposesSerialPortMockis used instead ofasio::serial_port, a mockSerialPortMock::read_some()would be needed. However, due to the function’s signature, it is not possible to set expectations on the read characters. This has led toSerialInterfaceTestoverridingSerialInterface::read_char()with an overload that either simply calls to theSerialInterface::read_char()implementation, or returns a character from a string. To do this,SerialInterfaceTestprovides aset_msg()function that is used to set the line thatread_linewill read. To enableSerialInterfaceTest::read_char()to read characters from the set message instead of usingread_some(), ause_parent_read_char()is provided. By default,SerialInterfaceTest::read_char()will callSerialInterface::read_char()(which callsread_some()), however, if theuse_parent_read_char_flag is set (callinguse_parent_read_char(false)), thenSerialInterfaceTest::read_char()will read the characters of the set message one at a time (simulating reading characters one by one from the serial port).
open()¶
openSuccess: Opens a not previously opened serial port with a valid port and baudrate. The return is expected to be
trueopenOpened: Attempts to open an already opened port. The return is expected to be
false.openWrongPort: Attempts to open a port on an invalid port. The return is expected to be
false.openWrongBaudrate: Attempts to set a non valid baudrate to the serial port. The return is expected to be
false.
is_open()¶
is_openOpened: Checks whether
SerialInterface::is_open()returnstruefor an open port.is_openClosed: Checks whether
SerialInterface::is_open()returnsfalsefor an closed port.
close()¶
closeSuccess: Closes an already opened port. The return is expected to be
true.closeClosed: Closes an already closed port. The return is expected to be
true.closeAsioError: Attempts to close an open port that Asio cannot close. The return is expected to be
false.
read_line()¶
read_lineSuccess: Checks that lines ending in
\nor\r\nare returned correctly. The return is expected to betrue. This test is performed on an opened serial port. Furthermore, the function should be called with an empty string, as well as with a non-empty one. Both cases should output just the read line without any characters that it had on callingSerialInterface::read_line().read_lineClosed: Checks that calling
SerialInterface::read_line()on a closed port returnsfalse.read_lineReadError: Simulates that asio::serial_port::read_some() returns an error and checks that in the case, the
SerialInterface::read_line()return isfalse. This test covers the case when asio::serial_port::close() is called while blocked on asio::serial_port::read_some(), since that breaks the block, making asio::serial_port::read_some() return with a not OK asio::error_code.