File indexing completed on 2024-05-12 12:02:18
0001 /* 0002 SPDX-FileCopyrightText: 2016 Volker Krause <vkrause@kde.org> 0003 SPDX-FileCopyrightText: 2021 Igor Kushnir <igorkuo@gmail.com> 0004 0005 SPDX-License-Identifier: MIT 0006 */ 0007 0008 #include "repository_test_base.h" 0009 0010 #include "test-config.h" 0011 0012 #include <KSyntaxHighlighting/Definition> 0013 0014 #include <QStandardPaths> 0015 #include <QTest> 0016 0017 namespace 0018 { 0019 constexpr const QChar definitionSeparator = QLatin1Char{'\n'}; 0020 struct DefinitionDataRow { 0021 const char *dataTag; 0022 0023 const char *m_inputString; 0024 const char *m_definitionName; 0025 0026 QString inputString() const 0027 { 0028 return QString::fromUtf8(m_inputString); 0029 } 0030 QString firstDefinitionName() const 0031 { 0032 return QString::fromUtf8(m_definitionName).section(definitionSeparator, 0, 0); 0033 } 0034 QStringList definitionNames() const 0035 { 0036 return m_definitionName[0] == 0 ? QStringList{} : QString::fromUtf8(m_definitionName).split(definitionSeparator); 0037 } 0038 }; 0039 0040 // The two tables below have been copied to ktexteditor:autotests/src/katemodemanager_test_base.cpp and adjusted. 0041 // The two versions of the tables should be kept in sync. 0042 0043 constexpr DefinitionDataRow definitionsForFileNames[] = { 0044 {"empty", "", ""}, 0045 0046 {"*.tar.gz", "noMatch.tar.gz", ""}, 0047 {"No match", "a_random#filename", ""}, 0048 {"Long path, no match", "/this/is/a/moderately/long/path/to/no-match", ""}, 0049 {"Prefix in dir name", "Kconfig/no_match", ""}, 0050 0051 {"*.qml", "/bla/foo.qml", "QML"}, 0052 {"*.frag", "flat.frag", "GLSL"}, 0053 {"*.md", "highPriority.md", "Markdown"}, 0054 {"*.octave", "lowPriority.octave", "Octave"}, 0055 {"*.hats", "sameLastLetterPattern.hats", "ATS"}, 0056 0057 {"*.c", "test.c", "C\nANSI C89\nLPC"}, 0058 {"*.fs", "test.fs", "FSharp\nANS-Forth94\nGLSL"}, 0059 {"*.m", "/bla/foo.m", "Objective-C\nMagma\nMatlab\nOctave"}, 0060 0061 {"Makefile", "Makefile", "Makefile"}, 0062 {"Path to Makefile", "/some/path/to/Makefile", "Makefile"}, 0063 {"Makefile.*", "Makefile.am", "Makefile"}, 0064 0065 {"not-Makefile.dic", "not-Makefile.dic", "Hunspell Dictionary File"}, 0066 {"*qmakefile.cpp", "test_qmakefile.cpp", "C++\nISO C++\nGCCExtensions\nSystemC"}, 0067 {"*_makefile.mm", "bench_makefile.mm", "Objective-C++\nMetamath"}, 0068 0069 {"xorg.conf", "/etc/literal-pattern/xorg.conf", "x.org Configuration\nTOML"}, 0070 {".profile", "2-literal-patterns/.profile", "Bash\nZsh"}, 0071 0072 {"Config.*", "Config.beginning", "Kconfig"}, 0073 {"usr.libexec.*", "usr.libexec.", "AppArmor Security Profile"}, 0074 {"Jam*", "Jam-beginning-no-dot", "Jam"}, 0075 {"usr.li-*.ch", "usr.li-many-partial-prefix-matches.ch", "xHarbour\nClipper"}, 0076 {"QRPG*.*", "QRPG1u4[+.unusual", "ILERPG"}, 0077 0078 {"*patch", "no-dot-before-ending~patch", "Diff"}, 0079 {"*.cmake.in", "two-dots-after-asterisk.cmake.in", "CMake"}, 0080 {"*.html.mst", "two-dots-priority!=0.html.mst", "Mustache/Handlebars (HTML)"}, 0081 0082 {"*.desktop.cmake", "2_suffixes.desktop.cmake", ".desktop\nCMake"}, 0083 {"*.per.err", "2_suffixes-but-one-a-better-match.per.err", "4GL\n4GL-PER"}, 0084 {"*.xml.eex", "2_suffixes-one-lang.xml.eex", "Elixir"}, 0085 {"fishd.*.fish", "fishd.prefix,suffix=one-lang.fish", "Fish"}, 0086 0087 {"usr.bin.*.ftl", "usr.bin.heterogenousPatternMatch.ftl", "AppArmor Security Profile\nFTL\nFluent"}, 0088 {"Doxyfile.*.pro", "Doxyfile.heterogenous.Pattern-Match.pro", "QMake\nDoxyfile\nProlog\nRSI IDL"}, 0089 {"Kconfig*.ml", "KconfigHeterogenous_pattern_match.ml", "Objective Caml\nSML\nKconfig"}, 0090 {"snap-confine.*.html.rac", "snap-confine.2.-higher-priority.html.rac", "Mustache/Handlebars (HTML)\nAppArmor Security Profile"}, 0091 {"file_contexts_*.fq.gz", "file_contexts_prefix-higher-priority.fq.gz", "SELinux File Contexts\nFASTQ"}, 0092 {"QRPG*.ninja", "QRPG.ninja", "ILERPG\nNinja"}, 0093 {"qrpg*.tt", "qrpgTwoUnusualPatterns.tt", "ILERPG\nTT2"}, 0094 {"qrpg*.cl", "qrpg$heterogenous~pattern&match.cl", "OpenCL\nCommon Lisp\nILERPG"}, 0095 {".gitignore*.tt*.textile", ".gitignoreHeterogenous3.tt.textile", "Textile\nGit Ignore\nTT2"}, 0096 }; 0097 0098 constexpr DefinitionDataRow definitionsForMimeTypeNames[] = { 0099 {"empty", "", ""}, 0100 0101 {"Nonexistent MIME type", "text/nonexistent-mt", ""}, 0102 {"No match", "application/x-bzip-compressed-tar", ""}, 0103 0104 {"High priority", "text/rust", "Rust"}, 0105 {"Negative priority", "text/octave", "Octave"}, 0106 0107 {"Multiple types match", "text/x-chdr", "C++\nISO C++\nC\nGCCExtensions\nANSI C89\nSystemC"}, 0108 }; 0109 0110 template<std::size_t size> 0111 void addFirstDefinitionDataRows(const DefinitionDataRow (&array)[size]) 0112 { 0113 for (const auto &row : array) { 0114 QTest::newRow(row.dataTag) << row.inputString() << row.firstDefinitionName(); 0115 } 0116 } 0117 template<std::size_t size> 0118 void addDefinitionsDataRows(const DefinitionDataRow (&array)[size]) 0119 { 0120 for (const auto &row : array) { 0121 QTest::newRow(row.dataTag) << row.inputString() << row.definitionNames(); 0122 } 0123 } 0124 0125 void verifyDefinition(const KSyntaxHighlighting::Definition &definition, const QString &definitionName) 0126 { 0127 if (definitionName.isEmpty()) { 0128 QVERIFY(!definition.isValid()); 0129 } else { 0130 QVERIFY(definition.isValid()); 0131 QCOMPARE(definition.name(), definitionName); 0132 } 0133 } 0134 0135 void verifyDefinitionList(const QVector<KSyntaxHighlighting::Definition> &definitionList, const QStringList &definitionNames) 0136 { 0137 QStringList names; 0138 for (const auto &definition : definitionList) { 0139 names.push_back(definition.name()); 0140 } 0141 QCOMPARE(names, definitionNames); 0142 } 0143 } // unnamed namespace 0144 0145 void RepositoryTestBase::initTestCase() 0146 { 0147 QStandardPaths::setTestModeEnabled(true); 0148 initRepositorySearchPaths(m_repo); 0149 } 0150 0151 void RepositoryTestBase::definitionByExtensionTestData() 0152 { 0153 QTest::addColumn<QString>("fileName"); 0154 QTest::addColumn<QString>("definitionName"); 0155 addFirstDefinitionDataRows(definitionsForFileNames); 0156 } 0157 0158 void RepositoryTestBase::definitionByExtensionTest(const QString &fileName, const QString &definitionName) 0159 { 0160 verifyDefinition(m_repo.definitionForFileName(fileName), definitionName); 0161 } 0162 0163 void RepositoryTestBase::definitionsForFileNameTestData() 0164 { 0165 QTest::addColumn<QString>("fileName"); 0166 QTest::addColumn<QStringList>("definitionNames"); 0167 addDefinitionsDataRows(definitionsForFileNames); 0168 } 0169 0170 void RepositoryTestBase::definitionsForFileNameTest(const QString &fileName, const QStringList &definitionNames) 0171 { 0172 verifyDefinitionList(m_repo.definitionsForFileName(fileName), definitionNames); 0173 } 0174 0175 void RepositoryTestBase::definitionForMimeTypeTestData() 0176 { 0177 QTest::addColumn<QString>("mimeTypeName"); 0178 QTest::addColumn<QString>("definitionName"); 0179 addFirstDefinitionDataRows(definitionsForMimeTypeNames); 0180 } 0181 0182 void RepositoryTestBase::definitionForMimeTypeTest(const QString &mimeTypeName, const QString &definitionName) 0183 { 0184 verifyDefinition(m_repo.definitionForMimeType(mimeTypeName), definitionName); 0185 } 0186 0187 void RepositoryTestBase::definitionsForMimeTypeTestData() 0188 { 0189 QTest::addColumn<QString>("mimeTypeName"); 0190 QTest::addColumn<QStringList>("definitionNames"); 0191 addDefinitionsDataRows(definitionsForMimeTypeNames); 0192 } 0193 0194 void RepositoryTestBase::definitionsForMimeTypeTest(const QString &mimeTypeName, const QStringList &definitionNames) 0195 { 0196 verifyDefinitionList(m_repo.definitionsForMimeType(mimeTypeName), definitionNames); 0197 } 0198 0199 #include "moc_repository_test_base.cpp"