Pigweed
 
Loading...
Searching...
No Matches
pw::i2c::MockInitiator Class Reference

#include <initiator_mock.h>

Inheritance diagram for pw::i2c::MockInitiator:
pw::i2c::Initiator

Public Member Functions

constexpr MockInitiator (span< Transaction > transaction_list)
 
Status Finalize () const
 
 ~MockInitiator () override
 
- Public Member Functions inherited from pw::i2c::Initiator
constexpr Initiator ()
 
constexpr Initiator (Feature supported_features)
 
Status WriteReadFor (Address device_address, ConstByteSpan tx_buffer, ByteSpan rx_buffer, chrono::SystemClock::duration timeout)
 
Status TransferFor (span< const Message > messages, chrono::SystemClock::duration timeout)
 
Status WriteReadFor (Address device_address, const void *tx_buffer, size_t tx_size_bytes, void *rx_buffer, size_t rx_size_bytes, chrono::SystemClock::duration timeout)
 
Status WriteFor (Address device_address, ConstByteSpan tx_buffer, chrono::SystemClock::duration timeout)
 
Status WriteFor (Address device_address, const void *tx_buffer, size_t tx_size_bytes, chrono::SystemClock::duration timeout)
 
Status ReadFor (Address device_address, ByteSpan rx_buffer, chrono::SystemClock::duration timeout)
 
Status ReadFor (Address device_address, void *rx_buffer, size_t rx_size_bytes, chrono::SystemClock::duration timeout)
 
Status ProbeDeviceFor (Address device_address, chrono::SystemClock::duration timeout)
 

Additional Inherited Members

- Public Types inherited from pw::i2c::Initiator
enum class  Feature : int { kStandard = 0 , kTenBit = (1 << 0) , kNoStart = (1 << 1) }
 Defined set of supported i2c features.
 

Detailed Description

A generic mocked backend for pw::i2c::Initiator that's specifically designed to make it easier to develop I2C device drivers. pw::i2c::MockInitiator compares actual I2C transactions against expected transactions. The expected transactions are represented as a list of pw::i2c::Transaction instances that are passed as arguments in the pw::i2c::MockInitiator constructor. Each consecutive call to pw::i2c::MockInitiator iterates to the next expected transaction. pw::i2c::MockInitiator::Finalize() indicates whether the actual transactions matched the expected transactions.

pw::i2c::MockInitiator is intended to be used within GoogleTest tests. See

embed:rst:inline :ref:`module-pw_unit_test` 

.

#include <chrono>
#include "pw_bytes/array.h"
#include "pw_i2c/address.h"
#include "pw_i2c/initiator_mock.h"
#include "pw_result/result.h"
#include "pw_unit_test/framework.h"
using namespace std::chrono_literals;
namespace {
TEST(I2CTestSuite, I2CWriteTestCase) {
constexpr pw::i2c::Address kAddress =
pw::i2c::Address::SevenBit<0x01>(); constexpr auto kExpectedWrite =
pw::bytes::Array<1, 2, 3>(); auto expected_transactions =
pw::i2c::MakeExpectedTransactionArray(
{pw::i2c::WriteTransaction(pw::OkStatus(), kAddress, kExpectedWrite,
1ms)}
);
pw::i2c::MockInitiator initiator(expected_transactions);
pw::ConstByteSpan kActualWrite = pw::bytes::Array<1, 2, 3>();
pw::Status status = initiator.WriteFor(kAddress, kActualWrite, 1ms);
EXPECT_EQ(initiator.Finalize(), pw::OkStatus());
}
}
Definition: status.h:85
Definition: address.h:39
Definition: initiator_mock.h:172
constexpr Status OkStatus()
Definition: status.h:234

Constructor & Destructor Documentation

◆ ~MockInitiator()

pw::i2c::MockInitiator::~MockInitiator ( )
override

Runs pw::i2c::MockInitiator::Finalize() regardless of whether it was already optionally finalized.

Member Function Documentation

◆ DoWriteReadFor()

Status pw::i2c::MockInitiator::DoWriteReadFor ( Address  device_address,
ConstByteSpan  tx_buffer,
ByteSpan  rx_buffer,
chrono::SystemClock::duration  timeout 
)
overrideprivatevirtual

Reimplemented from pw::i2c::Initiator.

◆ Finalize()

Status pw::i2c::MockInitiator::Finalize ( ) const
inline

Indicates whether the actual I2C transactions matched the expected transactions. Should be called at the end of the test.

Returns
embed:rst:leading-asterisk
 
* 
*  .. pw-status-codes::
* 
*     OK: The actual transactions matched the expected transactions.
* 
*     OUT_OF_RANGE: The mocked set of transactions hasn't been exhausted.
* 
*  

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