File indexing completed on 2024-05-12 04:38:54
0001 /* 0002 SPDX-FileCopyrightText: 2017 Friedrich W. H. Kossebau <kossebau@kde.org> 0003 0004 SPDX-License-Identifier: GPL-2.0-or-later 0005 */ 0006 0007 #include "test_vcsdiff.h" 0008 0009 #include <QTest> 0010 #include <QStandardPaths> 0011 0012 #include <vcs/vcslocation.h> 0013 0014 using namespace KDevelop; 0015 0016 void TestVcsDiff::initTestCase() 0017 { 0018 QStandardPaths::setTestModeEnabled(true); 0019 QString path = QFINDTESTDATA("sample.diff"); 0020 QFile in(path); 0021 QCOMPARE(in.open(QIODevice::ReadOnly), true); 0022 sampleDiff = QString::fromUtf8(in.readAll()); 0023 QCOMPARE(sampleDiff.length(), 6235); 0024 } 0025 0026 void TestVcsDiff::setDiff(VcsDiff& diff, 0027 const QString& diffString, 0028 const QUrl& baseDiff, 0029 uint depth) 0030 { 0031 diff.setDiff(diffString); 0032 diff.setBaseDiff(baseDiff); 0033 diff.setDepth(depth); 0034 } 0035 0036 void TestVcsDiff::compareDiff(const VcsDiff& diff, 0037 const QString& diffString, 0038 const QUrl& baseDiff, 0039 uint depth) 0040 { 0041 QCOMPARE(diff.diff(), diffString); 0042 QCOMPARE(diff.baseDiff(), baseDiff); 0043 QCOMPARE(diff.depth(), depth); 0044 } 0045 0046 void TestVcsDiff::testFileNames() 0047 { 0048 VcsDiff diff; 0049 diff.setDiff(sampleDiff); 0050 const QVector<VcsDiff::FilePair> expected = { 0051 {"kdevplatform/vcs/vcsdiff.cpp", "kdevplatform/vcs/vcsdiff.cpp"}, 0052 {"kdevplatform/vcs/vcsdiff.h", "kdevplatform/vcs/vcsdiff.h"} 0053 }; 0054 QCOMPARE(diff.fileNames(), expected); 0055 } 0056 0057 void TestVcsDiff::testSubDiff() 0058 { 0059 /************************** 0060 * Test a real world diff * 0061 **************************/ 0062 VcsDiff diff; 0063 diff.setDiff(sampleDiff); 0064 QString expected(R"diff(--- a/kdevplatform/vcs/vcsdiff.cpp 0065 +++ b/kdevplatform/vcs/vcsdiff.cpp 0066 @@ -39,7 +39,7 @@ public: 0067 , oldCount 0068 , newStart 0069 , newCount 0070 - , firstLineIdx 0071 + , firstLineIdx /**< The 0-based line number (in the whole diff) of the hunk header line (the one starting with `@@`) */ 0072 ; 0073 QString srcFile /**< The source filename */ 0074 , tgtFile /**< The target filename */ 0075 )diff"); 0076 const auto subHunkDiffs = { 0077 diff.subDiffHunk(15), 0078 }; 0079 for(auto df: subHunkDiffs) { 0080 QVERIFY(df.fileNames().count()>0); 0081 QCOMPARE(df.fileNames().front().source, "kdevplatform/vcs/vcsdiff.cpp"); 0082 QCOMPARE(df.diff(), expected); 0083 } 0084 0085 VcsDiff subHunkDiff = diff.subDiff(15, 50); 0086 QVERIFY(subHunkDiff.fileNames().count()>0); 0087 QCOMPARE(subHunkDiff.fileNames().front().source, "kdevplatform/vcs/vcsdiff.cpp"); 0088 0089 /********************************* 0090 * Test start offset computation * 0091 *********************************/ 0092 VcsDiff skipDiff; 0093 skipDiff.setDiff(R"diff(--- a/skip 0094 +++ b/skip 0095 @@ -1,2 +3,2 @@ heading 0096 + increase offset 0097 0098 + dont increase offset 0099 - deletion 0100 - second deletion 0101 )diff"); 0102 expected = QStringLiteral(R"diff(--- a/skip 0103 +++ b/skip 0104 @@ -2,3 +4,2 @@ heading 0105 0106 - deletion 0107 second deletion 0108 )diff"); 0109 auto subDiff = skipDiff.subDiff(6,6); 0110 QCOMPARE(subDiff.diff(), expected); 0111 0112 } 0113 0114 void TestVcsDiff::testConflicts() 0115 { 0116 VcsDiff conflictDiff; 0117 conflictDiff.setDiff(R"diff(--- a/skip 0118 +++ b/skip 0119 @@ -1,2 +3,2 @@ conflict 0120 + increase offset 3 2 0121 4 0 3 0122 + dont increase offset 5 4 0123 - deletion 6 1 0124 <<<<<<< HEAD 7 0125 blablabla 8 2 0126 ======= 9 0127 yadayadayada 10 5 0128 >>>>>>> commit 11 0129 - second deletion 12 3 0130 )diff"); 0131 QCOMPARE(conflictDiff.diffLineToSource(8).line, 2); 0132 QCOMPARE(conflictDiff.diffLineToTarget(10).line, 5); 0133 QCOMPARE(conflictDiff.diffLineToSource(7).line, -1); 0134 QCOMPARE(conflictDiff.diffLineToSource(9).line, -1); 0135 QCOMPARE(conflictDiff.diffLineToSource(11).line, -1); 0136 } 0137 0138 0139 void TestVcsDiff::testLineMapping() 0140 { 0141 VcsDiff diff; 0142 diff.setDiff(sampleDiff); 0143 0144 auto src = QStringLiteral("kdevplatform/vcs/vcsdiff.cpp"); 0145 auto tgt = QStringLiteral("kdevplatform/vcs/vcsdiff.h"); 0146 QCOMPARE(diff.diffLineToSource(15-1).path, src); 0147 QCOMPARE(diff.diffLineToTarget(147-1).path, tgt); 0148 0149 // We have no way to map the headings 0150 QCOMPARE(diff.diffLineToSource(169-1).line, -1); 0151 QCOMPARE(diff.diffLineToTarget(169-1).line, -1); 0152 0153 QCOMPARE(diff.diffLineToSource(15-1).line, 42-1); 0154 QCOMPARE(diff.diffLineToTarget(15-1).line, -1); 0155 0156 QCOMPARE(diff.diffLineToTarget(147-1).line, 180-1); 0157 QCOMPARE(diff.diffLineToSource(147-1).line, 150-1); 0158 } 0159 0160 0161 void TestVcsDiff::testCopyConstructor() 0162 { 0163 // test plain copy 0164 const QString diffString("diff"); 0165 const QUrl baseDiff("git://1"); 0166 const uint depth = 1; 0167 const VcsLocation location("server"); 0168 0169 { 0170 VcsDiff diffA; 0171 setDiff(diffA, 0172 diffString, baseDiff, depth); 0173 0174 VcsDiff diffB(diffA); 0175 compareDiff(diffA, 0176 diffString, baseDiff, depth); 0177 compareDiff(diffB, 0178 diffString, baseDiff, depth); 0179 } 0180 0181 const QString diffStringNew("diffNew"); 0182 0183 // test detach after changing A 0184 { 0185 VcsDiff diffA; 0186 setDiff(diffA, 0187 diffString, baseDiff, depth); 0188 0189 VcsDiff diffB(diffA); 0190 // change a property of A 0191 diffA.setDiff(diffStringNew); 0192 0193 compareDiff(diffA, 0194 diffStringNew, baseDiff, depth); 0195 compareDiff(diffB, 0196 diffString, baseDiff, depth); 0197 } 0198 } 0199 0200 void TestVcsDiff::testAssignOperator() 0201 { 0202 // test plain copy 0203 const QString diffString("diff"); 0204 const QUrl baseDiff("git://1"); 0205 const uint depth = 1; 0206 const VcsLocation location("server"); 0207 0208 { 0209 VcsDiff diffA; 0210 setDiff(diffA, 0211 diffString, baseDiff, depth); 0212 0213 VcsDiff diffB; 0214 diffB = diffA; 0215 compareDiff(diffA, 0216 diffString, baseDiff, depth); 0217 compareDiff(diffB, 0218 diffString, baseDiff, depth); 0219 } 0220 0221 const QString diffStringNew("diffNew"); 0222 0223 // test detach after changing A 0224 { 0225 VcsDiff diffA; 0226 setDiff(diffA, 0227 diffString, baseDiff, depth); 0228 0229 VcsDiff diffB; 0230 diffB = diffA; 0231 // change a property of A 0232 diffA.setDiff(diffStringNew); 0233 0234 compareDiff(diffA, 0235 diffStringNew, baseDiff, depth); 0236 compareDiff(diffB, 0237 diffString, baseDiff, depth); 0238 } 0239 } 0240 0241 QTEST_GUILESS_MAIN(TestVcsDiff) 0242 0243 #include "moc_test_vcsdiff.cpp"