From 798ec3da653f955a1eb218279fa93f52f15fcfbf Mon Sep 17 00:00:00 2001 From: EkelmansPh <58972933+EkelmansPh@users.noreply.github.com> Date: Wed, 9 Oct 2024 11:35:29 +0200 Subject: [PATCH] test: add NotifyingVerifyingFunctionMock (#708) * test: add NotifyingVerifyingFunctionMock * Rename VerifyingFunctionMock to VerifyingFunction, and NotifyingVerifyingFunctionMock to VerifyingInvoker * Update infra/util/test_helper/MockCallback.hpp Co-authored-by: fabiangottstein <75617853+fabiangottstein@users.noreply.github.com> --------- Co-authored-by: Richard Peters Co-authored-by: fabiangottstein <75617853+fabiangottstein@users.noreply.github.com> --- .../test/TestSerialCommunication.cpp | 2 +- .../test/TestI2cRegisterAccessMock.cpp | 20 ++++---- .../event/test/TestAtomicTriggerScheduler.cpp | 2 +- .../test/TestTickOnInterruptTimerService.cpp | 2 +- infra/util/test/TestMultiFunction.cpp | 8 ++-- infra/util/test_helper/MockCallback.hpp | 46 +++++++++++++++---- services/network/test/TestHttpClient.cpp | 4 +- .../test/TestHttpClientCachedConnection.cpp | 4 +- services/util/test/TestConfigurationStore.cpp | 4 +- services/util/test/TestCyclicStore.cpp | 2 +- .../util/test/TestFlashQuadSpiCypressFll.cpp | 2 +- .../util/test/TestFlashQuadSpiMicronN25q.cpp | 2 +- .../util/test/TestFlashQuadSpiSingleSpeed.cpp | 2 +- .../test/TestSerialCommunicationLoopback.cpp | 4 +- .../util/test/TestWritableConfiguration.cpp | 4 +- 15 files changed, 68 insertions(+), 40 deletions(-) diff --git a/hal/interfaces/test/TestSerialCommunication.cpp b/hal/interfaces/test/TestSerialCommunication.cpp index c23f42ab1..5c6b0b54d 100644 --- a/hal/interfaces/test/TestSerialCommunication.cpp +++ b/hal/interfaces/test/TestSerialCommunication.cpp @@ -31,7 +31,7 @@ class BufferedSerialCommunicationOnUnbufferedTest TEST_F(BufferedSerialCommunicationOnUnbufferedTest, SendData_is_forwarded) { - infra::VerifyingFunctionMock onDone; + infra::VerifyingFunction onDone; std::array data; EXPECT_CALL(serial, SendData(infra::MakeConstByteRange(data), testing::_)).WillOnce(testing::InvokeArgument<1>()); diff --git a/hal/interfaces/test_doubles/test/TestI2cRegisterAccessMock.cpp b/hal/interfaces/test_doubles/test/TestI2cRegisterAccessMock.cpp index 4a1d5537d..1a7e9540f 100644 --- a/hal/interfaces/test_doubles/test/TestI2cRegisterAccessMock.cpp +++ b/hal/interfaces/test_doubles/test/TestI2cRegisterAccessMock.cpp @@ -18,8 +18,8 @@ class RegisterAccessMockTest TEST_F(RegisterAccessMockTest, ReadRegisterInOneRead) { - infra::VerifyingFunctionMock sendDataDoneVerifier(hal::Result::complete, 1); - infra::VerifyingFunctionMock receiveDataDoneVerifier(hal::Result::complete); + infra::VerifyingFunction sendDataDoneVerifier(hal::Result::complete, 1); + infra::VerifyingFunction receiveDataDoneVerifier(hal::Result::complete); EXPECT_CALL(masterMock, ReadRegisterMock(3)).WillOnce(testing::Return(std::vector{ 1, 2, 3, 4 })); uint8_t dataRegister = 3; @@ -32,8 +32,8 @@ TEST_F(RegisterAccessMockTest, ReadRegisterInOneRead) TEST_F(RegisterAccessMockTest, ReadRegisterInTwoReads) { - infra::VerifyingFunctionMock receiveDataDone1Verifier(hal::Result::complete); - infra::VerifyingFunctionMock receiveDataDone2Verifier(hal::Result::complete); + infra::VerifyingFunction receiveDataDone1Verifier(hal::Result::complete); + infra::VerifyingFunction receiveDataDone2Verifier(hal::Result::complete); EXPECT_CALL(masterMock, ReadRegisterMock(3)).WillOnce(testing::Return(std::vector{ 1, 2, 3, 4 })); uint8_t dataRegister = 3; @@ -49,7 +49,7 @@ TEST_F(RegisterAccessMockTest, ReadRegisterInTwoReads) TEST_F(RegisterAccessMockTest, WriteRegisterInOneWrite) { - infra::VerifyingFunctionMock sendDataDoneVerifier(hal::Result::complete, 5); + infra::VerifyingFunction sendDataDoneVerifier(hal::Result::complete, 5); EXPECT_CALL(masterMock, WriteRegisterMock(3, std::vector{ 1, 2, 3, 4 })); std::array data = { 3, 1, 2, 3, 4 }; @@ -58,8 +58,8 @@ TEST_F(RegisterAccessMockTest, WriteRegisterInOneWrite) TEST_F(RegisterAccessMockTest, WriteRegisterInTwoWrites) { - infra::VerifyingFunctionMock sendDataDone1Verifier(hal::Result::complete, 1); - infra::VerifyingFunctionMock sendDataDone2Verifier(hal::Result::complete, 4); + infra::VerifyingFunction sendDataDone1Verifier(hal::Result::complete, 1); + infra::VerifyingFunction sendDataDone2Verifier(hal::Result::complete, 4); EXPECT_CALL(masterMock, WriteRegisterMock(3, std::vector{ 1, 2, 3, 4 })); uint8_t dataRegister = 3; @@ -70,9 +70,9 @@ TEST_F(RegisterAccessMockTest, WriteRegisterInTwoWrites) TEST_F(RegisterAccessMockTest, WriteRegisterInThreeWrites) { - infra::VerifyingFunctionMock sendDataDone1Verifier(hal::Result::complete, 1); - infra::VerifyingFunctionMock sendDataDone2Verifier(hal::Result::complete, 3); - infra::VerifyingFunctionMock sendDataDone3Verifier(hal::Result::complete, 1); + infra::VerifyingFunction sendDataDone1Verifier(hal::Result::complete, 1); + infra::VerifyingFunction sendDataDone2Verifier(hal::Result::complete, 3); + infra::VerifyingFunction sendDataDone3Verifier(hal::Result::complete, 1); EXPECT_CALL(masterMock, WriteRegisterMock(3, std::vector{ 1, 2, 3, 4 })); uint8_t dataRegister = 3; diff --git a/infra/event/test/TestAtomicTriggerScheduler.cpp b/infra/event/test/TestAtomicTriggerScheduler.cpp index 96c499068..f000a5c56 100644 --- a/infra/event/test/TestAtomicTriggerScheduler.cpp +++ b/infra/event/test/TestAtomicTriggerScheduler.cpp @@ -10,7 +10,7 @@ class AtomicTriggerSchedulerTest TEST_F(AtomicTriggerSchedulerTest, scheduled_once) { - infra::VerifyingFunctionMock mock; + infra::VerifyingFunction mock; infra::AtomicTriggerScheduler scheduler; scheduler.Schedule([&]() diff --git a/infra/timer/test/TestTickOnInterruptTimerService.cpp b/infra/timer/test/TestTickOnInterruptTimerService.cpp index 44f25a383..4baedfb08 100644 --- a/infra/timer/test/TestTickOnInterruptTimerService.cpp +++ b/infra/timer/test/TestTickOnInterruptTimerService.cpp @@ -19,7 +19,7 @@ class BaseTimerServiceTest TEST_F(BaseTimerServiceTest, set_resolution) { - infra::VerifyingFunctionMock verify; + infra::VerifyingFunction verify; infra::TimerSingleShot timer(std::chrono::seconds(1), verify); timerService.SetResolution(std::chrono::seconds(2)); diff --git a/infra/util/test/TestMultiFunction.cpp b/infra/util/test/TestMultiFunction.cpp index 6076ef4a3..25b98c7a4 100644 --- a/infra/util/test/TestMultiFunction.cpp +++ b/infra/util/test/TestMultiFunction.cpp @@ -22,7 +22,7 @@ TEST(MultiFunctionTest, construct_empty_with_nullptr) TEST(MultiFunctionTest, construct_with_function) { - infra::VerifyingFunctionMock m; + infra::VerifyingFunction m; infra::MultiFunction f([&]() { @@ -33,7 +33,7 @@ TEST(MultiFunctionTest, construct_with_function) TEST(MultiFunctionTest, multi_function_construct_with_first) { - infra::VerifyingFunctionMock m; + infra::VerifyingFunction m; infra::MultiFunction::And::And f([&]() { @@ -44,7 +44,7 @@ TEST(MultiFunctionTest, multi_function_construct_with_first) TEST(MultiFunctionTest, multi_function_construct_with_second) { - infra::VerifyingFunctionMock m(true); + infra::VerifyingFunction m(true); infra::MultiFunction::And f([&](bool v) { @@ -55,7 +55,7 @@ TEST(MultiFunctionTest, multi_function_construct_with_second) TEST(MultiFunctionTest, reassign_multi_function) { - infra::VerifyingFunctionMock m(true); + infra::VerifyingFunction m(true); infra::MultiFunction::And f([]() {}); f = [&](bool v) diff --git a/infra/util/test_helper/MockCallback.hpp b/infra/util/test_helper/MockCallback.hpp index 54ff1edd4..4b511148c 100644 --- a/infra/util/test_helper/MockCallback.hpp +++ b/infra/util/test_helper/MockCallback.hpp @@ -46,14 +46,14 @@ namespace infra }; template - class VerifyingFunctionMock; + class VerifyingFunction; template - class VerifyingFunctionMock + class VerifyingFunction : public MockCallback { public: - VerifyingFunctionMock() + VerifyingFunction() { EXPECT_CALL(*this, callback()); } @@ -68,11 +68,11 @@ namespace infra }; template - class VerifyingFunctionMock + class VerifyingFunction : public MockCallback { public: - VerifyingFunctionMock(P1 p1) + VerifyingFunction(P1 p1) { EXPECT_CALL(*this, callback(p1)); } @@ -87,11 +87,11 @@ namespace infra }; template - class VerifyingFunctionMock + class VerifyingFunction : public MockCallback { public: - VerifyingFunctionMock(P1 p1, P2 p2) + VerifyingFunction(P1 p1, P2 p2) { EXPECT_CALL(*this, callback(p1, p2)); } @@ -106,11 +106,11 @@ namespace infra }; template - class VerifyingFunctionMock + class VerifyingFunction : public MockCallback { public: - VerifyingFunctionMock(P1 p1, P2 p2, P3 p3) + VerifyingFunction(P1 p1, P2 p2, P3 p3) { EXPECT_CALL(*this, callback(p1, p2, p3)); } @@ -124,6 +124,34 @@ namespace infra } }; + template + class VerifyingInvoker; + + template + class VerifyingInvoker + { + public: + explicit VerifyingInvoker(infra::Function onCalled) + : onCalled(onCalled) + { + EXPECT_CALL(*this, callback()); + } + + MOCK_METHOD(Result, callback, (), (const override)); + + operator infra::Function() + { + return [this](Args&&... args) + { + onCalled(args...); + return callback(); + }; + } + + private: + infra::Function onCalled; + }; + template auto MockFunction = [] { diff --git a/services/network/test/TestHttpClient.cpp b/services/network/test/TestHttpClient.cpp index 466d047cf..fb16debdd 100644 --- a/services/network/test/TestHttpClient.cpp +++ b/services/network/test/TestHttpClient.cpp @@ -870,7 +870,7 @@ TEST_F(HttpClientTest, Stop_while_closed) EXPECT_CALL(client, Detaching()); client.Subject().CloseConnection(); - infra::VerifyingFunctionMock onDone; + infra::VerifyingFunction onDone; connector.Stop([&]() { onDone.callback(); @@ -881,7 +881,7 @@ TEST_F(HttpClientTest, Stop_while_connection_open) { Connect(); - infra::VerifyingFunctionMock onDone; + infra::VerifyingFunction onDone; EXPECT_CALL(connection, CloseAndDestroyMock()); EXPECT_CALL(client, Detaching()); connector.Stop([&]() diff --git a/services/network/test/TestHttpClientCachedConnection.cpp b/services/network/test/TestHttpClientCachedConnection.cpp index abd4d0a1d..98b2ae46c 100644 --- a/services/network/test/TestHttpClientCachedConnection.cpp +++ b/services/network/test/TestHttpClientCachedConnection.cpp @@ -430,7 +430,7 @@ TEST_F(HttpClientCachedConnectionTest, Stop_while_closed) CreateConnection(factory); CloseConnection(); - infra::VerifyingFunctionMock onDone; + infra::VerifyingFunction onDone; connector.Stop([&]() { onDone.callback(); @@ -441,7 +441,7 @@ TEST_F(HttpClientCachedConnectionTest, Stop_while_connection_open) { CreateConnection(factory); - infra::VerifyingFunctionMock onDone; + infra::VerifyingFunction onDone; EXPECT_CALL(clientSubject, CloseConnection()).WillOnce([this]() { clientSubject.Detach(); diff --git a/services/util/test/TestConfigurationStore.cpp b/services/util/test/TestConfigurationStore.cpp index 21ecebb69..b4b9ed592 100644 --- a/services/util/test/TestConfigurationStore.cpp +++ b/services/util/test/TestConfigurationStore.cpp @@ -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 writeDone; + infra::VerifyingFunction writeDone; configurationBlob.Write(8, writeDone); std::array blobData = { 0x21, 0xf1, 0xfb, 0x20, 0xaf, 0x91, 0x92, 0x86, 8, 0, 0, 0, 1, 2, 3, 4, 5, 6, 7, 0 }; @@ -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 eraseDone; + infra::VerifyingFunction eraseDone; configurationBlob.Erase(eraseDone); onEraseDone(); diff --git a/services/util/test/TestCyclicStore.cpp b/services/util/test/TestCyclicStore.cpp index 1c53a2b72..24c387804 100644 --- a/services/util/test/TestCyclicStore.cpp +++ b/services/util/test/TestCyclicStore.cpp @@ -55,7 +55,7 @@ class CyclicStoreTest TEST_F(CyclicStoreTest, AddFirstItem) { - infra::VerifyingFunctionMock done; + infra::VerifyingFunction done; AddItem(KeepBytesAlive({ 11, 12 }), [&done]() { diff --git a/services/util/test/TestFlashQuadSpiCypressFll.cpp b/services/util/test/TestFlashQuadSpiCypressFll.cpp index 888f1f5a1..4be43987b 100644 --- a/services/util/test/TestFlashQuadSpiCypressFll.cpp +++ b/services/util/test/TestFlashQuadSpiCypressFll.cpp @@ -21,7 +21,7 @@ class FlashQuadSpiCypressFllTest } testing::StrictMock spiStub; - infra::VerifyingFunctionMock onInitialized; + infra::VerifyingFunction onInitialized; services::FlashQuadSpiCypressFll flash; testing::StrictMock> finished; diff --git a/services/util/test/TestFlashQuadSpiMicronN25q.cpp b/services/util/test/TestFlashQuadSpiMicronN25q.cpp index 36c60d9b6..e63a60940 100644 --- a/services/util/test/TestFlashQuadSpiMicronN25q.cpp +++ b/services/util/test/TestFlashQuadSpiMicronN25q.cpp @@ -23,7 +23,7 @@ class FlashQuadSpiMicronN25qTest } testing::StrictMock spiStub; - infra::VerifyingFunctionMock onInitialized; + infra::VerifyingFunction onInitialized; services::FlashQuadSpiMicronN25q flash; testing::StrictMock> finished; diff --git a/services/util/test/TestFlashQuadSpiSingleSpeed.cpp b/services/util/test/TestFlashQuadSpiSingleSpeed.cpp index 64716aa68..dbb77010f 100644 --- a/services/util/test/TestFlashQuadSpiSingleSpeed.cpp +++ b/services/util/test/TestFlashQuadSpiSingleSpeed.cpp @@ -18,7 +18,7 @@ class FlashQuadSpiSingleSpeedTest } testing::StrictMock spiStub; - infra::VerifyingFunctionMock onInitialized; + infra::VerifyingFunction onInitialized; services::FlashQuadSpiSingleSpeed flash; testing::StrictMock> finished; diff --git a/services/util/test/TestSerialCommunicationLoopback.cpp b/services/util/test/TestSerialCommunicationLoopback.cpp index 748bf5db8..2f799952b 100644 --- a/services/util/test/TestSerialCommunicationLoopback.cpp +++ b/services/util/test/TestSerialCommunicationLoopback.cpp @@ -20,7 +20,7 @@ class SerialCommunicationLoopbackTest TEST_F(SerialCommunicationLoopbackTest, SendFromServerReceiveByClient) { infra::ConstByteRange data = infra::MakeStringByteRange("hello"); - infra::VerifyingFunctionMock clientReceiveCallback{ data }; + infra::VerifyingFunction clientReceiveCallback{ data }; client.ReceiveData(clientReceiveCallback); server.SendData(data, infra::emptyFunction); @@ -31,7 +31,7 @@ TEST_F(SerialCommunicationLoopbackTest, SendFromServerReceiveByClient) TEST_F(SerialCommunicationLoopbackTest, SendFromClientReceiveByServer) { infra::ConstByteRange data = infra::MakeStringByteRange("world"); - infra::VerifyingFunctionMock serverReceiveCallback{ data }; + infra::VerifyingFunction serverReceiveCallback{ data }; server.ReceiveData(serverReceiveCallback); client.SendData(data, infra::emptyFunction); diff --git a/services/util/test/TestWritableConfiguration.cpp b/services/util/test/TestWritableConfiguration.cpp index 460b8a6d7..6b6009e65 100644 --- a/services/util/test/TestWritableConfiguration.cpp +++ b/services/util/test/TestWritableConfiguration.cpp @@ -93,7 +93,7 @@ TEST_F(FlashReadingWritableConfigurationTest, write_data_in_empty_flash) formatter.PutVarInt(15); })); - infra::VerifyingFunctionMock callback; + infra::VerifyingFunction callback; configuration->Write(newValue, [&callback]() { callback.callback(); @@ -140,7 +140,7 @@ TEST_F(MemoryMappedWritableConfigurationTest, write_data_in_empty_flash) formatter.PutVarInt(15); })); - infra::VerifyingFunctionMock callback; + infra::VerifyingFunction callback; configuration->Write(newValue, [&callback]() { callback.callback();