Add custom TestEventListener 99/25899/3
authorAleksander Zdyb <a.zdyb@partner.samsung.com>
Thu, 14 Aug 2014 05:13:23 +0000 (07:13 +0200)
committerAleksander Zdyb <a.zdyb@partner.samsung.com>
Thu, 14 Aug 2014 05:13:23 +0000 (07:13 +0200)
This is needed to show custom properties of test cases
(like for example time elapsed in benchmarks).

Change-Id: Id54c709125c07ceb4d3b387f76047706361d05b3

test/CMakeLists.txt
test/TestEventListenerProxy.cpp [new file with mode: 0644]
test/TestEventListenerProxy.h [new file with mode: 0644]
test/tests.cpp [new file with mode: 0644]

index 820279f..0f64ab4 100644 (file)
@@ -16,7 +16,7 @@
 # @author      Aleksander Zdyb <a.zdyb@partner.samsung.com>
 # @brief       Cmake for tests
 #
-PKG_CHECK_MODULES(PKGS REQUIRED gmock_main)
+PKG_CHECK_MODULES(PKGS REQUIRED gmock)
 
 ADD_DEFINITIONS("-DCYNARA_NO_LOGS")
 SET(CYNARA_SRC ${PROJECT_SOURCE_DIR}/src)
@@ -49,6 +49,8 @@ SET(CYNARA_TESTS_SOURCES
     storage/serializer/serialize.cpp
     common/types/policybucket.cpp
     helpers.cpp
+    TestEventListenerProxy.cpp
+    tests.cpp
 )
 
 INCLUDE_DIRECTORIES(
diff --git a/test/TestEventListenerProxy.cpp b/test/TestEventListenerProxy.cpp
new file mode 100644 (file)
index 0000000..f00a00a
--- /dev/null
@@ -0,0 +1,86 @@
+/*
+ * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ *    Licensed under the Apache License, Version 2.0 (the "License");
+ *    you may not use this file except in compliance with the License.
+ *    You may obtain a copy of the License at
+ *
+ *        http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *    Unless required by applicable law or agreed to in writing, software
+ *    distributed under the License is distributed on an "AS IS" BASIS,
+ *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *    See the License for the specific language governing permissions and
+ *    limitations under the License.
+ */
+/*
+ * @file        TestEventListenerProxy.cpp
+ * @author      Aleksander Zdyb <a.zdyb@partner.samsung.com>
+ * @version     1.0
+ * @brief       Proxy for ::testing::TestEventListener
+ */
+
+#include "TestEventListenerProxy.h"
+
+namespace Cynara {
+
+TestEventListenerProxy::TestEventListenerProxy(TestEventListener *originalListener)
+    : m_originalListener(originalListener) {};
+
+TestEventListenerProxy::~TestEventListenerProxy() {
+    delete m_originalListener;
+};
+
+void TestEventListenerProxy::OnTestProgramStart(const UnitTest &unit_test) {
+    m_originalListener->OnTestProgramStart(unit_test);
+}
+
+void TestEventListenerProxy::OnTestIterationStart(const UnitTest &unit_test, int iteration) {
+    m_originalListener->OnTestIterationStart(unit_test, iteration);
+}
+
+void TestEventListenerProxy::OnEnvironmentsSetUpStart(const UnitTest &unit_test) {
+    m_originalListener->OnEnvironmentsSetUpStart(unit_test);
+}
+
+void TestEventListenerProxy::OnEnvironmentsSetUpEnd(const UnitTest &unit_test) {
+    m_originalListener->OnEnvironmentsSetUpEnd(unit_test);
+}
+
+void TestEventListenerProxy::OnTestCaseStart(const TestCase &test_case) {
+    m_originalListener->OnTestCaseStart(test_case);
+}
+
+void TestEventListenerProxy::OnTestStart(const TestInfo &test_info) {
+    m_originalListener->OnTestStart(test_info);
+}
+
+void TestEventListenerProxy::OnTestPartResult(const TestPartResult &result) {
+    m_originalListener->OnTestPartResult(result);
+}
+
+void TestEventListenerProxy::OnTestEnd(const TestInfo &test_info) {
+    m_originalListener->OnTestEnd(test_info);
+}
+
+void TestEventListenerProxy::OnTestCaseEnd(const TestCase &test_case) {
+    m_originalListener->OnTestCaseEnd(test_case);
+}
+
+void TestEventListenerProxy::OnEnvironmentsTearDownStart(const UnitTest &unit_test) {
+    m_originalListener->OnEnvironmentsTearDownStart(unit_test);
+}
+
+void TestEventListenerProxy::OnEnvironmentsTearDownEnd(const UnitTest &unit_test) {
+    m_originalListener->OnEnvironmentsTearDownEnd(unit_test);
+}
+
+void TestEventListenerProxy::OnTestIterationEnd(const UnitTest &unit_test, int iteration) {
+    m_originalListener->OnTestIterationEnd(unit_test, iteration);
+}
+
+void TestEventListenerProxy::OnTestProgramEnd(const UnitTest &unit_test) {
+    m_originalListener->OnTestProgramEnd(unit_test);
+}
+
+} /* namespace Cynara */
diff --git a/test/TestEventListenerProxy.h b/test/TestEventListenerProxy.h
new file mode 100644 (file)
index 0000000..79097ce
--- /dev/null
@@ -0,0 +1,63 @@
+/*
+ * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ *    Licensed under the Apache License, Version 2.0 (the "License");
+ *    you may not use this file except in compliance with the License.
+ *    You may obtain a copy of the License at
+ *
+ *        http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *    Unless required by applicable law or agreed to in writing, software
+ *    distributed under the License is distributed on an "AS IS" BASIS,
+ *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *    See the License for the specific language governing permissions and
+ *    limitations under the License.
+ */
+/*
+ * @file        TestEventListenerProxy.h
+ * @author      Aleksander Zdyb <a.zdyb@partner.samsung.com>
+ * @version     1.0
+ * @brief       Proxy for ::testing::TestEventListener
+ */
+
+#ifndef TEST_TESTEVENTLISTENERPROXY_H_
+#define TEST_TESTEVENTLISTENERPROXY_H_
+
+#include <gtest/gtest.h>
+
+namespace Cynara {
+
+using namespace testing;
+
+class TestEventListenerProxy : public TestEventListener
+{
+public:
+    explicit TestEventListenerProxy(TestEventListener *originalListener);
+    virtual ~TestEventListenerProxy();
+
+    virtual void OnTestProgramStart(const UnitTest &unit_test);
+    virtual void OnTestIterationStart(const UnitTest &unit_test, int iteration);
+    virtual void OnEnvironmentsSetUpStart(const UnitTest &unit_test);
+    virtual void OnEnvironmentsSetUpEnd(const UnitTest &unit_test);
+    virtual void OnTestCaseStart(const TestCase &test_case);
+    virtual void OnTestStart(const TestInfo &test_info);
+    virtual void OnTestPartResult(const TestPartResult &result);
+    virtual void OnTestEnd(const TestInfo &test_info);
+    virtual void OnTestCaseEnd(const TestCase &test_case);
+    virtual void OnEnvironmentsTearDownStart(const UnitTest &unit_test);
+    virtual void OnEnvironmentsTearDownEnd(const UnitTest &unit_test);
+    virtual void OnTestIterationEnd(const UnitTest &unit_test, int iteration);
+    virtual void OnTestProgramEnd(const UnitTest &unit_test);
+
+protected:
+    TestEventListener *originalListener() {
+        return m_originalListener;
+    }
+
+private:
+    TestEventListener *m_originalListener;
+};
+
+} /* namespace Cynara */
+
+#endif /* TEST_TESTEVENTLISTENERPROXY_H_ */
diff --git a/test/tests.cpp b/test/tests.cpp
new file mode 100644 (file)
index 0000000..1525fa9
--- /dev/null
@@ -0,0 +1,59 @@
+/*
+ * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ *    Licensed under the Apache License, Version 2.0 (the "License");
+ *    you may not use this file except in compliance with the License.
+ *    You may obtain a copy of the License at
+ *
+ *        http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *    Unless required by applicable law or agreed to in writing, software
+ *    distributed under the License is distributed on an "AS IS" BASIS,
+ *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *    See the License for the specific language governing permissions and
+ *    limitations under the License.
+ */
+/*
+ * @file        tests.cpp
+ * @author      Aleksander Zdyb <a.zdyb@partner.samsung.com>
+ * @version     1.0
+ * @brief       Unit-tests setup
+ */
+
+#include <gmock/gmock.h>
+#include <gtest/gtest.h>
+
+#include "TestEventListenerProxy.h"
+
+class PropertyAwarePrettyUnitTestResultPrinter : public Cynara::TestEventListenerProxy {
+    using Cynara::TestEventListenerProxy::TestEventListenerProxy;
+
+    void OnTestEnd(const ::testing::TestInfo &testInfo) {
+        auto result = testInfo.result();
+        for (int i = 0; i < result->test_property_count(); ++i) {
+            const auto &property = result->GetTestProperty(i);
+            std::cout << "             - "
+                      << property.key() << " = " << property.value()
+                      << std::endl;
+        }
+        originalListener()->OnTestEnd(testInfo);
+    }
+};
+
+int main(int argc, char** argv) {
+    // Disables elapsed time by default.
+    ::testing::GTEST_FLAG(print_time) = false;
+
+    // This allows the user to override the flag on the command line.
+    ::testing::InitGoogleTest(&argc, argv);
+
+    // Gets hold of the event listener list.
+    auto& listeners = ::testing::UnitTest::GetInstance()->listeners();
+
+    // Remove original listener (PrettyUnitTestResultPrinter)
+    // and wrap it with our proxy (PropertyAwarePrettyUnitTestResultPrinter)
+    auto originalListener = listeners.Release(listeners.default_result_printer());
+    listeners.Append(new PropertyAwarePrettyUnitTestResultPrinter(originalListener));
+
+    return RUN_ALL_TESTS();
+}