diff --git a/kdevplatform/vcs/tests/CMakeLists.txt b/kdevplatform/vcs/tests/CMakeLists.txt index 3c213c534f..20c9492d42 100644 --- a/kdevplatform/vcs/tests/CMakeLists.txt +++ b/kdevplatform/vcs/tests/CMakeLists.txt @@ -1,27 +1,31 @@ ecm_add_test(test_vcsrevision LINK_LIBRARIES Qt5::Test KDev::Vcs ) ecm_add_test(test_vcsannotationline LINK_LIBRARIES Qt5::Test KDev::Vcs ) ecm_add_test(test_vcsannotation LINK_LIBRARIES Qt5::Test KDev::Vcs ) ecm_add_test(test_vcsitemevent LINK_LIBRARIES Qt5::Test KDev::Vcs ) ecm_add_test(test_vcsevent LINK_LIBRARIES Qt5::Test KDev::Vcs ) ecm_add_test(test_vcsstatusinfo LINK_LIBRARIES Qt5::Test KDev::Vcs ) 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.cpp b/kdevplatform/vcs/tests/test_vcslocation.cpp new file mode 100644 index 0000000000..0cc201728e --- /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/tests/test_vcslocation.h b/kdevplatform/vcs/tests/test_vcslocation.h new file mode 100644 index 0000000000..46139c03ac --- /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/vcslocation.cpp b/kdevplatform/vcs/vcslocation.cpp index 3662fb2156..5a0f50b041 100644 --- a/kdevplatform/vcs/vcslocation.cpp +++ b/kdevplatform/vcs/vcslocation.cpp @@ -1,204 +1,187 @@ /*************************************************************************** * This file is part of KDevelop * * Copyright 2007 Andreas Pakulat * * * * This program is free software; you can redistribute it and/or modify * * it under the terms of the GNU Library 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 Library 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 "vcslocation.h" #include namespace KDevelop { -class VcsLocationPrivate +class VcsLocationPrivate : public QSharedData { public: QUrl m_localUrl; QString m_repoServer; QString m_repoPath; QString m_repoModule; QString m_repoBranch; QString m_repoTag; VcsLocation::LocationType m_type; QVariant m_userData; }; VcsLocation::VcsLocation() : d(new VcsLocationPrivate) { d->m_type = VcsLocation::LocalLocation; } VcsLocation::VcsLocation( const QUrl& u ) : d(new VcsLocationPrivate) { setLocalUrl( u ); } VcsLocation::VcsLocation( const QString& s ) : d(new VcsLocationPrivate) { setRepositoryServer( s ); } 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; } QUrl VcsLocation::localUrl() const { return d->m_localUrl; } QString VcsLocation::repositoryServer() const { return d->m_repoServer; } VcsLocation::LocationType VcsLocation::type() const { return d->m_type; } bool VcsLocation::isValid() const { return( ( d->m_localUrl.isValid() && d->m_type == VcsLocation::LocalLocation ) || ( !d->m_repoServer.isEmpty() && d->m_type == VcsLocation::RepositoryLocation ) ); } void VcsLocation::setLocalUrl( const QUrl& url ) { d->m_repoServer.clear(); d->m_repoModule.clear(); d->m_repoBranch.clear(); d->m_repoTag.clear(); d->m_repoPath.clear(); d->m_type = VcsLocation::LocalLocation; d->m_localUrl = url; } void VcsLocation::setRepositoryServer( const QString& location ) { d->m_repoServer = location; d->m_type = VcsLocation::RepositoryLocation; d->m_localUrl = QUrl(); } bool VcsLocation::operator==( const KDevelop::VcsLocation& rhs ) { return( type() == rhs.type() && repositoryServer() == rhs.repositoryServer() && localUrl() == rhs.localUrl() && repositoryPath() == rhs.repositoryPath() && repositoryModule() == rhs.repositoryModule() && repositoryBranch() == rhs.repositoryBranch() && repositoryTag() == rhs.repositoryTag() && userData() == rhs.userData() ); } QString VcsLocation::repositoryModule( ) const { return d->m_repoModule; } QString VcsLocation::repositoryTag( ) const { return d->m_repoTag; } QString VcsLocation::repositoryBranch( ) const { return d->m_repoBranch; } QString VcsLocation::repositoryPath( ) const { return d->m_repoPath; } void VcsLocation::setRepositoryModule( const QString & module ) { d->m_repoModule = module; d->m_type = VcsLocation::RepositoryLocation; d->m_localUrl.clear(); } void VcsLocation::setRepositoryBranch( const QString & branch ) { d->m_repoBranch = branch; d->m_type = VcsLocation::RepositoryLocation; d->m_localUrl.clear(); } void VcsLocation::setRepositoryTag( const QString & tag ) { d->m_repoTag = tag; d->m_type = VcsLocation::RepositoryLocation; d->m_localUrl.clear(); } void VcsLocation::setRepositoryPath( const QString & path ) { d->m_repoPath = path; d->m_type = VcsLocation::RepositoryLocation; d->m_localUrl.clear(); } QVariant VcsLocation::userData( ) const { return d->m_userData; } void VcsLocation::setUserData( const QVariant& data ) { d->m_type = VcsLocation::RepositoryLocation; d->m_localUrl.clear(); d->m_userData = data; } } diff --git a/kdevplatform/vcs/vcslocation.h b/kdevplatform/vcs/vcslocation.h index 6e518d5d1a..a83463a50b 100644 --- a/kdevplatform/vcs/vcslocation.h +++ b/kdevplatform/vcs/vcslocation.h @@ -1,164 +1,165 @@ /* This file is part of KDevelop * * Copyright 2007 Andreas Pakulat * Copyright 2007 Matthew Woehlke * * 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_VCSLOCATION_H #define KDEVPLATFORM_VCSLOCATION_H #include #include #include -#include +#include class QVariant; #include "vcsexport.h" namespace KDevelop { /** * Denotes a local or repository location for a Vcs system. * * For the RepositoryLocation type, most of the information * is vcs-specific. */ class KDEVPLATFORMVCS_EXPORT VcsLocation { public: enum LocationType { LocalLocation = 0 /**< this is a local location */, RepositoryLocation = 1 /**< this is a repository location */ }; VcsLocation(); explicit VcsLocation( const QUrl& ); explicit VcsLocation( const QString& ); ~VcsLocation(); VcsLocation( const VcsLocation& ); VcsLocation& operator=( const VcsLocation& ); /** * @returns Local url if this location is a LocalLocation */ QUrl localUrl() const; /** * Returns a string for the repository, usually this identifies the server. * @returns a vcs-implementation-specific string identifying the server */ QString repositoryServer() const; /** * Returns the module or module path inside the server. * @returns a vcs-implementation-specific string identifying the module */ QString repositoryModule() const; /** * Identifies the tag which this location belongs to. * @returns a vcs-implementation-specific string identifying the tag */ QString repositoryTag() const; /** * Identifies the branch to which this location belongs to. * @returns a vcs-implementation-specific string identifying the branch */ QString repositoryBranch() const; /** * This can define a path relative to the module. This is used * when identifying a subdirectory or file inside a repository location * @returns a path relative to module */ QString repositoryPath() const; /** * @returns the type of this location */ LocationType type() const; /** * Set the local url for this location, automatically sets the type to LocalLocation * @param url the local url */ void setLocalUrl( const QUrl& url ); /** * Set the server string for this location, automatically sets the type to RepositoryLocation */ void setRepositoryServer( const QString& ); /** * Set the module for this location, automatically sets the type to RepositoryLocation */ void setRepositoryModule( const QString& ); /** * Set the branch string for this location, automatically sets the type to RepositoryLocation */ void setRepositoryBranch( const QString& ); /** * Set the tag string for this location, automatically sets the type to RepositoryLocation */ void setRepositoryTag( const QString& ); /** * Set the path for this location, automatically sets the type to RepositoryLocation */ void setRepositoryPath( const QString& ); /** * Allows to add vcs-specific data to this location. * Automatically sets the type to RepositoryLocation * @param data the vcs-specific data */ void setUserData( const QVariant& data ); /** * retrieve vcs-specific data */ QVariant userData() const; bool operator==( const KDevelop::VcsLocation& ); bool isValid() const; private: - const QScopedPointer d; + QSharedDataPointer d; }; inline uint qHash( const KDevelop::VcsLocation& loc ) { if( loc.type() == KDevelop::VcsLocation::LocalLocation ) { return qHash(loc.localUrl()); }else { return qHash(loc.repositoryServer()); } } inline bool operator==( const KDevelop::VcsLocation& lhs, const KDevelop::VcsLocation& rhs ) { return( lhs.type() == rhs.type() && lhs.repositoryServer() == rhs.repositoryServer() && lhs.localUrl() == rhs.localUrl() ); } } Q_DECLARE_METATYPE( KDevelop::VcsLocation ) +Q_DECLARE_TYPEINFO(KDevelop::VcsLocation, Q_MOVABLE_TYPE); #endif