sangaboard.testing module¶
Classes that enable unit testing by mocking the serial responses.
- class sangaboard.testing.DummySerialDevice[source]¶
Bases:
objectThis class exists to allow unit testing without serial port hardware.
It can be passed to an
ExtensibleSerialInstrumentin place of the serial port name, and will emulate a device using stored responses.It works on a line-by-line basis, so it will only do anything once it gets a termination character (by default, newline). It tries to give the same interface as
pyserial.Serialat least as far as the methods used byExtensibleSerialInstrumentgo.The
timeoutproperty will be used to cause delays if there is no response to give - but infinite timeouts raise an exception rather than attempt to do anything clever with threads.You can set
print_buffersto True in order to make it print the input and output buffers each time the “port” is written to or read from.- termination_character = '\n'¶
- responses = ()¶
- unmatched_response = 'error!\n'¶
- timeout = 0¶
- print_buffers = False¶
- read_buffer = b''¶
- write_buffer = b''¶
- write(bytes_buffer)[source]¶
Accept data being written to the port
This is data being sent out of the serial port, and is stored in the
write buffer.
- inWaiting()[source]¶
Return the number of bytes in the input buffer
Note the input buffer is for bytes being read from the port
- flushInput()[source]¶
Flush the input buffer, so there are no previously-arrived bytes when we read a response.
- register_response(regular_expression, response_function)[source]¶
Match a query and respond to it with a function.
See
respond_tofor details of how this works.
- remove_response(regex_to_remove)[source]¶
Remove a previously registered response
The regular expression string must be exactly the same as the one you are trying to remove.
- replace_response(regex, response_function)[source]¶
Remove a response and replace it with another.
Order is not preserved.
- respond_to(message)[source]¶
Do something in response to a message.
Each time we receive a termination character, we attempt to match the message (which doesn’t include the termination character) with the regular expressions in the
responsesproperty. The first one that matches is used.The regular expression match object’s
groups()value is passed to the function, which can then use it to return its response.
- class sangaboard.testing.DummySangaboardDevice(firmware_version='Sangaboard Firmware v0.5.1')[source]¶
Bases:
DummySerialDeviceCreate a dummy port, with the minimal responses to work
When the Sangaboard class is initialised, it checks the board, the version, and the optional modules. All of those commands must therefore return reasonable values.
- blocking_moves = False¶