|
iodrivers_base
|
#include <Fixture.hpp>
Classes | |
| class | BoostMockContext |
| class | GTestMockContext |
Public Types | |
| typedef Driver | fixture_driver_t |
Public Member Functions | |
| Fixture () | |
| TestStream * | getStream () |
| std::vector< uint8_t > | readPacket () |
| void | writePacket (std::vector< uint8_t > const &data) |
| void | writePacket (uint8_t const *buffer, size_t size) |
| void | pushDataToDriver (std::vector< uint8_t > const &data) |
| template<typename Iterator > | |
| void | pushDataToDriver (Iterator begin, Iterator end) |
| std::vector< uint8_t > | readDataFromDriver () |
| unsigned int | getQueuedBytes () const |
| void | EXPECT_REPLY (std::vector< uint8_t > const &expectation, std::vector< uint8_t > const &reply) |
| void | validateExpectationsAreEmpty () |
| void | setMockMode (bool mode) |
| void | clearExpectations () |
Public Attributes | |
| std::vector< uint8_t > | packetBuffer |
| Driver | driver |
A fixture class designed to ease testing of iodrivers_base drivers in boost and GTest
It creates a given Driver class, which must be opened with the test:// URI. It then provides helper methods to provide access to the underlying TestStream.
readPacket/writePacket/pushDataToDevice/readDataFromDevice are then available within the test.
In boost-test:
BOOST_FIXTURE_TEST(MyTest, Fixture<MyDriver>) { MyDriver.openURI("test://"); uint8_t buffer[4] = { 0, 1, 2, 3 }; pushDataToDriver(buffer, buffer + 2); auto packet = readPacket(); // Check that the packet matches the expected extraction }
In GTest:
struct DriverTest : ::testing::Test, iodrivers_base::Fixture<MyDriver> { Fixture() { // Optional: open here // driver.openURI("test://") }
}
TEST_F(DriverTest, it_handles_an_invalid_packet) { MyDriver.openURI("test://"); uint8_t buffer[4] = { 0, 1, 2, 3 }; pushDataToDriver(buffer, buffer + 2); auto packet = readPacket(); // Check that the packet matches the expected extraction }
Mock Mode: To mock the behavior of the device in situations which a reply is expected, the mock mode should be set, using IODRIVERS_BASE_MOCK(). The expectations are set usint EXPECT_REPLY(expectation, reply) and multiple expecations can be set. The mock will check the expecations and reply in the order that they were defined and will raise an error if any of them is not met. Mock mode is available in both BOOST and GTest Frameworks.
IODRIVER_BASE_MOCK() uint8_t exp[] = { 0, 1, 2, 3 }; uint8_t rep[] = { 3, 2, 1, 0 }; EXPECT_REPLY(vector<uint8_t>(exp, exp + 4), vector<uint8_t>(rep, rep + 4)); writePacket(exp,4);
| typedef Driver iodrivers_base::Fixture< Driver >::fixture_driver_t |
|
inline |
|
inline |
|
inline |
|
inline |
Return the number of bytes currently queued in the driver's internal buffer
This is useful mainly when testing extractPacket
TEST_F(DriverTest, KeepsSingleBytes) { uint8_t data = 0x01; pushDataToDriver(&data, &data + 1); ASSERT_THROW(TimeoutError, readPacket()); ASSERT_EQUAL(1, getQueuedBytes()); }
|
inline |
Get the underlying TestStream
|
inline |
Push data to the driver "as-if" it was coming from the device
|
inline |
Helper method to allow passing any kind of uint8_t range
uint8_t packet[] = { 0, 1, 2, 3 }; pushDataToDriver(packet, packet + sizeof(packet));
|
inline |
Read data that the driver sent to the device
|
inline |
Read a packet from the driver and return it as an std::vector
|
inline |
|
inline |
Check if the test has any expectation set and throw if positive. It should be used to check if the test reached its end without any expecation left only
|
inline |
Write data to the driver
|
inline |
Write data to the driver
| Driver iodrivers_base::Fixture< Driver >::driver |
| std::vector<uint8_t> iodrivers_base::Fixture< Driver >::packetBuffer |
1.8.6