File indexing completed on 2024-04-21 05:42:27

0001 // clang-format off
0002 /*
0003  * KDiff3 - Text Diff And Merge Tool
0004  *
0005  * SPDX-FileCopyrightText: 2021-2021 David Hallas <david@davidhallas.dk>
0006  * SPDX-License-Identifier: GPL-2.0-or-later
0007  */
0008 // clang-format on
0009 
0010 #include <QTest>
0011 #include <QtGlobal>
0012 
0013 #include "../GitIgnoreList.h"
0014 #include "../fileaccess.h"
0015 
0016 class GitIgnoreListStub final : public GitIgnoreList
0017 {
0018   public:
0019     QString m_fileContents;
0020     [[nodiscard]] QString readFile([[maybe_unused]] const QString& fileName) const final
0021     {
0022         return m_fileContents;
0023     }
0024 };
0025 
0026 class GitIgnoreListTest : public QObject
0027 {
0028     Q_OBJECT
0029   private Q_SLOTS:
0030     void matches()
0031     {
0032         const QString testDir("dir");
0033         DirectoryList directoryList;
0034         // Empty dir doesn't match anything
0035         {
0036             GitIgnoreListStub testObject;
0037             testObject.enterDir(testDir, directoryList);
0038             QVERIFY(testObject.matches(testDir, "foo", true) == false);
0039         }
0040         // Simple .gitignore file containing wild cards
0041         {
0042             FileAccess gitignoreFile(".gitignore");
0043             directoryList.push_back(gitignoreFile);
0044             GitIgnoreListStub testObject;
0045             testObject.m_fileContents = QString("foo\n*.cpp\n#comment");
0046             testObject.enterDir(testDir, directoryList);
0047             QVERIFY(testObject.matches(testDir, "foo", true) == true);
0048             QVERIFY(testObject.matches(testDir, "FOO", true) == false);
0049             QVERIFY(testObject.matches(testDir, "FOO", false) == true);
0050             QVERIFY(testObject.matches(testDir, "file.cpp", false) == true);
0051             QVERIFY(testObject.matches(testDir, "file.h", false) == false);
0052             QVERIFY(testObject.matches(testDir, "#comment", false) == false);
0053         }
0054         // Test that matches honors the directory and any subdirectories
0055         {
0056             const QString otherTestDir("other_dir");
0057             const QString testSubDir("dir/sub");
0058             FileAccess gitignoreFile(".gitignore");
0059             directoryList.push_back(gitignoreFile);
0060             GitIgnoreListStub testObject;
0061             testObject.m_fileContents = QString("foo");
0062             testObject.enterDir(testDir, directoryList);
0063             QVERIFY(testObject.matches(testDir, "foo", true) == true);
0064             QVERIFY(testObject.matches(testSubDir, "foo", true) == true);
0065             QVERIFY(testObject.matches(otherTestDir, "foo", true) == false);
0066         }
0067     }
0068 };
0069 
0070 QTEST_MAIN(GitIgnoreListTest);
0071 
0072 #include "GitIgnoreListTest.moc"