From 566fc0228265e86ce2b36efaf1c06ba7648be36d Mon Sep 17 00:00:00 2001 From: Matthew Eshleman Date: Fri, 21 Jun 2024 20:41:57 -0500 Subject: [PATCH] add PostAndProcess helper convenience methods --- .../include/cms_cpputest_qf_ctrl.hpp | 21 ++++++ cpputest-for-qpc-lib/tests/CMakeLists.txt | 1 + .../tests/cms_cpputest_qf_ctrl_post_tests.cpp | 74 +++++++++++++++++++ 3 files changed, 96 insertions(+) create mode 100644 cpputest-for-qpc-lib/tests/cms_cpputest_qf_ctrl_post_tests.cpp diff --git a/cpputest-for-qpc-lib/include/cms_cpputest_qf_ctrl.hpp b/cpputest-for-qpc-lib/include/cms_cpputest_qf_ctrl.hpp index 8d9dbb9..2818db3 100644 --- a/cpputest-for-qpc-lib/include/cms_cpputest_qf_ctrl.hpp +++ b/cpputest-for-qpc-lib/include/cms_cpputest_qf_ctrl.hpp @@ -95,6 +95,27 @@ void PublishAndProcess(enum_t sig, void PublishAndProcess(QEvt const * e, PublishedEventRecorder* recorder = nullptr); +/// Helper method to Post an event to an active object +/// followed internally by ProcessEvents(). +/// \param e +/// \param dest +inline void PostAndProcess(QEvt const * e, QActive* dest) +{ + QACTIVE_POST(dest, e, nullptr); + ProcessEvents(); +} + +/// Helper method to Post a static const QEvt to an active object +/// followed internally by ProcessEvents(). +/// \param sig (template param): the signal value of the const event to post +/// \param dest : the active object to post to. +template +inline void PostAndProcess(QActive* dest) +{ + static const QEvt constEvent = QEVT_INITIALIZER(sig); + PostAndProcess(&constEvent, dest); +} + /// Get the internal library version string. /// Uses semantic versioning. const char * GetVersion(); diff --git a/cpputest-for-qpc-lib/tests/CMakeLists.txt b/cpputest-for-qpc-lib/tests/CMakeLists.txt index 7032cc2..2d56006 100644 --- a/cpputest-for-qpc-lib/tests/CMakeLists.txt +++ b/cpputest-for-qpc-lib/tests/CMakeLists.txt @@ -4,6 +4,7 @@ set(TEST_SOURCES qassertTests.cpp cms_cpputest_qf_ctrlTests.cpp cms_cpputest_qf_ctrlPublishTests.cpp + cms_cpputest_qf_ctrl_post_tests.cpp publishedEventRecorderTests.cpp backedQueueTests.cpp ) diff --git a/cpputest-for-qpc-lib/tests/cms_cpputest_qf_ctrl_post_tests.cpp b/cpputest-for-qpc-lib/tests/cms_cpputest_qf_ctrl_post_tests.cpp new file mode 100644 index 0000000..3183598 --- /dev/null +++ b/cpputest-for-qpc-lib/tests/cms_cpputest_qf_ctrl_post_tests.cpp @@ -0,0 +1,74 @@ +//// @brief Tests for the qf_ctrl post related functions for the QF cpputest port. +/// @ingroup +/// @cond +///*************************************************************************** +/// +/// Copyright (C) 2024 Matthew Eshleman. All rights reserved. +/// +/// This program is open source software: you can redistribute it and/or +/// modify it under the terms of the GNU General Public License as published +/// by the Free Software Foundation, either version 3 of the License, or +/// (at your option) any later version. +/// +/// Alternatively, upon written permission from Matthew Eshleman, this program +/// may be distributed and modified under the terms of a Commercial +/// License. For further details, see the Contact Information below. +/// +/// Contact Information: +/// Matthew Eshleman +/// https://covemountainsoftware.com +/// info@covemountainsoftware.com +///*************************************************************************** +/// @endcond + +#include "cmsDummyActiveObject.hpp" +#include "cms_cpputest_qf_ctrl.hpp" +#include "qpc.h" + +//cpputest header include must always be last +#include "CppUTest/TestHarness.h" + +using namespace cms::test; + +TEST_GROUP(qf_ctrl_post_tests) +{ + cms::DefaultDummyActiveObject* mDummy = nullptr; + + void setup() final + { + qf_ctrl::Setup(Q_USER_SIG, 100); + mDummy = new cms::DefaultDummyActiveObject(); + mDummy->dummyStart(); + } + + void teardown() final + { + delete mDummy; + cms::test::qf_ctrl::Teardown(); + } +}; + +TEST(qf_ctrl_post_tests, + provides_a_post_and_process_helper_func_with_trivial_signal_enum) +{ + static constexpr enum_t TEST1_SIG = Q_USER_SIG + 1; + enum_t capturedSig = -1; + mDummy->SetPostedEventHandler([&](const QEvt* e){ + capturedSig = e->sig; + }); + qf_ctrl::PostAndProcess(mDummy->getQActive()); + CHECK_EQUAL(TEST1_SIG, capturedSig); +} + +TEST(qf_ctrl_post_tests, + provides_a_post_and_process_helper_func) +{ + static constexpr enum_t TEST2_SIG = Q_USER_SIG + 111; + static const QEvt testEvent = QEVT_INITIALIZER(TEST2_SIG); + enum_t capturedSig = -1; + mDummy->SetPostedEventHandler([&](const QEvt* e){ + capturedSig = e->sig; + }); + qf_ctrl::PostAndProcess(&testEvent, mDummy->getQActive()); + CHECK_EQUAL(TEST2_SIG, capturedSig); +}