iodrivers_base
Classes | Public Types | Public Member Functions | Public Attributes | List of all members
iodrivers_base::Fixture< Driver > Struct Template Reference

#include <Fixture.hpp>

Classes

class  BoostMockContext
 
class  GTestMockContext
 

Public Types

typedef Driver fixture_driver_t
 

Public Member Functions

 Fixture ()
 
TestStreamgetStream ()
 
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
 

Detailed Description

template<typename Driver>
struct iodrivers_base::Fixture< 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);

Member Typedef Documentation

◆ fixture_driver_t

template<typename Driver >
typedef Driver iodrivers_base::Fixture< Driver >::fixture_driver_t

Constructor & Destructor Documentation

◆ Fixture()

template<typename Driver >
iodrivers_base::Fixture< Driver >::Fixture ( )
inline

Member Function Documentation

◆ clearExpectations()

template<typename Driver >
void iodrivers_base::Fixture< Driver >::clearExpectations ( )
inline

◆ EXPECT_REPLY()

template<typename Driver >
void iodrivers_base::Fixture< Driver >::EXPECT_REPLY ( std::vector< uint8_t > const &  expectation,
std::vector< uint8_t > const &  reply 
)
inline

◆ getQueuedBytes()

template<typename Driver >
unsigned int iodrivers_base::Fixture< Driver >::getQueuedBytes ( ) const
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()); }

◆ getStream()

template<typename Driver >
TestStream* iodrivers_base::Fixture< Driver >::getStream ( )
inline

Get the underlying TestStream

◆ pushDataToDriver() [1/2]

template<typename Driver >
void iodrivers_base::Fixture< Driver >::pushDataToDriver ( std::vector< uint8_t > const &  data)
inline

Push data to the driver "as-if" it was coming from the device

◆ pushDataToDriver() [2/2]

template<typename Driver >
template<typename Iterator >
void iodrivers_base::Fixture< Driver >::pushDataToDriver ( Iterator  begin,
Iterator  end 
)
inline

Helper method to allow passing any kind of uint8_t range

uint8_t packet[] = { 0, 1, 2, 3 }; pushDataToDriver(packet, packet + sizeof(packet));

◆ readDataFromDriver()

template<typename Driver >
std::vector<uint8_t> iodrivers_base::Fixture< Driver >::readDataFromDriver ( )
inline

Read data that the driver sent to the device

◆ readPacket()

template<typename Driver >
std::vector<uint8_t> iodrivers_base::Fixture< Driver >::readPacket ( )
inline

Read a packet from the driver and return it as an std::vector

◆ setMockMode()

template<typename Driver >
void iodrivers_base::Fixture< Driver >::setMockMode ( bool  mode)
inline

◆ validateExpectationsAreEmpty()

template<typename Driver >
void iodrivers_base::Fixture< Driver >::validateExpectationsAreEmpty ( )
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

◆ writePacket() [1/2]

template<typename Driver >
void iodrivers_base::Fixture< Driver >::writePacket ( std::vector< uint8_t > const &  data)
inline

Write data to the driver

◆ writePacket() [2/2]

template<typename Driver >
void iodrivers_base::Fixture< Driver >::writePacket ( uint8_t const *  buffer,
size_t  size 
)
inline

Write data to the driver

Member Data Documentation

◆ driver

template<typename Driver >
Driver iodrivers_base::Fixture< Driver >::driver

◆ packetBuffer

template<typename Driver >
std::vector<uint8_t> iodrivers_base::Fixture< Driver >::packetBuffer

The documentation for this struct was generated from the following file: