diff --git a/kdevplatform/vcs/tests/CMakeLists.txt b/kdevplatform/vcs/tests/CMakeLists.txt --- a/kdevplatform/vcs/tests/CMakeLists.txt +++ b/kdevplatform/vcs/tests/CMakeLists.txt @@ -25,3 +25,7 @@ ecm_add_test(test_vcsdiff LINK_LIBRARIES Qt5::Test KDev::Vcs ) + +ecm_add_test(test_vcslocation + LINK_LIBRARIES Qt5::Test KDev::Vcs +) diff --git a/kdevplatform/vcs/tests/test_vcslocation.h b/kdevplatform/vcs/tests/test_vcslocation.h new file mode 100644 --- /dev/null +++ b/kdevplatform/vcs/tests/test_vcslocation.h @@ -0,0 +1,56 @@ +/* This file is part of KDevelop + * + * Copyright 2017 Friedrich W. H. Kossebau + * + * 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) any later version. + * + * 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, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA + * 02110-1301, USA. + */ + +#ifndef KDEVPLATFORM_TESTVCSLOCATION_H +#define KDEVPLATFORM_TESTVCSLOCATION_H + +#include + +#include "vcslocation.h" + +class TestVcsLocation : public QObject +{ + Q_OBJECT + +private Q_SLOTS: + void testDefaultConstructor(); + void testLocalUrlConstructor(); + void testRepositoryServerConstructor(); + + void testCopyConstructor(); + void testAssignOperator(); + +private: + void setServerLocation(KDevelop::VcsLocation& serverLocation, + const QString& repositoryModule, + const QString& repositoryBranch, + const QString& repositoryTag, + const QString& repositoryPath, + const QVariant& userData); + void compareServerLocation(const KDevelop::VcsLocation& serverLocation, + const QString& repositoryServer, + const QString& repositoryModule, + const QString& repositoryBranch, + const QString& repositoryTag, + const QString& repositoryPath, + const QVariant& userData); +}; + +#endif // KDEVPLATFORM_TESTVCSLOCATION_H diff --git a/kdevplatform/vcs/tests/test_vcslocation.cpp b/kdevplatform/vcs/tests/test_vcslocation.cpp new file mode 100644 --- /dev/null +++ b/kdevplatform/vcs/tests/test_vcslocation.cpp @@ -0,0 +1,222 @@ +/* This file is part of KDevelop + * + * Copyright 2017 Friedrich W. H. Kossebau + * + * 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) any later version. + * + * 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, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA + * 02110-1301, USA. + */ + +#include "test_vcslocation.h" + +#include + +using namespace KDevelop; + +void TestVcsLocation::setServerLocation(VcsLocation& serverLocation, + const QString& repositoryModule, + const QString& repositoryBranch, + const QString& repositoryTag, + const QString& repositoryPath, + const QVariant& userData) +{ + serverLocation.setRepositoryModule(repositoryModule); + serverLocation.setRepositoryBranch(repositoryBranch); + serverLocation.setRepositoryTag(repositoryTag); + serverLocation.setRepositoryPath(repositoryPath); + serverLocation.setUserData(userData); +} + +void TestVcsLocation::compareServerLocation(const VcsLocation& serverLocation, + const QString& repositoryServer, + const QString& repositoryModule, + const QString& repositoryBranch, + const QString& repositoryTag, + const QString& repositoryPath, + const QVariant& userData) +{ + QCOMPARE(serverLocation.isValid(), true); + QCOMPARE(serverLocation.type(), VcsLocation::RepositoryLocation); + QCOMPARE(serverLocation.repositoryServer(), repositoryServer); + QCOMPARE(serverLocation.repositoryModule(), repositoryModule); + QCOMPARE(serverLocation.repositoryBranch(), repositoryBranch); + QCOMPARE(serverLocation.repositoryTag(), repositoryTag); + QCOMPARE(serverLocation.repositoryPath(), repositoryPath); + QCOMPARE(serverLocation.userData(), userData); +} + +void TestVcsLocation::testDefaultConstructor() +{ + const VcsLocation location; + + QCOMPARE(location.isValid(), false); +} + +void TestVcsLocation::testLocalUrlConstructor() +{ + // valid + { + const QUrl localUrl = QUrl("file:///tmp/foo"); + + const VcsLocation localLocation(localUrl); + + QCOMPARE(localLocation.isValid(), true); + QCOMPARE(localLocation.type(), VcsLocation::LocalLocation); + QCOMPARE(localLocation.localUrl(), localUrl); + } + + // invalid + { + const QUrl localUrl; + + const VcsLocation localLocation(localUrl); + + QCOMPARE(localLocation.isValid(), false); + QCOMPARE(localLocation.type(), VcsLocation::LocalLocation); + QCOMPARE(localLocation.localUrl(), localUrl); + } +} + +void TestVcsLocation::testRepositoryServerConstructor() +{ + // valid + { + const QString repositoryServer = QStringLiteral("server"); + const QString repositoryModule = QStringLiteral("module"); + const QString repositoryBranch = QStringLiteral("branch"); + const QString repositoryTag = QStringLiteral("tag"); + const QString repositoryPath = QStringLiteral("path"); + const QVariant userData = QVariant(QStringLiteral("userdata")); + + VcsLocation serverLocation(repositoryServer); + setServerLocation(serverLocation, + repositoryModule, repositoryBranch, repositoryTag, repositoryPath, userData); + + compareServerLocation(serverLocation, + repositoryServer, repositoryModule, repositoryBranch, repositoryTag, repositoryPath, + userData); + } + + // invalid + { + const QString repositoryServer; + VcsLocation serverLocation(repositoryServer); + + QCOMPARE(serverLocation.isValid(), false); + QCOMPARE(serverLocation.type(), VcsLocation::RepositoryLocation); + QCOMPARE(serverLocation.repositoryServer(), repositoryServer); + } +} + +void TestVcsLocation::testCopyConstructor() +{ + // test plain copy + const QString repositoryServer = QStringLiteral("server"); + const QString repositoryModule = QStringLiteral("module"); + const QString repositoryBranch = QStringLiteral("branch"); + const QString repositoryTag = QStringLiteral("tag"); + const QString repositoryPath = QStringLiteral("path"); + const QVariant userData = QVariant(QStringLiteral("userdata")); + + { + VcsLocation serverLocationA(repositoryServer); + setServerLocation(serverLocationA, + repositoryModule, repositoryBranch, repositoryTag, repositoryPath, userData); + + VcsLocation serverLocationB(serverLocationA); + compareServerLocation(serverLocationA, + repositoryServer, repositoryModule, repositoryBranch, repositoryTag, repositoryPath, + userData); + compareServerLocation(serverLocationB, + repositoryServer, repositoryModule, repositoryBranch, repositoryTag, repositoryPath, + userData); + QVERIFY(serverLocationB == serverLocationA); + QVERIFY(serverLocationA == serverLocationB); + } + + const QString repositoryServerNew = QStringLiteral("servernew"); + + // test detach after changing A + { + VcsLocation serverLocationA(repositoryServer); + setServerLocation(serverLocationA, + repositoryModule, repositoryBranch, repositoryTag, repositoryPath, userData); + + VcsLocation serverLocationB(serverLocationA); + // change a property of A + serverLocationA.setRepositoryServer(repositoryServerNew); + + compareServerLocation(serverLocationA, + repositoryServerNew, repositoryModule, repositoryBranch, repositoryTag, repositoryPath, + userData); + compareServerLocation(serverLocationB, + repositoryServer, repositoryModule, repositoryBranch, repositoryTag, repositoryPath, + userData); + QVERIFY(!(serverLocationB == serverLocationA)); + QVERIFY(!(serverLocationA == serverLocationB)); + } +} + +void TestVcsLocation::testAssignOperator() +{ + // test plain copy + const QString repositoryServer = QStringLiteral("server"); + const QString repositoryModule = QStringLiteral("module"); + const QString repositoryBranch = QStringLiteral("branch"); + const QString repositoryTag = QStringLiteral("tag"); + const QString repositoryPath = QStringLiteral("path"); + const QVariant userData = QVariant(QStringLiteral("userdata")); + + { + VcsLocation serverLocationA(repositoryServer); + setServerLocation(serverLocationA, + repositoryModule, repositoryBranch, repositoryTag, repositoryPath, userData); + + VcsLocation serverLocationB; + serverLocationB = serverLocationA; + compareServerLocation(serverLocationA, + repositoryServer, repositoryModule, repositoryBranch, repositoryTag, repositoryPath, + userData); + compareServerLocation(serverLocationB, + repositoryServer, repositoryModule, repositoryBranch, repositoryTag, repositoryPath, + userData); + QVERIFY(serverLocationB == serverLocationA); + QVERIFY(serverLocationA == serverLocationB); + } + + const QString repositoryServerNew = QStringLiteral("servernew"); + + // test detach after changing A + { + VcsLocation serverLocationA(repositoryServer); + setServerLocation(serverLocationA, + repositoryModule, repositoryBranch, repositoryTag, repositoryPath, userData); + + VcsLocation serverLocationB; + serverLocationB = serverLocationA; + // change a property of A + serverLocationA.setRepositoryServer(repositoryServerNew); + + compareServerLocation(serverLocationA, + repositoryServerNew, repositoryModule, repositoryBranch, repositoryTag, repositoryPath, + userData); + compareServerLocation(serverLocationB, + repositoryServer, repositoryModule, repositoryBranch, repositoryTag, repositoryPath, + userData); + QVERIFY(!(serverLocationB == serverLocationA)); + QVERIFY(!(serverLocationA == serverLocationB)); + } +} + +QTEST_GUILESS_MAIN(TestVcsLocation); diff --git a/kdevplatform/vcs/vcslocation.h b/kdevplatform/vcs/vcslocation.h --- a/kdevplatform/vcs/vcslocation.h +++ b/kdevplatform/vcs/vcslocation.h @@ -25,7 +25,7 @@ #include #include #include -#include +#include class QVariant; @@ -135,7 +135,7 @@ bool isValid() const; private: - const QScopedPointer d; + QSharedDataPointer d; }; inline uint qHash( const KDevelop::VcsLocation& loc ) @@ -159,6 +159,7 @@ } Q_DECLARE_METATYPE( KDevelop::VcsLocation ) +Q_DECLARE_TYPEINFO(KDevelop::VcsLocation, Q_MOVABLE_TYPE); #endif diff --git a/kdevplatform/vcs/vcslocation.cpp b/kdevplatform/vcs/vcslocation.cpp --- a/kdevplatform/vcs/vcslocation.cpp +++ b/kdevplatform/vcs/vcslocation.cpp @@ -25,7 +25,7 @@ namespace KDevelop { -class VcsLocationPrivate +class VcsLocationPrivate : public QSharedData { public: QUrl m_localUrl; @@ -60,30 +60,13 @@ VcsLocation::~VcsLocation() = default; VcsLocation::VcsLocation( const VcsLocation& rhs ) - : d(new VcsLocationPrivate) + : d(rhs.d) { - d->m_type = rhs.d->m_type; - d->m_localUrl = rhs.d->m_localUrl; - d->m_repoServer = rhs.d->m_repoServer; - d->m_repoPath = rhs.d->m_repoPath; - d->m_repoModule = rhs.d->m_repoModule; - d->m_repoBranch = rhs.d->m_repoBranch; - d->m_repoTag = rhs.d->m_repoTag; - d->m_userData = rhs.d->m_userData; } VcsLocation& VcsLocation::operator=( const VcsLocation& rhs ) { - if( &rhs == this ) - return *this; - d->m_type = rhs.d->m_type; - d->m_localUrl = rhs.d->m_localUrl; - d->m_repoServer = rhs.d->m_repoServer; - d->m_repoPath = rhs.d->m_repoPath; - d->m_repoModule = rhs.d->m_repoModule; - d->m_repoBranch = rhs.d->m_repoBranch; - d->m_repoTag = rhs.d->m_repoTag; - d->m_userData = rhs.d->m_userData; + d = rhs.d; return *this; }