File indexing completed on 2024-05-05 17:51:18
0001 // clang-format off 0002 /* 0003 * This file is part of KDiff3 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 "../CompositeIgnoreList.h" 0011 0012 #include <QTest> 0013 #include <QtGlobal> 0014 0015 class IgnoreListMock final : public IgnoreList { 0016 public: 0017 mutable unsigned callCount = 0; 0018 bool match = false; 0019 void enterDir(const QString&, const DirectoryList&) final {} 0020 [[nodiscard]] bool matches([[maybe_unused]] const QString& dir, [[maybe_unused]] const QString& text, [[maybe_unused]] bool bCaseSensitive) const final 0021 { 0022 ++callCount; 0023 return match; 0024 } 0025 }; 0026 0027 class CompositeIgnoreListTest : public QObject 0028 { 0029 Q_OBJECT 0030 private Q_SLOTS: 0031 void testMatches() 0032 { 0033 CompositeIgnoreList testObject; 0034 // Verify that with no ignore list handlers added, matches returns false 0035 QVERIFY(testObject.matches(QString(), QString(), false) == false); 0036 auto firstIgnoreList = std::make_unique<IgnoreListMock>(); 0037 auto firstIgnoreListPtr = firstIgnoreList.get(); 0038 testObject.addIgnoreList(std::move(firstIgnoreList)); 0039 // Verify that the added ignore list is called, and the return value of CompositeIgnoreList is that 0040 // of the added ignore list class 0041 QVERIFY(testObject.matches(QString(), QString(), false) == firstIgnoreListPtr->match); 0042 QVERIFY(firstIgnoreListPtr->callCount == 1); 0043 firstIgnoreListPtr->match = true; 0044 QVERIFY(testObject.matches(QString(), QString(), false) == firstIgnoreListPtr->match); 0045 QVERIFY(firstIgnoreListPtr->callCount == 2); 0046 // Verify that CompositeIgnoreList calls each added ignore list class and returns true the first time 0047 // a match is made 0048 auto secondIgnoreList = std::make_unique<IgnoreListMock>(); 0049 auto secondIgnoreListPtr = secondIgnoreList.get(); 0050 testObject.addIgnoreList(std::move(secondIgnoreList)); 0051 auto thirdIgnoreList = std::make_unique<IgnoreListMock>(); 0052 auto thirdIgnoreListPtr = thirdIgnoreList.get(); 0053 testObject.addIgnoreList(std::move(thirdIgnoreList)); 0054 firstIgnoreListPtr->callCount = 0; 0055 firstIgnoreListPtr->match = false; 0056 secondIgnoreListPtr->match = true; 0057 thirdIgnoreListPtr->match = false; 0058 QVERIFY(testObject.matches(QString(), QString(), false) == true); 0059 QVERIFY(firstIgnoreListPtr->callCount == 1); 0060 QVERIFY(secondIgnoreListPtr->callCount == 1); 0061 QVERIFY(thirdIgnoreListPtr->callCount == 0); 0062 } 0063 }; 0064 0065 QTEST_MAIN(CompositeIgnoreListTest); 0066 0067 #include "CompositeIgnoreListTest.moc"