Commit 678e4f7c authored by Viktor Porvaznik's avatar Viktor Porvaznik Committed by Viktor Porvaznik

Trying to generate unit test coverage

parent e848cd23
......@@ -25,4 +25,27 @@ target_link_libraries(kdeconnect-filesync-daemon
install(TARGETS kdeconnect-filesync-daemon ${INSTALL_TARGETS_DEFAULT_ARGS})
# Add tests
#add_subdirectory(tests/index_test)
\ No newline at end of file
include(ECMCoverageOption)
set(BUILD_COVERAGE 1)
file(REMOVE_RECURSE ${CMAKE_BINARY_DIR}/tests/coverage)
file(REMOVE_RECURSE ${CMAKE_BINARY_DIR}/tests/html)
file(MAKE_DIRECTORY ${CMAKE_BINARY_DIR}/tests/coverage)
file(MAKE_DIRECTORY ${CMAKE_BINARY_DIR}/tests/html)
function(prepare_to_extract_coverage_data test_name class_under_test)
set(class_to_extract *${class_under_test}.*)
find_program(test_target ${class_under_test})
add_custom_command(TARGET ${test_target}
POST_BUILD
COMMAND echo "Processing gcov data ..."
COMMAND lcov --directory ${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles/${test_name}.dir/ --capture --output-file ${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles/${test_name}.dir/gcov.info
COMMAND lcov --extract ${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles/${test_name}.dir/gcov.info ${class_to_extract} --output-file ${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles/${test_name}.dir/${test_name}.info
COMMAND lcov --add-tracefile ${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles/${test_name}.dir/${test_name}.info --output-file ${CMAKE_BINARY_DIR}/tests/coverage/kdeconnect.info
COMMAND genhtml --legend -s -o ${CMAKE_BINARY_DIR}/tests/html/ ${CMAKE_BINARY_DIR}/tests/coverage/kdeconnect.info)
endfunction()
add_subdirectory(tests/singleton_pool_test)
add_subdirectory(tests/index_test)
\ No newline at end of file
......@@ -23,6 +23,7 @@
#include <QString>
#include <QMap>
#include <qdebug.h>
namespace FileSync
{
......@@ -41,6 +42,7 @@ namespace FileSync
{
s_instances = new QMap<QString, T *>();
instance = new T(instanceId);
qDebug() << "Created " << instance;
s_instances->insert(instanceId, instance);
}
else
......@@ -49,10 +51,12 @@ namespace FileSync
if (it != s_instances->end())
{
instance = it.value();
qDebug() << "Obtained " << instance;
}
else
{
instance = new T(instanceId);
qDebug() << "Created " << instance;
s_instances->insert(instanceId, instance);
}
}
......@@ -89,14 +93,17 @@ namespace FileSync
auto it = s_instances->find(instanceId);
if (it != s_instances->end())
{
delete it.value();
T *tmpInstance = it.value();
s_instances->remove(instanceId);
qDebug() << "Destroying " << tmpInstance;
delete tmpInstance;
}
}
static void destroyAllInstances()
{
delete s_instances;
s_instances = NULL;
}
private:
......
......@@ -29,3 +29,4 @@ ecm_add_test(
../../index.cpp
index_test.cpp LINK_LIBRARIES ${kdeconnect_libraries} TEST_NAME index_test)
prepare_to_extract_coverage_data("index_test" "index")
\ No newline at end of file
......@@ -34,98 +34,102 @@ namespace FileSync
private Q_SLOTS:
void initIndex()
{
m_testDeviceId = "testDeviceId";
QString testIndexPath = QDir::currentPath() +
"/../sync_index_test/test_resources/sync_db.json";
m_index = &Index::instance(m_testDeviceId);
QVERIFY(m_index != NULL);
QVERIFY(QFileInfo(testIndexPath).exists());
}
void testGetAndDestroyInstance0()
{
Index::destroyInstance(m_testDeviceId);
QVERIFY_EXCEPTION_THROWN(Index::instance(m_testDeviceId), std::logic_error);
}
void testGetAndDestroyInstance1ExistingIndex()
{
QString testDeviceId("testDeviceId");
QString testIndexPath = QDir::currentPath() +
"/../sync_index_test/test_resources/testGetAndDestroyInstance1ExistingIndex/sync_db.json";
Index *indexA = &Index::instance(testDeviceId, testIndexPath);
Index *indexB = &Index::instance();
QVERIFY(indexA == indexB);
QVERIFY(QFileInfo(testIndexPath).exists());
Index::destroyInstance();
QVERIFY_EXCEPTION_THROWN(Index::instance(m_testDeviceId), std::logic_error);
}
void testGetAndDestroyInstance1NotExistingIndex()
{
QString testDeviceId("testDeviceId");
QString testIndexPath = QDir::currentPath() +
"/../sync_index_test/test_resources/sync_db_that_does_not_exist.json";
Index *indexA = &Index::instance(testDeviceId, testIndexPath);
Index *indexB = &Index::instance();
QVERIFY(indexA == indexB);
QVERIFY(QFileInfo(testIndexPath).exists());
Index::destroyInstance();
QVERIFY_EXCEPTION_THROWN(Index::instance(), std::logic_error);
}
void testGetAll()
{
initIndex();
QMap<QString, QString> indexAData = m_index->getAll();
QVERIFY(indexAData["file_key1"] == "file_value1");
QVERIFY(indexAData.size() == 1);
destroyIndex();
}
void testSearch()
{
initIndex();
QString search = m_index->search("file_key1");
QVERIFY(search == "file_value1");
destroyIndex();
}
void testUpdateAndPersistency()
{
initIndex();
m_index->update(QPair<QString, QString>("new_key", "new_value"));
QMap<QString, QString> indexAData = m_index->getAll();
QVERIFY(indexAData["file_key1"] == "file_value1");
QVERIFY(indexAData["new_key"] == "new_value");
QVERIFY(indexAData.size() == 2);
destroyIndex();
initIndex();
indexAData = m_index->getAll();
QVERIFY(indexAData["file_key1"] == "file_value1");
QVERIFY(indexAData["new_key"] == "new_value");
QVERIFY(indexAData.size() == 2);
destroyIndex();
}
void destroyIndex()
{
Index::destroyInstance();
QVERIFY_EXCEPTION_THROWN(Index::instance(), std::logic_error);
m_index = NULL;
m_index = &Index::instance("testIndos");
// m_testDeviceId = "testDeviceId";
// QString testIndexPath = QDir::currentPath() +
// "/../sync_index_test/test_resources/sync_db.json";
// m_index = &Index::instance(m_testDeviceId);
// QVERIFY(m_index != NULL);
// QVERIFY(QFileInfo(testIndexPath).exists());
}
//
//
// void testGetAndDestroyInstance0()
// {
// Index::destroyInstance(m_testDeviceId);
// QVERIFY_EXCEPTION_THROWN(Index::instance(m_testDeviceId), std::logic_error);
// }
//
// void testGetAndDestroyInstance1ExistingIndex()
// {
// QString testDeviceId("testDeviceId");
// QString testIndexPath = QDir::currentPath() +
// "/../sync_index_test/test_resources/testGetAndDestroyInstance1ExistingIndex/sync_db.json";
// Index *indexA = &Index::instance(testDeviceId, testIndexPath);
// Index *indexB = &Index::instance();
// QVERIFY(indexA == indexB);
// QVERIFY(QFileInfo(testIndexPath).exists());
// Index::destroyInstance();
// QVERIFY_EXCEPTION_THROWN(Index::instance(m_testDeviceId), std::logic_error);
// }
//
// void testGetAndDestroyInstance1NotExistingIndex()
// {
// QString testDeviceId("testDeviceId");
// QString testIndexPath = QDir::currentPath() +
// "/../sync_index_test/test_resources/sync_db_that_does_not_exist.json";
// Index *indexA = &Index::instance(testDeviceId, testIndexPath);
// Index *indexB = &Index::instance();
// QVERIFY(indexA == indexB);
// QVERIFY(QFileInfo(testIndexPath).exists());
// Index::destroyInstance();
// QVERIFY_EXCEPTION_THROWN(Index::instance(), std::logic_error);
// }
//
// void testGetAll()
// {
// initIndex();
//
// QMap<QString, QString> indexAData = m_index->getAll();
// QVERIFY(indexAData["file_key1"] == "file_value1");
// QVERIFY(indexAData.size() == 1);
//
// destroyIndex();
// }
//
// void testSearch()
// {
// initIndex();
//
// QString search = m_index->search("file_key1");
// QVERIFY(search == "file_value1");
//
// destroyIndex();
// }
//
// void testUpdateAndPersistency()
// {
// initIndex();
//
// m_index->update(QPair<QString, QString>("new_key", "new_value"));
// QMap<QString, QString> indexAData = m_index->getAll();
// QVERIFY(indexAData["file_key1"] == "file_value1");
// QVERIFY(indexAData["new_key"] == "new_value");
// QVERIFY(indexAData.size() == 2);
//
// destroyIndex();
//
// initIndex();
//
// indexAData = m_index->getAll();
// QVERIFY(indexAData["file_key1"] == "file_value1");
// QVERIFY(indexAData["new_key"] == "new_value");
// QVERIFY(indexAData.size() == 2);
//
// destroyIndex();
// }
//
// void destroyIndex()
// {
// Index::destroyInstance();
// QVERIFY_EXCEPTION_THROWN(Index::instance(), std::logic_error);
// m_index = NULL;
// }
};
}
......
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fexceptions")
find_package(Qt5 ${QT_MIN_VERSION} REQUIRED COMPONENTS Test)
include_directories(
${KDEConnectCore_BINARY_DIR}
${CMAKE_SOURCE_DIR}
../../
)
set(kdeconnect_libraries
kdeconnectcore
KF5::I18n
KF5::KIOWidgets
Qt5::DBus
Qt5::Network
Qt5::Test
qca-qt5
pthread
)
ecm_add_test(
../../common.cpp
../../sync_operation.cpp
../../log.cpp
../../index.cpp
singleton_pool_test.cpp LINK_LIBRARIES ${kdeconnect_libraries} TEST_NAME singleton_pool_test)
prepare_to_extract_coverage_data("singleton_pool_test" "singleton_pool")
/**
* Copyright 2019 Viktor Porvaznik <viktor.porvaznik@gmail.com>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of
* the License or (at your option) version 3 or any later version
* accepted by the membership of KDE e.V. (or its successor approved
* by the membership of KDE e.V.), which shall act as a proxy
* defined in Section 14 of version 3 of the license.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <QObject>
#include <QTest>
#include <QThread>
#include "singleton_pool.h"
namespace FileSync
{
class SingletonPoolTest : public QObject
{
Q_OBJECT
private:
class TestSingletonClass : public SingletonPool<TestSingletonClass>
{
public:
TestSingletonClass(const QString &instanceId) : creationTime(
QDateTime::currentMSecsSinceEpoch())
{
qDebug() << "created " << creationTime;
}
qint64 creationTime;
};
TestSingletonClass *m_pool;
private Q_SLOTS:
/**
* Try to create and destroy single instance, and repeat verifying that
* second created instance is a different one
*/
void testCreateAndDestroySingleInstance()
{
qint64 originalCTime = m_pool->instance("testInstance0").creationTime;
QVERIFY(m_pool->instance("testInstance0").creationTime == originalCTime);
QVERIFY(m_pool->instance("testInstance0").creationTime == originalCTime);
QVERIFY(m_pool->instance("testInstance0").creationTime == originalCTime);
QVERIFY(m_pool->instance("testInstance0").creationTime == originalCTime);
QVERIFY(m_pool->instance("testInstance0").creationTime == originalCTime);
QVERIFY(m_pool->instance("testInstance0").creationTime == originalCTime);
m_pool->destroyInstance("testInstance0");
QThread::msleep(1);
qint64 newCTime = m_pool->instance("testInstance0").creationTime;
QVERIFY(newCTime != originalCTime);
QVERIFY(m_pool->instance("testInstance0").creationTime == newCTime);
QVERIFY(m_pool->instance("testInstance0").creationTime == newCTime);
m_pool->destroyInstance("testInstance0");
}
/**
* Create three instances and
*/
void testCreateAndDestroyTwoInstances()
{
qint64 originalCTime0 = m_pool->instance("testInstance0").creationTime;
QThread::msleep(1);
qint64 originalCTime1 = m_pool->instance("testInstance1").creationTime;
QThread::msleep(1);
qint64 originalCTime2 = m_pool->instance("testInstance2").creationTime;
QVERIFY(m_pool->instance("testInstance0").creationTime == originalCTime0);
QVERIFY(m_pool->instance("testInstance1").creationTime == originalCTime1);
QVERIFY(m_pool->instance("testInstance2").creationTime == originalCTime2);
QVERIFY(originalCTime0 != originalCTime1);
QVERIFY(originalCTime0 != originalCTime2);
QVERIFY(originalCTime1 != originalCTime2);
QVERIFY(m_pool->instance("testInstance0").creationTime == originalCTime0);
QVERIFY(m_pool->instance("testInstance1").creationTime == originalCTime1);
QVERIFY(m_pool->instance("testInstance2").creationTime == originalCTime2);
QVERIFY(originalCTime0 != originalCTime1);
QVERIFY(originalCTime0 != originalCTime2);
QVERIFY(originalCTime1 != originalCTime2);
QVERIFY(originalCTime0 != originalCTime1);
QVERIFY(m_pool->instance("testInstance0").creationTime == originalCTime0);
QVERIFY(m_pool->instance("testInstance1").creationTime == originalCTime1);
QVERIFY(m_pool->instance("testInstance2").creationTime == originalCTime2);
QVERIFY(originalCTime0 != originalCTime1);
QVERIFY(originalCTime0 != originalCTime2);
QVERIFY(originalCTime1 != originalCTime2);
m_pool->destroyInstance("testInstance1");
QVERIFY(m_pool->instance("testInstance0").creationTime == originalCTime0);
QVERIFY(m_pool->instance("testInstance2").creationTime == originalCTime2);
QVERIFY(originalCTime0 != originalCTime2);
QThread::msleep(1);
qint64 newCTime1 = m_pool->instance("testInstance1").creationTime;
QVERIFY(m_pool->instance("testInstance1").creationTime == newCTime1);
QVERIFY(originalCTime1 != newCTime1);
m_pool->destroyAllInstances();
QThread::msleep(1);
originalCTime0 = m_pool->instance("testInstance0").creationTime;
QThread::msleep(1);
originalCTime1 = m_pool->instance("testInstance1").creationTime;
QThread::msleep(1);
originalCTime2 = m_pool->instance("testInstance2").creationTime;
QVERIFY(m_pool->instance("testInstance0").creationTime == originalCTime0);
QVERIFY(m_pool->instance("testInstance1").creationTime == originalCTime1);
QVERIFY(m_pool->instance("testInstance2").creationTime == originalCTime2);
QVERIFY(originalCTime0 != originalCTime1);
QVERIFY(originalCTime0 != originalCTime2);
QVERIFY(originalCTime1 != originalCTime2);
QVERIFY(m_pool->instance("testInstance0").creationTime == originalCTime0);
QVERIFY(m_pool->instance("testInstance1").creationTime == originalCTime1);
QVERIFY(m_pool->instance("testInstance2").creationTime == originalCTime2);
QVERIFY(originalCTime0 != originalCTime1);
QVERIFY(originalCTime0 != originalCTime2);
QVERIFY(originalCTime1 != originalCTime2);
}
};
}
QTEST_MAIN(FileSync::SingletonPoolTest)
#include "singleton_pool_test.moc"
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment