File indexing completed on 2024-06-23 10:37:17

0001 /*
0002     This file is part of the KIO framework tests
0003     SPDX-FileCopyrightText: 2022 Nicolas Fella <nicolas.fella@gmx.de>
0004 
0005     SPDX-License-Identifier: LGPL-2.0-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
0006 */
0007 
0008 #include "kfilefilter.h"
0009 
0010 #include <QMimeDatabase>
0011 #include <QTest>
0012 #include <qtestcase.h>
0013 #include <qvariant.h>
0014 
0015 class KFileFilterTest : public QObject
0016 {
0017     Q_OBJECT
0018 
0019 private Q_SLOTS:
0020     void testFromFilterString_data()
0021     {
0022         QTest::addColumn<QString>("filterString");
0023         QTest::addColumn<QVariantList>("expectedFilters");
0024 
0025         QVariantList cppFilters = {QVariant::fromValue(KFileFilter("C++ Source Files", {"*.cpp", "*.cc", "*.C"}, {})),
0026                                    QVariant::fromValue(KFileFilter("Header files", {"*.h", "*.H"}, {}))};
0027 
0028         QMimeDatabase mdb;
0029 
0030         QTest::addRow("empty") << "" << QVariantList{};
0031         QTest::addRow("cpp") << "*.cpp|Sources (*.cpp)" << QVariantList{QVariant::fromValue(KFileFilter("Sources (*.cpp)", {"*.cpp"}, {}))};
0032         QTest::addRow("cpp_headers") << "*.cpp *.cc *.C|C++ Source Files\n*.h *.H|Header files" << cppFilters;
0033         QTest::addRow("no_label") << "*.cpp" << QVariantList{QVariant::fromValue(KFileFilter("*.cpp", {"*.cpp"}, {}))};
0034         QTest::addRow("escaped_slash") << "*.cue|CUE\\/BIN Files (*.cue)"
0035                                        << QVariantList{QVariant::fromValue(KFileFilter("CUE/BIN Files (*.cue)", {"*.cue"}, {}))};
0036         QTest::addRow("single_mimetype") << "text/plain"
0037                                          << QVariantList{QVariant::fromValue(KFileFilter(mdb.mimeTypeForName("text/plain").comment(), {}, {"text/plain"}))};
0038 
0039         QVariantList multipleMimeFilters = {QVariant::fromValue(KFileFilter(mdb.mimeTypeForName("image/png").comment(), {}, {"image/png"})),
0040                                             QVariant::fromValue(KFileFilter(mdb.mimeTypeForName("image/jpeg").comment(), {}, {"image/jpeg"}))};
0041 
0042         QTest::addRow("multiple_mimetypes") << "image/png image/jpeg" << multipleMimeFilters;
0043         QTest::addRow("mimeglob") << "audio/*" << QVariantList{QVariant::fromValue(KFileFilter(QString(), {}, {"audio/*"}))};
0044     }
0045 
0046     void testFromFilterString()
0047     {
0048         QFETCH(QString, filterString);
0049         QFETCH(QVariantList, expectedFilters);
0050 
0051         const QVector<KFileFilter> filters = KFileFilter::fromFilterString(filterString);
0052 
0053         QCOMPARE(filters.size(), expectedFilters.size());
0054 
0055         for (int i = 0; i < filters.size(); ++i) {
0056             KFileFilter expectedFilter = expectedFilters[i].value<KFileFilter>();
0057             KFileFilter filter = filters[i];
0058 
0059             QCOMPARE(expectedFilter, filter);
0060         }
0061     }
0062 
0063     void testToFilterString_data()
0064     {
0065         QTest::addColumn<QVariant>("input");
0066         QTest::addColumn<QString>("expectedFilterString");
0067 
0068         QTest::addRow("single_mime") << QVariant::fromValue(KFileFilter("", {}, {"text/plain"})) << "text/plain";
0069         QTest::addRow("double_mime") << QVariant::fromValue(KFileFilter("", {}, {"text/plain", "image/png"})) << "text/plain image/png";
0070         QTest::addRow("cpp") << QVariant::fromValue(KFileFilter("C++ source files", {"*.cpp"}, {})) << "*.cpp|C++ source files";
0071         QTest::addRow("cpp_with_header") << QVariant::fromValue(KFileFilter("C++ files", {"*.cpp", "*.h"}, {})) << "*.cpp *.h|C++ files";
0072         QTest::addRow("no_label") << QVariant::fromValue(KFileFilter("", {"*.png"}, {})) << "*.png";
0073         QTest::addRow("duplicate_label") << QVariant::fromValue(KFileFilter("*.cpp", {"*.cpp"}, {})) << "*.cpp";
0074         QTest::addRow("slash_to_escape") << QVariant::fromValue(KFileFilter("VCS/ICS calendar", {"*.ical"}, {})) << "*.ical|VCS\\/ICS calendar";
0075     }
0076 
0077     void testToFilterString()
0078     {
0079         QFETCH(QVariant, input);
0080         QFETCH(QString, expectedFilterString);
0081 
0082         QCOMPARE(input.value<KFileFilter>().toFilterString(), expectedFilterString);
0083     }
0084 };
0085 Q_DECLARE_METATYPE(KFileFilter);
0086 
0087 QTEST_MAIN(KFileFilterTest)
0088 
0089 #include "filefiltertest.moc"