Commit 5c6d6389 authored by Igor Kushnir's avatar Igor Kushnir
Browse files

Extract common test code and use QTemporaryDir

The temporary directory and files are now removed at test/bench exit.
parent 518cdc62
......@@ -6,23 +6,21 @@ remove_definitions(
-DQT_NO_CAST_FROM_BYTEARRAY
)
add_library(itemrepositorytestbase STATIC itemrepositorytestbase.cpp)
target_link_libraries(itemrepositorytestbase PUBLIC
Qt5::Test KDev::Serialization KDev::Tests)
if(BUILD_BENCHMARKS)
ecm_add_test(bench_itemrepository.cpp LINK_LIBRARIES
LINK_LIBRARIES Qt5::Test KDev::Serialization KDev::Tests)
ecm_add_test(bench_indexedstring.cpp LINK_LIBRARIES
LINK_LIBRARIES Qt5::Test KDev::Serialization KDev::Tests)
ecm_add_test(bench_itemrepository.cpp LINK_LIBRARIES itemrepositorytestbase)
ecm_add_test(bench_indexedstring.cpp LINK_LIBRARIES itemrepositorytestbase)
set_tests_properties(bench_itemrepository PROPERTIES TIMEOUT 30)
set_tests_properties(bench_indexedstring PROPERTIES TIMEOUT 30)
endif()
ecm_add_test(test_itemrepository.cpp
LINK_LIBRARIES Qt5::Test KDev::Serialization KDev::Tests
)
ecm_add_test(test_itemrepository.cpp LINK_LIBRARIES itemrepositorytestbase)
ecm_add_test(test_itemrepositoryregistry_automatic.cpp
LINK_LIBRARIES Qt5::Test KDev::Serialization KDev::Tests
)
ecm_add_test(test_itemrepositoryregistry_deferred.cpp
LINK_LIBRARIES Qt5::Test KDev::Serialization KDev::Tests
)
ecm_add_test(test_indexedstring.cpp LINK_LIBRARIES
LINK_LIBRARIES Qt5::Test KDev::Serialization KDev::Tests
)
ecm_add_test(test_indexedstring.cpp LINK_LIBRARIES itemrepositorytestbase)
......@@ -8,12 +8,10 @@
#include "bench_indexedstring.h"
#include <language/util/kdevhash.h>
#include <serialization/itemrepositoryregistry.h>
#include <serialization/indexedstring.h>
#include <tests/testhelpers.h>
#include <QTest>
#include <QStandardPaths>
#include <utility>
#include <vector>
......@@ -22,17 +20,6 @@ QTEST_GUILESS_MAIN(BenchIndexedString)
using namespace KDevelop;
void BenchIndexedString::initTestCase()
{
QStandardPaths::setTestModeEnabled(true);
ItemRepositoryRegistry::initialize(m_repositoryPath);
}
void BenchIndexedString::cleanupTestCase()
{
ItemRepositoryRegistry::deleteRepositoryFromDisk(m_repositoryPath);
}
static QVector<QString> generateData()
{
QVector<QString> data;
......
......@@ -7,18 +7,14 @@
#ifndef BENCH_INDEXEDSTRING_H
#define BENCH_INDEXEDSTRING_H
#include <QDir>
#include <QObject>
#include "itemrepositorytestbase.h"
class BenchIndexedString
: public QObject
: public ItemRepositoryTestBase
{
Q_OBJECT
private Q_SLOTS:
void initTestCase();
void cleanupTestCase();
void bench_index();
void bench_length();
void bench_qstring();
......@@ -32,9 +28,6 @@ private Q_SLOTS:
void bench_create();
void bench_destroy();
void bench_swap();
private:
const QString m_repositoryPath = QDir::tempPath() + QStringLiteral("/bench_indexedstring");
};
#endif // BENCH_INDEXEDSTRING_H
......@@ -15,7 +15,6 @@
#include <random>
#include <vector>
#include <QTest>
#include <QStandardPaths>
QTEST_GUILESS_MAIN(BenchItemRepository)
......@@ -98,17 +97,6 @@ struct TestDataRepositoryItemRequest
using TestDataRepository = ItemRepository<TestData, TestDataRepositoryItemRequest, false, true>;
void BenchItemRepository::initTestCase()
{
QStandardPaths::setTestModeEnabled(true);
ItemRepositoryRegistry::initialize(m_repositoryPath);
}
void BenchItemRepository::cleanupTestCase()
{
ItemRepositoryRegistry::deleteRepositoryFromDisk(m_repositoryPath);
}
static QVector<QString> generateData()
{
QVector<QString> data;
......
......@@ -7,18 +7,14 @@
#ifndef BENCHITEMREPOSITORY_H
#define BENCHITEMREPOSITORY_H
#include <QDir>
#include <QObject>
#include "itemrepositorytestbase.h"
class BenchItemRepository
: public QObject
: public ItemRepositoryTestBase
{
Q_OBJECT
private Q_SLOTS:
void initTestCase();
void cleanupTestCase();
void insert();
void remove();
void removeDisk();
......@@ -27,9 +23,6 @@ private Q_SLOTS:
void shouldDoReferenceCounting_data();
void shouldDoReferenceCounting();
private:
const QString m_repositoryPath = QDir::tempPath() + QStringLiteral("/bench_itemrepository");
};
#endif // BENCHITEMREPOSITORY_H
/*
SPDX-FileCopyrightText: 2012-2013 Milian Wolff <mail@milianw.de>
SPDX-FileCopyrightText: 2021 Igor Kushnir <igorkuo@gmail.com>
SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
*/
#include "itemrepositorytestbase.h"
#include <serialization/itemrepositoryregistry.h>
#include <QStandardPaths>
#include <QTest>
using namespace KDevelop;
void ItemRepositoryTestBase::initTestCase()
{
QStandardPaths::setTestModeEnabled(true);
QVERIFY2(m_repositoryDir.isValid(),
qPrintable("couldn't create temporary directory: " + m_repositoryDir.errorString()));
ItemRepositoryRegistry::initialize(m_repositoryDir.path());
}
void ItemRepositoryTestBase::cleanupTestCase()
{
if (!m_repositoryDir.isValid()) {
return; // the repository has not been initialized => skip the cleanup
}
ItemRepositoryRegistry::deleteRepositoryFromDisk(m_repositoryDir.path());
}
/*
SPDX-FileCopyrightText: 2021 Igor Kushnir <igorkuo@gmail.com>
SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
*/
#ifndef KDEVPLATFORM_ITEMREPOSITORYTESTBASE_H
#define KDEVPLATFORM_ITEMREPOSITORYTESTBASE_H
#include <QObject>
#include <QTemporaryDir>
class ItemRepositoryTestBase : public QObject
{
Q_OBJECT
private Q_SLOTS:
void initTestCase();
void cleanupTestCase();
private:
const QTemporaryDir m_repositoryDir;
};
#endif // KDEVPLATFORM_ITEMREPOSITORYTESTBASE_H
......@@ -6,11 +6,10 @@
#include "test_indexedstring.h"
#include <serialization/itemrepositoryregistry.h>
#include <serialization/indexedstring.h>
#include <serialization/referencecounting.h>
#include <QTest>
#include <QStandardPaths>
#include <cstddef>
#include <utility>
......@@ -19,17 +18,6 @@ QTEST_GUILESS_MAIN(TestIndexedString)
using namespace KDevelop;
void TestIndexedString::initTestCase()
{
QStandardPaths::setTestModeEnabled(true);
ItemRepositoryRegistry::initialize(m_repositoryPath);
}
void TestIndexedString::cleanupTestCase()
{
ItemRepositoryRegistry::deleteRepositoryFromDisk(m_repositoryPath);
}
void TestIndexedString::testUrl_data()
{
QTest::addColumn<QUrl>("url");
......
......@@ -7,18 +7,14 @@
#ifndef TESTINDEXEDSTRING_H
#define TESTINDEXEDSTRING_H
#include <QDir>
#include <QObject>
#include "itemrepositorytestbase.h"
class TestIndexedString
: public QObject
: public ItemRepositoryTestBase
{
Q_OBJECT
private Q_SLOTS:
void initTestCase();
void cleanupTestCase();
void testUrl_data();
void testUrl();
......@@ -30,9 +26,6 @@ private Q_SLOTS:
void testSwap_data();
void testCString();
private:
const QString m_repositoryPath = QDir::tempPath() + QStringLiteral("/test_indexedstring");
};
#endif // TESTINDEXEDSTRING_H
#include <QObject>
#include "itemrepositorytestbase.h"
#include <QTest>
#include <QStandardPaths>
#include <serialization/itemrepository.h>
#include <serialization/indexedstring.h>
#include <cstdlib>
......@@ -112,21 +112,11 @@ TestItem* createItem(uint id, uint size)
///@todo Add a test where the complete content is deleted again, and make sure the result has a nice structure
///@todo More consistency and lost-space tests, especially about monster-buckets. Make sure their space is re-claimed
class TestItemRepository
: public QObject
: public ItemRepositoryTestBase
{
Q_OBJECT
private Q_SLOTS:
void initTestCase()
{
QStandardPaths::setTestModeEnabled(true);
ItemRepositoryRegistry::initialize(m_repositoryPath);
}
void cleanupTestCase()
{
ItemRepositoryRegistry::deleteRepositoryFromDisk(m_repositoryPath);
}
void testItemRepository()
{
ItemRepository<TestItem, TestItemRequest> repository(QStringLiteral("TestItemRepository"));
......@@ -386,9 +376,6 @@ private Q_SLOTS:
* be done correctly using only Bucket::hasClashingItem as of now.
*/
}
private:
const QString m_repositoryPath = QDir::tempPath() + QStringLiteral("/test_itemrepository");
};
#include "test_itemrepository.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