Merge branch 'cynara' into tizen
[platform/core/test/security-tests.git] / src / framework / src / test_runner.cpp
index b753cdd..726d434 100644 (file)
@@ -13,7 +13,7 @@
  *    See the License for the specific language governing permissions and
  *    limitations under the License.
  */
-/*
+/**
  * @file        test_runner.cpp
  * @author      Przemyslaw Dobrowolski (p.dobrowolsk@samsung.com)
  * @author      Lukasz Wrzosek (l.wrzosek@samsung.com)
@@ -21,6 +21,8 @@
  * @brief       This file is the implementation file of test runner
  */
 #include <stddef.h>
+#include <dpl/test/test_failed.h>
+#include <dpl/test/test_ignored.h>
 #include <dpl/test/test_runner.h>
 #include <dpl/test/test_results_collector.h>
 #include <dpl/exception.h>
@@ -31,9 +33,6 @@
 #include <algorithm>
 #include <cstdio>
 #include <memory.h>
-#include <libgen.h>
-#include <cstring>
-#include <cstdlib>
 
 #include <libxml/xpath.h>
 #include <libxml/xpathInternals.h>
@@ -59,42 +58,6 @@ std::string getXMLNode(xmlNodePtr node)
 
 namespace DPL {
 namespace Test {
-namespace // anonymous
-{
-std::string BaseName(std::string aPath)
-{
-    ScopedFree<char> path(strdup(aPath.c_str()));
-    if (nullptr == path.Get()) {
-        throw std::bad_alloc();
-    }
-    char* baseName = basename(path.Get());
-    std::string retValue = baseName;
-    return retValue;
-}
-} // namespace anonymous
-
-//! \brief Failed test message creator
-//!
-//! \param[in] aTest string for tested expression
-//! \param[in] aFile source file name
-//! \param[in] aLine source file line
-//! \param[in] aMessage error message
-TestRunner::TestFailed::TestFailed(const char* aTest,
-                                   const char* aFile,
-                                   int aLine,
-                                   const std::string &aMessage)
-{
-    std::ostringstream assertMsg;
-    assertMsg << "[" << BaseName(aFile) << ":" << aLine
-              << "] Assertion failed ("
-              << aTest << ") " << aMessage;
-    m_message = assertMsg.str();
-}
-
-TestRunner::TestFailed::TestFailed(const std::string &message)
-{
-    m_message = message;
-}
 
 void TestRunner::RegisterTest(const char *testName, TestCase proc)
 {
@@ -246,6 +209,7 @@ bool TestRunner::filterByXML(std::map<std::string, bool> & casesMap)
 void TestRunner::RunTestCase(const TestCaseStruct& testCase)
 {
     setCurrentTestCase(&(const_cast<TestCaseStruct &>(testCase)));
+    m_deferDeepness = 0U;
     try {
         testCase.proc();
     } catch (const TestFailed &e) {
@@ -256,7 +220,7 @@ void TestRunner::RunTestCase(const TestCaseStruct& testCase)
 
         setCurrentTestCase(nullptr);
         return;
-    } catch (const Ignored &e) {
+    } catch (const TestIgnored &e) {
         if (m_runIgnored) {
             // Simple test have to be implemented
             CollectResult(testCase.name,
@@ -723,5 +687,60 @@ bool TestRunner::GetAllowChildLogs()
     return m_allowChildLogs;
 }
 
+void TestRunner::deferFailedException(const DPL::Test::TestFailed &ex)
+{
+    if (m_deferDeepness <= 0)
+        throw ex;
+
+    if (m_deferredExceptionsMessages.empty()) {
+        m_firstDeferredFail = ex;
+        m_firstDeferredExceptionType = DeferredExceptionType::DEFERRED_FAILED;
+    }
+    m_deferredExceptionsMessages.push_back(ex.GetMessage());
+}
+
+void TestRunner::deferIgnoredException(const DPL::Test::TestIgnored &ex)
+{
+    if (m_deferDeepness <= 0)
+        throw ex;
+
+    if (m_deferredExceptionsMessages.empty()) {
+        m_firstDeferredIgnore = ex;
+        m_firstDeferredExceptionType = DeferredExceptionType::DEFERRED_IGNORED;
+    }
+    m_deferredExceptionsMessages.push_back(ex.GetMessage());
+}
+
+void TestRunner::deferBegin()
+{
+    m_deferDeepness++;
+}
+
+void TestRunner::deferEnd()
+{
+    if (m_deferDeepness > 0)
+        m_deferDeepness--;
+
+    if (m_deferDeepness > 0)
+        return;
+
+    bool oops = std::uncaught_exception();
+    size_t additionalExceptions = oops ? 0 : 1;
+    for (size_t i = additionalExceptions; i < m_deferredExceptionsMessages.size(); ++i)
+        addFailReason(m_deferredExceptionsMessages[i]);
+
+    if (!oops && !m_deferredExceptionsMessages.empty())
+    {
+        m_deferredExceptionsMessages.clear();
+        switch (m_firstDeferredExceptionType) {
+            case DeferredExceptionType::DEFERRED_FAILED:
+                throw m_firstDeferredFail;
+            case DeferredExceptionType::DEFERRED_IGNORED:
+                throw m_firstDeferredIgnore;
+        }
+    }
+    m_deferredExceptionsMessages.clear();
 }
+
+} // namespace Test
 } // namespace DPL