Add defer macros
[platform/core/test/security-tests.git] / src / framework / include / dpl / test / test_runner.h
index 5f070c8..53764bf 100644 (file)
@@ -25,6 +25,7 @@
 #define DPL_TEST_RUNNER_H
 
 #include <chrono>
+#include <cstdlib>
 #include <cstring>
 #include <exception>
 #include <iostream>
@@ -41,6 +42,7 @@
 #include <dpl/colors.h>
 #include <dpl/gdbbacktrace.h>
 #include <dpl/singleton.h>
+#include <dpl/test/test_exception.h>
 #include <dpl/test/test_failed.h>
 #include <dpl/test/test_ignored.h>
 #include <dpl/test/test_results_collector.h>
@@ -63,6 +65,8 @@ class TestRunner
         m_currentTestCase(nullptr)
       , m_terminate(false)
       , m_allowChildLogs(false)
+      , m_deferDeepness(0U)
+      , m_firstDeferredExceptionType(DeferredExceptionType::DEFERRED_FAILED)
     {}
 
     void beginPerformanceTestTime(std::chrono::system_clock::duration maxTimeInMicroseconds);
@@ -166,6 +170,21 @@ class TestRunner
     // The runner will terminate as soon as possible (after current test).
     void Terminate();
     bool GetAllowChildLogs();
+
+    void deferFailedException(const DPL::Test::TestFailed &ex);
+    void deferIgnoredException(const DPL::Test::TestIgnored &ex);
+    void deferBegin();
+    void deferEnd();
+
+private:
+    std::vector<std::string> m_deferredExceptionsMessages;
+    std::size_t m_deferDeepness;
+    enum DeferredExceptionType {
+        DEFERRED_FAILED,
+        DEFERRED_IGNORED,
+    } m_firstDeferredExceptionType;
+    DPL::Test::TestFailed m_firstDeferredFail;
+    DPL::Test::TestIgnored m_firstDeferredIgnore;
 };
 
 typedef DPL::Singleton<TestRunner> TestRunnerSingleton;
@@ -307,4 +326,39 @@ typedef DPL::Singleton<TestRunner> TestRunnerSingleton;
                   << DPL::Colors::Text::RED_END << std::endl; \
     } while (0)
 
+/**
+ * DEFER MACROS
+ *
+ * Use them to defer fails and ignores in test cases.
+ * Some code constructions disallow to throw. Such places can be surrounded
+ * with RUNNER_DEFER_SCOPE macro. RUNNER_DEFER_TRYCATCH macro can be used to catch possibly thrown
+ * exceptions within such scope. Possibly catched exceptions will be rethrown
+ * when leaving RUNNER_DEFER_SCOPE scope.
+ * Macros can be safely nested.
+ */
+
+
+#define RUNNER_DEFER_TRYCATCH(scope)                                              \
+    do {                                                                          \
+        try                                                                       \
+        {                                                                         \
+            scope                                                                 \
+        }                                                                         \
+        catch (const DPL::Test::TestFailed &ex)                                   \
+        {                                                                         \
+            DPL::Test::TestRunnerSingleton::Instance().deferFailedException(ex);  \
+        }                                                                         \
+        catch (const DPL::Test::TestIgnored &ex)                                  \
+        {                                                                         \
+            DPL::Test::TestRunnerSingleton::Instance().deferIgnoredException(ex); \
+        }                                                                         \
+    } while (0)                                                                   \
+
+#define RUNNER_DEFER_SCOPE(scope)                                \
+    do {                                                         \
+        DPL::Test::TestRunnerSingleton::Instance().deferBegin(); \
+        scope                                                    \
+        DPL::Test::TestRunnerSingleton::Instance().deferEnd();   \
+    } while (0)                                                  \
+
 #endif // DPL_TEST_RUNNER_H