1 /*
2     SPDX-FileCopyrightText: 2017 Friedrich W. H. Kossebau <kossebau@kde.org>
3 
4     SPDX-License-Identifier: GPL-2.0-or-later
5 */
6 
7 #include "test_vcslocation.h"
8 
9 #include <QTest>
10 #include <QStandardPaths>
11 
12 using namespace KDevelop;
13 
initTestCase()14 void TestVcsLocation::initTestCase()
15 {
16     QStandardPaths::setTestModeEnabled(true);
17 }
18 
setServerLocation(VcsLocation & serverLocation,const QString & repositoryModule,const QString & repositoryBranch,const QString & repositoryTag,const QString & repositoryPath,const QVariant & userData)19 void TestVcsLocation::setServerLocation(VcsLocation& serverLocation,
20                                         const QString& repositoryModule,
21                                         const QString& repositoryBranch,
22                                         const QString& repositoryTag,
23                                         const QString& repositoryPath,
24                                         const QVariant& userData)
25 {
26     serverLocation.setRepositoryModule(repositoryModule);
27     serverLocation.setRepositoryBranch(repositoryBranch);
28     serverLocation.setRepositoryTag(repositoryTag);
29     serverLocation.setRepositoryPath(repositoryPath);
30     serverLocation.setUserData(userData);
31 }
32 
compareServerLocation(const VcsLocation & serverLocation,const QString & repositoryServer,const QString & repositoryModule,const QString & repositoryBranch,const QString & repositoryTag,const QString & repositoryPath,const QVariant & userData)33 void TestVcsLocation::compareServerLocation(const VcsLocation& serverLocation,
34                                             const QString& repositoryServer,
35                                             const QString& repositoryModule,
36                                             const QString& repositoryBranch,
37                                             const QString& repositoryTag,
38                                             const QString& repositoryPath,
39                                             const QVariant& userData)
40 {
41     QCOMPARE(serverLocation.isValid(), true);
42     QCOMPARE(serverLocation.type(), VcsLocation::RepositoryLocation);
43     QCOMPARE(serverLocation.repositoryServer(), repositoryServer);
44     QCOMPARE(serverLocation.repositoryModule(), repositoryModule);
45     QCOMPARE(serverLocation.repositoryBranch(), repositoryBranch);
46     QCOMPARE(serverLocation.repositoryTag(), repositoryTag);
47     QCOMPARE(serverLocation.repositoryPath(), repositoryPath);
48     QCOMPARE(serverLocation.userData(), userData);
49 }
50 
testDefaultConstructor()51 void TestVcsLocation::testDefaultConstructor()
52 {
53     const VcsLocation location;
54 
55     QCOMPARE(location.isValid(), false);
56 }
57 
testLocalUrlConstructor()58 void TestVcsLocation::testLocalUrlConstructor()
59 {
60     // valid
61     {
62         const QUrl localUrl = QUrl("file:///tmp/foo");
63 
64         const VcsLocation localLocation(localUrl);
65 
66         QCOMPARE(localLocation.isValid(), true);
67         QCOMPARE(localLocation.type(), VcsLocation::LocalLocation);
68         QCOMPARE(localLocation.localUrl(), localUrl);
69     }
70 
71     // invalid
72     {
73         const QUrl localUrl;
74 
75         const VcsLocation localLocation(localUrl);
76 
77         QCOMPARE(localLocation.isValid(), false);
78         QCOMPARE(localLocation.type(), VcsLocation::LocalLocation);
79         QCOMPARE(localLocation.localUrl(), localUrl);
80     }
81 }
82 
testRepositoryServerConstructor()83 void TestVcsLocation::testRepositoryServerConstructor()
84 {
85     // valid
86     {
87         const QString repositoryServer = QStringLiteral("server");
88         const QString repositoryModule = QStringLiteral("module");
89         const QString repositoryBranch = QStringLiteral("branch");
90         const QString repositoryTag = QStringLiteral("tag");
91         const QString repositoryPath = QStringLiteral("path");
92         const QVariant userData = QVariant(QStringLiteral("userdata"));
93 
94         VcsLocation serverLocation(repositoryServer);
95         setServerLocation(serverLocation,
96                           repositoryModule, repositoryBranch, repositoryTag, repositoryPath, userData);
97 
98         compareServerLocation(serverLocation,
99                               repositoryServer, repositoryModule, repositoryBranch, repositoryTag, repositoryPath,
100                               userData);
101     }
102 
103     // invalid
104     {
105         const QString repositoryServer;
106         VcsLocation serverLocation(repositoryServer);
107 
108         QCOMPARE(serverLocation.isValid(), false);
109         QCOMPARE(serverLocation.type(), VcsLocation::RepositoryLocation);
110         QCOMPARE(serverLocation.repositoryServer(), repositoryServer);
111     }
112 }
113 
testCopyConstructor()114 void TestVcsLocation::testCopyConstructor()
115 {
116     // test plain copy
117     const QString repositoryServer = QStringLiteral("server");
118     const QString repositoryModule = QStringLiteral("module");
119     const QString repositoryBranch = QStringLiteral("branch");
120     const QString repositoryTag = QStringLiteral("tag");
121     const QString repositoryPath = QStringLiteral("path");
122     const QVariant userData = QVariant(QStringLiteral("userdata"));
123 
124     {
125         VcsLocation serverLocationA(repositoryServer);
126         setServerLocation(serverLocationA,
127                           repositoryModule, repositoryBranch, repositoryTag, repositoryPath, userData);
128 
129         VcsLocation serverLocationB(serverLocationA);
130         compareServerLocation(serverLocationA,
131                               repositoryServer, repositoryModule, repositoryBranch, repositoryTag, repositoryPath,
132                               userData);
133         compareServerLocation(serverLocationB,
134                               repositoryServer, repositoryModule, repositoryBranch, repositoryTag, repositoryPath,
135                               userData);
136         QVERIFY(serverLocationB == serverLocationA);
137         QVERIFY(serverLocationA == serverLocationB);
138     }
139 
140     const QString repositoryServerNew = QStringLiteral("servernew");
141 
142     // test detach after changing A
143     {
144         VcsLocation serverLocationA(repositoryServer);
145         setServerLocation(serverLocationA,
146                           repositoryModule, repositoryBranch, repositoryTag, repositoryPath, userData);
147 
148         VcsLocation serverLocationB(serverLocationA);
149         // change a property of A
150         serverLocationA.setRepositoryServer(repositoryServerNew);
151 
152         compareServerLocation(serverLocationA,
153                               repositoryServerNew, repositoryModule, repositoryBranch, repositoryTag, repositoryPath,
154                               userData);
155         compareServerLocation(serverLocationB,
156                               repositoryServer,    repositoryModule, repositoryBranch, repositoryTag, repositoryPath,
157                               userData);
158         QVERIFY(!(serverLocationB == serverLocationA));
159         QVERIFY(!(serverLocationA == serverLocationB));
160     }
161 }
162 
testAssignOperator()163 void TestVcsLocation::testAssignOperator()
164 {
165     // test plain copy
166     const QString repositoryServer = QStringLiteral("server");
167     const QString repositoryModule = QStringLiteral("module");
168     const QString repositoryBranch = QStringLiteral("branch");
169     const QString repositoryTag = QStringLiteral("tag");
170     const QString repositoryPath = QStringLiteral("path");
171     const QVariant userData = QVariant(QStringLiteral("userdata"));
172 
173     {
174         VcsLocation serverLocationA(repositoryServer);
175         setServerLocation(serverLocationA,
176                           repositoryModule, repositoryBranch, repositoryTag, repositoryPath, userData);
177 
178         VcsLocation serverLocationB;
179         serverLocationB = serverLocationA;
180         compareServerLocation(serverLocationA,
181                               repositoryServer, repositoryModule, repositoryBranch, repositoryTag, repositoryPath,
182                               userData);
183         compareServerLocation(serverLocationB,
184                               repositoryServer, repositoryModule, repositoryBranch, repositoryTag, repositoryPath,
185                               userData);
186         QVERIFY(serverLocationB == serverLocationA);
187         QVERIFY(serverLocationA == serverLocationB);
188     }
189 
190     const QString repositoryServerNew = QStringLiteral("servernew");
191 
192     // test detach after changing A
193     {
194         VcsLocation serverLocationA(repositoryServer);
195         setServerLocation(serverLocationA,
196                           repositoryModule, repositoryBranch, repositoryTag, repositoryPath, userData);
197 
198         VcsLocation serverLocationB;
199         serverLocationB = serverLocationA;
200         // change a property of A
201         serverLocationA.setRepositoryServer(repositoryServerNew);
202 
203         compareServerLocation(serverLocationA,
204                               repositoryServerNew, repositoryModule, repositoryBranch, repositoryTag, repositoryPath,
205                               userData);
206         compareServerLocation(serverLocationB,
207                               repositoryServer,    repositoryModule, repositoryBranch, repositoryTag, repositoryPath,
208                               userData);
209         QVERIFY(!(serverLocationB == serverLocationA));
210         QVERIFY(!(serverLocationA == serverLocationB));
211     }
212 }
213 
214 QTEST_GUILESS_MAIN(TestVcsLocation)
215