From 2b8b37e6340920d47f36e6d4efa54be7f1f0adda Mon Sep 17 00:00:00 2001 From: kongr45gpen <electrovesta@gmail.com> Date: Thu, 14 Mar 2019 05:45:57 +0200 Subject: [PATCH] Add testing utilities to see whether Errors have been thrown after a Message Closes #14 Closes #8 --- inc/ErrorHandler.hpp | 15 ++++++---- test/Services/ServiceTests.hpp | 52 +++++++++++++++++++++++++++++++++- test/TestPlatform.cpp | 14 +++++++++ 3 files changed, 74 insertions(+), 7 deletions(-) diff --git a/inc/ErrorHandler.hpp b/inc/ErrorHandler.hpp index 46dfa145..d01471bf 100644 --- a/inc/ErrorHandler.hpp +++ b/inc/ErrorHandler.hpp @@ -219,17 +219,20 @@ public: if (typeid(ErrorType) == typeid(AcceptanceErrorType)) { return Acceptance; - } else if (typeid(ErrorType) == typeid(ExecutionStartErrorType)) { + } + if (typeid(ErrorType) == typeid(ExecutionStartErrorType)) { return ExecutionStart; - } else if (typeid(ErrorType) == typeid(ExecutionProgressErrorType)) { + } + if (typeid(ErrorType) == typeid(ExecutionProgressErrorType)) { return ExecutionProgress; - } else if (typeid(ErrorType) == typeid(ExecutionCompletionErrorType)) { + } + if (typeid(ErrorType) == typeid(ExecutionCompletionErrorType)) { return ExecutionCompletion; - } else if (typeid(ErrorType) == typeid(RoutingErrorType)) { + } + if (typeid(ErrorType) == typeid(RoutingErrorType)) { return Routing; - } else { - return Internal; } + return Internal; } }; diff --git a/test/Services/ServiceTests.hpp b/test/Services/ServiceTests.hpp index ede8804c..3cf718bd 100644 --- a/test/Services/ServiceTests.hpp +++ b/test/Services/ServiceTests.hpp @@ -23,6 +23,12 @@ class ServiceTests { */ static std::multimap<std::pair<ErrorHandler::ErrorSource, uint16_t>, bool> thrownErrors; + /** + * Whether an error assertion function was called, indicating that we are expecting to see + * Errors thrown after this Message + */ + static bool expectingErrors; + public: /** * Get a message from the list of queued messages to send @@ -65,10 +71,54 @@ public: } /** - * Remove all the queued messages from the list, starting over from 0 items again + * Remove all the queued messages & errors from the list, starting over from 0 items again */ static void reset() { queuedMessages.clear(); + thrownErrors.clear(); + expectingErrors = false; + } + + /** + * Return whether an error assertion function was called, which means that we are expecting this + * request to contain errors + * @return + */ + static bool isExpectingErrors() { + return expectingErrors; + } + + /** + * Find if there are *no* thrown errors + * @return True if 0 errors were thrown after the message + * @todo Implement a way to run this assertion at the end of every test + */ + static bool hasNoErrors() { + return thrownErrors.empty(); + } + + /** + * Find the number of thrown errors after the processing of this Message. + */ + static uint64_t countErrors() { + expectingErrors = true; + + return thrownErrors.size(); + } + + /** + * Find if an error + * @tparam ErrorType An enumeration of ErrorHandler + * @param errorType The error code of the Error, corresponding to the correct type as + * specified in ErrorHandler + */ + template<typename ErrorType> + static bool thrownError(ErrorType errorType) { + ErrorHandler::ErrorSource errorSource = ErrorHandler::findErrorSource(errorType); + + expectingErrors = true; + + return thrownErrors.find(std::make_pair(errorSource, errorType)) != thrownErrors.end(); } }; diff --git a/test/TestPlatform.cpp b/test/TestPlatform.cpp index 192802cd..0880d64e 100644 --- a/test/TestPlatform.cpp +++ b/test/TestPlatform.cpp @@ -5,6 +5,7 @@ #include <Service.hpp> #include "Services/ServiceTests.hpp" +// Explicit template specializations for the logError() function template void ErrorHandler::logError(const Message &, ErrorHandler::AcceptanceErrorType); template void ErrorHandler::logError(const Message &, ErrorHandler::ExecutionStartErrorType); template void ErrorHandler::logError(const Message &, ErrorHandler::ExecutionProgressErrorType); @@ -12,9 +13,11 @@ template void ErrorHandler::logError(const Message &, ErrorHandler::ExecutionCom template void ErrorHandler::logError(const Message &, ErrorHandler::RoutingErrorType); template void ErrorHandler::logError(ErrorHandler::InternalErrorType); +// Initialisation of ServiceTests properties std::vector<Message> ServiceTests::queuedMessages = std::vector<Message>(); std::multimap<std::pair<ErrorHandler::ErrorSource, uint16_t>, bool> ServiceTests::thrownErrors = std::multimap<std::pair<ErrorHandler::ErrorSource, uint16_t>, bool>(); +bool ServiceTests::expectingErrors = false; void Service::storeMessage(const Message &message) { // Just add the message to the queue @@ -34,6 +37,17 @@ void ErrorHandler::logError(ErrorType errorType) { struct ServiceTestsListener : Catch::TestEventListenerBase { using TestEventListenerBase::TestEventListenerBase; // inherit constructor + void sectionEnded(Catch::SectionStats const §ionStats) override { + // Make sure we don't have any errors + if (not ServiceTests::isExpectingErrors()) { + // An Error was thrown with this Message. If you expected this to happen, please call a + // corresponding assertion function from ServiceTests to silence this message. + + //TODO: Uncomment the following line as soon as Issue #19 is closed + // CHECK(ServiceTests::hasNoErrors()); + } + } + void testCaseEnded(Catch::TestCaseStats const &testCaseStats) override { // Tear-down after a test case is run ServiceTests::reset(); -- GitLab