diff --git a/inc/ErrorHandler.hpp b/inc/ErrorHandler.hpp index 46dfa145eb5923cacf9f5c095497d65797c3ceb0..d01471bfd0db9c3fc51213389ecbbf8124f9bedc 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 ede8804ce4a33c56ae4b93b58e229edcd1392f08..3cf718bd3ad33c1da9b8be2a2a3ca3b4248d4a48 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 192802cd0796afbeca098697ff70b45ad10b58b3..0880d64ea865d220846ac5659068606cbce65d62 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();