Skip to content

Commit

Permalink
test: add NotifyingVerifyingFunctionMock (#708)
Browse files Browse the repository at this point in the history
* test: add NotifyingVerifyingFunctionMock

* Rename VerifyingFunctionMock to VerifyingFunction, and NotifyingVerifyingFunctionMock to VerifyingInvoker

* Update infra/util/test_helper/MockCallback.hpp

Co-authored-by: fabiangottstein <[email protected]>

---------

Co-authored-by: Richard Peters <[email protected]>
Co-authored-by: fabiangottstein <[email protected]>
  • Loading branch information
3 people authored Oct 9, 2024
1 parent 5fb1b8e commit 798ec3d
Show file tree
Hide file tree
Showing 15 changed files with 68 additions and 40 deletions.
2 changes: 1 addition & 1 deletion hal/interfaces/test/TestSerialCommunication.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class BufferedSerialCommunicationOnUnbufferedTest

TEST_F(BufferedSerialCommunicationOnUnbufferedTest, SendData_is_forwarded)
{
infra::VerifyingFunctionMock<void()> onDone;
infra::VerifyingFunction<void()> onDone;
std::array<uint8_t, 4> data;

EXPECT_CALL(serial, SendData(infra::MakeConstByteRange(data), testing::_)).WillOnce(testing::InvokeArgument<1>());
Expand Down
20 changes: 10 additions & 10 deletions hal/interfaces/test_doubles/test/TestI2cRegisterAccessMock.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ class RegisterAccessMockTest

TEST_F(RegisterAccessMockTest, ReadRegisterInOneRead)
{
infra::VerifyingFunctionMock<void(hal::Result, uint32_t)> sendDataDoneVerifier(hal::Result::complete, 1);
infra::VerifyingFunctionMock<void(hal::Result)> receiveDataDoneVerifier(hal::Result::complete);
infra::VerifyingFunction<void(hal::Result, uint32_t)> sendDataDoneVerifier(hal::Result::complete, 1);
infra::VerifyingFunction<void(hal::Result)> receiveDataDoneVerifier(hal::Result::complete);
EXPECT_CALL(masterMock, ReadRegisterMock(3)).WillOnce(testing::Return(std::vector<uint8_t>{ 1, 2, 3, 4 }));

uint8_t dataRegister = 3;
Expand All @@ -32,8 +32,8 @@ TEST_F(RegisterAccessMockTest, ReadRegisterInOneRead)

TEST_F(RegisterAccessMockTest, ReadRegisterInTwoReads)
{
infra::VerifyingFunctionMock<void(hal::Result)> receiveDataDone1Verifier(hal::Result::complete);
infra::VerifyingFunctionMock<void(hal::Result)> receiveDataDone2Verifier(hal::Result::complete);
infra::VerifyingFunction<void(hal::Result)> receiveDataDone1Verifier(hal::Result::complete);
infra::VerifyingFunction<void(hal::Result)> receiveDataDone2Verifier(hal::Result::complete);
EXPECT_CALL(masterMock, ReadRegisterMock(3)).WillOnce(testing::Return(std::vector<uint8_t>{ 1, 2, 3, 4 }));

uint8_t dataRegister = 3;
Expand All @@ -49,7 +49,7 @@ TEST_F(RegisterAccessMockTest, ReadRegisterInTwoReads)

TEST_F(RegisterAccessMockTest, WriteRegisterInOneWrite)
{
infra::VerifyingFunctionMock<void(hal::Result, uint32_t)> sendDataDoneVerifier(hal::Result::complete, 5);
infra::VerifyingFunction<void(hal::Result, uint32_t)> sendDataDoneVerifier(hal::Result::complete, 5);
EXPECT_CALL(masterMock, WriteRegisterMock(3, std::vector<uint8_t>{ 1, 2, 3, 4 }));

std::array<uint8_t, 5> data = { 3, 1, 2, 3, 4 };
Expand All @@ -58,8 +58,8 @@ TEST_F(RegisterAccessMockTest, WriteRegisterInOneWrite)

TEST_F(RegisterAccessMockTest, WriteRegisterInTwoWrites)
{
infra::VerifyingFunctionMock<void(hal::Result, uint32_t)> sendDataDone1Verifier(hal::Result::complete, 1);
infra::VerifyingFunctionMock<void(hal::Result, uint32_t)> sendDataDone2Verifier(hal::Result::complete, 4);
infra::VerifyingFunction<void(hal::Result, uint32_t)> sendDataDone1Verifier(hal::Result::complete, 1);
infra::VerifyingFunction<void(hal::Result, uint32_t)> sendDataDone2Verifier(hal::Result::complete, 4);
EXPECT_CALL(masterMock, WriteRegisterMock(3, std::vector<uint8_t>{ 1, 2, 3, 4 }));

uint8_t dataRegister = 3;
Expand All @@ -70,9 +70,9 @@ TEST_F(RegisterAccessMockTest, WriteRegisterInTwoWrites)

TEST_F(RegisterAccessMockTest, WriteRegisterInThreeWrites)
{
infra::VerifyingFunctionMock<void(hal::Result, uint32_t)> sendDataDone1Verifier(hal::Result::complete, 1);
infra::VerifyingFunctionMock<void(hal::Result, uint32_t)> sendDataDone2Verifier(hal::Result::complete, 3);
infra::VerifyingFunctionMock<void(hal::Result, uint32_t)> sendDataDone3Verifier(hal::Result::complete, 1);
infra::VerifyingFunction<void(hal::Result, uint32_t)> sendDataDone1Verifier(hal::Result::complete, 1);
infra::VerifyingFunction<void(hal::Result, uint32_t)> sendDataDone2Verifier(hal::Result::complete, 3);
infra::VerifyingFunction<void(hal::Result, uint32_t)> sendDataDone3Verifier(hal::Result::complete, 1);
EXPECT_CALL(masterMock, WriteRegisterMock(3, std::vector<uint8_t>{ 1, 2, 3, 4 }));

uint8_t dataRegister = 3;
Expand Down
2 changes: 1 addition & 1 deletion infra/event/test/TestAtomicTriggerScheduler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class AtomicTriggerSchedulerTest

TEST_F(AtomicTriggerSchedulerTest, scheduled_once)
{
infra::VerifyingFunctionMock<void()> mock;
infra::VerifyingFunction<void()> mock;
infra::AtomicTriggerScheduler scheduler;

scheduler.Schedule([&]()
Expand Down
2 changes: 1 addition & 1 deletion infra/timer/test/TestTickOnInterruptTimerService.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class BaseTimerServiceTest

TEST_F(BaseTimerServiceTest, set_resolution)
{
infra::VerifyingFunctionMock<void()> verify;
infra::VerifyingFunction<void()> verify;
infra::TimerSingleShot timer(std::chrono::seconds(1), verify);

timerService.SetResolution(std::chrono::seconds(2));
Expand Down
8 changes: 4 additions & 4 deletions infra/util/test/TestMultiFunction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ TEST(MultiFunctionTest, construct_empty_with_nullptr)

TEST(MultiFunctionTest, construct_with_function)
{
infra::VerifyingFunctionMock<void()> m;
infra::VerifyingFunction<void()> m;

infra::MultiFunction<void()> f([&]()
{
Expand All @@ -33,7 +33,7 @@ TEST(MultiFunctionTest, construct_with_function)

TEST(MultiFunctionTest, multi_function_construct_with_first)
{
infra::VerifyingFunctionMock<void()> m;
infra::VerifyingFunction<void()> m;

infra::MultiFunction<void()>::And<void(bool)>::And<void(int)> f([&]()
{
Expand All @@ -44,7 +44,7 @@ TEST(MultiFunctionTest, multi_function_construct_with_first)

TEST(MultiFunctionTest, multi_function_construct_with_second)
{
infra::VerifyingFunctionMock<void(bool)> m(true);
infra::VerifyingFunction<void(bool)> m(true);

infra::MultiFunction<void()>::And<void(bool)> f([&](bool v)
{
Expand All @@ -55,7 +55,7 @@ TEST(MultiFunctionTest, multi_function_construct_with_second)

TEST(MultiFunctionTest, reassign_multi_function)
{
infra::VerifyingFunctionMock<void(bool)> m(true);
infra::VerifyingFunction<void(bool)> m(true);

infra::MultiFunction<void()>::And<void(bool)> f([]() {});
f = [&](bool v)
Expand Down
46 changes: 37 additions & 9 deletions infra/util/test_helper/MockCallback.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,14 @@ namespace infra
};

template<class T, std::size_t ExtraSize = INFRA_DEFAULT_FUNCTION_EXTRA_SIZE>
class VerifyingFunctionMock;
class VerifyingFunction;

template<class T, std::size_t ExtraSize>
class VerifyingFunctionMock<T(), ExtraSize>
class VerifyingFunction<T(), ExtraSize>
: public MockCallback<T()>
{
public:
VerifyingFunctionMock()
VerifyingFunction()
{
EXPECT_CALL(*this, callback());
}
Expand All @@ -68,11 +68,11 @@ namespace infra
};

template<class T, class P1, std::size_t ExtraSize>
class VerifyingFunctionMock<T(P1), ExtraSize>
class VerifyingFunction<T(P1), ExtraSize>
: public MockCallback<T(P1)>
{
public:
VerifyingFunctionMock(P1 p1)
VerifyingFunction(P1 p1)
{
EXPECT_CALL(*this, callback(p1));
}
Expand All @@ -87,11 +87,11 @@ namespace infra
};

template<class T, class P1, class P2, std::size_t ExtraSize>
class VerifyingFunctionMock<T(P1, P2), ExtraSize>
class VerifyingFunction<T(P1, P2), ExtraSize>
: public MockCallback<T(P1, P2)>
{
public:
VerifyingFunctionMock(P1 p1, P2 p2)
VerifyingFunction(P1 p1, P2 p2)
{
EXPECT_CALL(*this, callback(p1, p2));
}
Expand All @@ -106,11 +106,11 @@ namespace infra
};

template<class T, class P1, class P2, class P3, std::size_t ExtraSize>
class VerifyingFunctionMock<T(P1, P2, P3), ExtraSize>
class VerifyingFunction<T(P1, P2, P3), ExtraSize>
: public MockCallback<T(P1, P2, P3)>
{
public:
VerifyingFunctionMock(P1 p1, P2 p2, P3 p3)
VerifyingFunction(P1 p1, P2 p2, P3 p3)
{
EXPECT_CALL(*this, callback(p1, p2, p3));
}
Expand All @@ -124,6 +124,34 @@ namespace infra
}
};

template<class Result>
class VerifyingInvoker;

template<class Result, class... Args>
class VerifyingInvoker<Result(Args...)>
{
public:
explicit VerifyingInvoker(infra::Function<Result(Args...)> onCalled)
: onCalled(onCalled)
{
EXPECT_CALL(*this, callback());
}

MOCK_METHOD(Result, callback, (), (const override));

operator infra::Function<Result(Args...)>()
{
return [this](Args&&... args)
{
onCalled(args...);
return callback();
};
}

private:
infra::Function<Result(Args...)> onCalled;
};

template<class R, std::size_t ExtraSize = INFRA_DEFAULT_FUNCTION_EXTRA_SIZE>
auto MockFunction = []
{
Expand Down
4 changes: 2 additions & 2 deletions services/network/test/TestHttpClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -870,7 +870,7 @@ TEST_F(HttpClientTest, Stop_while_closed)
EXPECT_CALL(client, Detaching());
client.Subject().CloseConnection();

infra::VerifyingFunctionMock<void()> onDone;
infra::VerifyingFunction<void()> onDone;
connector.Stop([&]()
{
onDone.callback();
Expand All @@ -881,7 +881,7 @@ TEST_F(HttpClientTest, Stop_while_connection_open)
{
Connect();

infra::VerifyingFunctionMock<void()> onDone;
infra::VerifyingFunction<void()> onDone;
EXPECT_CALL(connection, CloseAndDestroyMock());
EXPECT_CALL(client, Detaching());
connector.Stop([&]()
Expand Down
4 changes: 2 additions & 2 deletions services/network/test/TestHttpClientCachedConnection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -430,7 +430,7 @@ TEST_F(HttpClientCachedConnectionTest, Stop_while_closed)
CreateConnection(factory);
CloseConnection();

infra::VerifyingFunctionMock<void()> onDone;
infra::VerifyingFunction<void()> onDone;
connector.Stop([&]()
{
onDone.callback();
Expand All @@ -441,7 +441,7 @@ TEST_F(HttpClientCachedConnectionTest, Stop_while_connection_open)
{
CreateConnection(factory);

infra::VerifyingFunctionMock<void()> onDone;
infra::VerifyingFunction<void()> onDone;
EXPECT_CALL(clientSubject, CloseConnection()).WillOnce([this]()
{
clientSubject.Detach();
Expand Down
4 changes: 2 additions & 2 deletions services/util/test/TestConfigurationStore.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ TEST_F(ConfigurationBlobTest, Write_writes_to_flash)

infra::ConstByteRange writeBuffer;
EXPECT_CALL(flash, WriteBuffer(testing::_, 0, testing::_)).WillOnce(testing::DoAll(testing::SaveArg<0>(&writeBuffer), testing::SaveArg<2>(&onWriteDone)));
infra::VerifyingFunctionMock<void()> writeDone;
infra::VerifyingFunction<void()> writeDone;
configurationBlob.Write(8, writeDone);

std::array<uint8_t, 20> blobData = { 0x21, 0xf1, 0xfb, 0x20, 0xaf, 0x91, 0x92, 0x86, 8, 0, 0, 0, 1, 2, 3, 4, 5, 6, 7, 0 };
Expand Down Expand Up @@ -190,7 +190,7 @@ TEST_F(ConfigurationBlobTest, Write_writes_to_flash)
TEST_F(ConfigurationBlobTest, Erase_erases_flash)
{
EXPECT_CALL(flash, EraseSectors(0, 1, testing::_)).WillOnce(testing::SaveArg<2>(&onEraseDone));
infra::VerifyingFunctionMock<void()> eraseDone;
infra::VerifyingFunction<void()> eraseDone;
configurationBlob.Erase(eraseDone);

onEraseDone();
Expand Down
2 changes: 1 addition & 1 deletion services/util/test/TestCyclicStore.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ class CyclicStoreTest

TEST_F(CyclicStoreTest, AddFirstItem)
{
infra::VerifyingFunctionMock<void()> done;
infra::VerifyingFunction<void()> done;

AddItem(KeepBytesAlive({ 11, 12 }), [&done]()
{
Expand Down
2 changes: 1 addition & 1 deletion services/util/test/TestFlashQuadSpiCypressFll.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class FlashQuadSpiCypressFllTest
}

testing::StrictMock<hal::QuadSpiStub> spiStub;
infra::VerifyingFunctionMock<void()> onInitialized;
infra::VerifyingFunction<void()> onInitialized;
services::FlashQuadSpiCypressFll flash;

testing::StrictMock<infra::MockCallback<void()>> finished;
Expand Down
2 changes: 1 addition & 1 deletion services/util/test/TestFlashQuadSpiMicronN25q.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class FlashQuadSpiMicronN25qTest
}

testing::StrictMock<hal::QuadSpiStub> spiStub;
infra::VerifyingFunctionMock<void()> onInitialized;
infra::VerifyingFunction<void()> onInitialized;
services::FlashQuadSpiMicronN25q flash;

testing::StrictMock<infra::MockCallback<void()>> finished;
Expand Down
2 changes: 1 addition & 1 deletion services/util/test/TestFlashQuadSpiSingleSpeed.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class FlashQuadSpiSingleSpeedTest
}

testing::StrictMock<hal::QuadSpiStub> spiStub;
infra::VerifyingFunctionMock<void()> onInitialized;
infra::VerifyingFunction<void()> onInitialized;
services::FlashQuadSpiSingleSpeed flash;

testing::StrictMock<infra::MockCallback<void()>> finished;
Expand Down
4 changes: 2 additions & 2 deletions services/util/test/TestSerialCommunicationLoopback.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class SerialCommunicationLoopbackTest
TEST_F(SerialCommunicationLoopbackTest, SendFromServerReceiveByClient)
{
infra::ConstByteRange data = infra::MakeStringByteRange("hello");
infra::VerifyingFunctionMock<void(infra::ConstByteRange)> clientReceiveCallback{ data };
infra::VerifyingFunction<void(infra::ConstByteRange)> clientReceiveCallback{ data };

client.ReceiveData(clientReceiveCallback);
server.SendData(data, infra::emptyFunction);
Expand All @@ -31,7 +31,7 @@ TEST_F(SerialCommunicationLoopbackTest, SendFromServerReceiveByClient)
TEST_F(SerialCommunicationLoopbackTest, SendFromClientReceiveByServer)
{
infra::ConstByteRange data = infra::MakeStringByteRange("world");
infra::VerifyingFunctionMock<void(infra::ConstByteRange)> serverReceiveCallback{ data };
infra::VerifyingFunction<void(infra::ConstByteRange)> serverReceiveCallback{ data };

server.ReceiveData(serverReceiveCallback);
client.SendData(data, infra::emptyFunction);
Expand Down
4 changes: 2 additions & 2 deletions services/util/test/TestWritableConfiguration.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ TEST_F(FlashReadingWritableConfigurationTest, write_data_in_empty_flash)
formatter.PutVarInt(15);
}));

infra::VerifyingFunctionMock<void()> callback;
infra::VerifyingFunction<void()> callback;
configuration->Write(newValue, [&callback]()
{
callback.callback();
Expand Down Expand Up @@ -140,7 +140,7 @@ TEST_F(MemoryMappedWritableConfigurationTest, write_data_in_empty_flash)
formatter.PutVarInt(15);
}));

infra::VerifyingFunctionMock<void()> callback;
infra::VerifyingFunction<void()> callback;
configuration->Write(newValue, [&callback]()
{
callback.callback();
Expand Down

0 comments on commit 798ec3d

Please sign in to comment.