Exception tests refactoring 09/238809/2
authorKrzysztof Jackiewicz <k.jackiewicz@samsung.com>
Fri, 17 Jul 2020 11:52:17 +0000 (13:52 +0200)
committerKrzysztof Jackiewicz <k.jackiewicz@samsung.com>
Mon, 20 Jul 2020 08:52:13 +0000 (08:52 +0000)
* Positive tests merged into one.
* Macros replaced with templates.
* Missing exceptions added.

Change-Id: Ia2da4262e874119a70940c1005d7c018aea9641b

unit-tests/test_exception.cpp

index cac3ec2..50e7d73 100644 (file)
 
 using namespace CKM;
 
-#define CHECK_EXCEPTION(exception, ec, isDebug) \
-do { \
-       const std::string errmsg = "test exception string"; \
-       BOOST_REQUIRE_THROW(ThrowErr(exception, errmsg), exception); \
-       try { \
-               ThrowErr(exception, errmsg); \
-       } catch (const exception &e) { \
-               checkExceptionInternal(e, ec, isDebug ? std::string() : errmsg); \
-       } \
-} while (false)
-
 namespace {
 
-void checkExceptionInternal(const Exc::Exception &e, int ec, const std::string &msg)
-{
-       BOOST_REQUIRE_MESSAGE(e.error() == ec,
-               "ec(" << ec << ") not matched(" << e.error() << ")");
-
-       BOOST_REQUIRE_MESSAGE(msg == e.what(),
-               "msg(" << msg << ") isn't matched(" << e.what() << ")");
-
-       BOOST_REQUIRE_MESSAGE(e.message().find(msg) != std::string::npos,
-               "msg(" << msg << ") isn't contained from message("
-               << e.message() << ")");
-}
-
-} // namespace anonymous
-
-BOOST_AUTO_TEST_SUITE(EXCEPTION_TEST)
-
-POSITIVE_TEST_CASE(internal_error)
-{
-       CHECK_EXCEPTION(Exc::InternalError, CKM_API_ERROR_SERVER_ERROR, false);
-}
-
-POSITIVE_TEST_CASE(database_locked)
-{
-       CHECK_EXCEPTION(Exc::DatabaseLocked, CKM_API_ERROR_DB_LOCKED, false);
-}
-
-POSITIVE_TEST_CASE(database_failed)
+template <int Error, bool IsDebug, typename Before, typename After>
+void verifyCatched(const Exc::DefineException<Error, IsDebug, Before, After>& e,
+                   const std::string errmsg)
 {
-       CHECK_EXCEPTION(Exc::DatabaseFailed, CKM_API_ERROR_DB_ERROR, false);
+       BOOST_REQUIRE_MESSAGE(e.error() == Error,
+                                                 "ec(" << Error << ") not matched(" << e.error() << ")");
+
+       if (IsDebug) {
+               BOOST_REQUIRE_MESSAGE(std::string() == e.what(),
+                                                         "expected empty e.what(), got(" << e.what() << ")");
+       } else {
+               BOOST_REQUIRE_MESSAGE(errmsg == e.what(),
+                                                         "msg(" << errmsg << ") isn't matched(" << e.what() << ")");
+
+               BOOST_REQUIRE_MESSAGE(e.message().find(errmsg) != std::string::npos,
+                                                         "msg(" << errmsg << ") isn't contained from message("
+                                                         << e.message() << ")");
+       }
 }
 
-POSITIVE_TEST_CASE(filesystem_failed)
+template <typename Exc>
+void checkException()
 {
-       CHECK_EXCEPTION(Exc::FileSystemFailed, CKM_API_ERROR_FILE_SYSTEM, false);
+       const std::string errmsg = "test exception string";
+       try {
+               ThrowErr(Exc, errmsg);
+               BOOST_FAIL("No exception thrown");
+       } catch (const Exc &e) {
+               verifyCatched(e ,errmsg);
+       }
 }
 
-POSITIVE_TEST_CASE(inputparam)
-{
-       CHECK_EXCEPTION(Exc::InputParam, CKM_API_ERROR_INPUT_PARAM, true);
-}
+} // namespace anonymous
 
-POSITIVE_TEST_CASE(authentication_failed)
-{
-       CHECK_EXCEPTION(Exc::AuthenticationFailed, CKM_API_ERROR_AUTHENTICATION_FAILED, true);
-}
+BOOST_AUTO_TEST_SUITE(EXCEPTION_TEST)
 
-POSITIVE_TEST_CASE(transaction_failed)
+POSITIVE_TEST_CASE(check_exceptions)
 {
-       CHECK_EXCEPTION(Exc::TransactionFailed, CKM_API_ERROR_DB_ERROR, false);
+       checkException<Exc::InternalError>();
+       checkException<Exc::DatabaseLocked>();
+       checkException<Exc::DatabaseFailed>();
+       checkException<Exc::FileSystemFailed>();
+       checkException<Exc::InputParam>();
+       checkException<Exc::AuthenticationFailed>();
+       checkException<Exc::VerificationFailed>();
+       checkException<Exc::InvalidFormat>();
+       checkException<Exc::BadResponse>();
+       checkException<Exc::TransactionFailed>();
 }
 
 BOOST_AUTO_TEST_SUITE_END()