File indexing completed on 2024-10-06 06:42:54

0001 /*
0002     SPDX-FileCopyrightText: 2021 Igor Kushnir <igorkuo@gmail.com>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "katemodemanager_test_base.h"
0008 
0009 #include <kateglobal.h>
0010 
0011 #include <QString>
0012 #include <QTest>
0013 
0014 namespace
0015 {
0016 struct FileTypeDataRow {
0017     const char *dataTag;
0018     const char *m_inputString;
0019     const char *m_fileTypeName;
0020 
0021     QString inputString() const
0022     {
0023         return QString::fromUtf8(m_inputString);
0024     }
0025     QString fileTypeName() const
0026     {
0027         return QString::fromUtf8(m_fileTypeName);
0028     }
0029 };
0030 // The two tables below have been copied from syntax-highlighting:autotests/repository_test_base.cpp and adjusted:
0031 // removed all except first definition/fileType names because only the first name is used in KTextEditor.
0032 // The two versions of the tables should be kept in sync.
0033 
0034 // Additional adjustments to the syntax-highlighting version of fileTypesForFileNames table:
0035 //  1) used the second, not first, definition/fileType name in the QRPG.ninja row because
0036 //     "ILERPG" < "Ninja" in KSyntaxHighlighting but "Sources/ILERPG" > "Other/Ninja" in KTextEditor.
0037 //  2) used the second, not first, definition/fileType name in the qrpg*.tt row because
0038 //     "ILERPG" < "TT2" in KSyntaxHighlighting but "Sources/ILERPG" > "Markup/TT2" in KTextEditor.
0039 constexpr FileTypeDataRow fileTypesForFileNames[] = {
0040     {"empty", "", ""},
0041 
0042     {"*.tar.gz", "noMatch.tar.gz", ""},
0043     {"No match", "a_random#filename", ""},
0044     {"Long path, no match", "/this/is/a/moderately/long/path/to/no-match", ""},
0045     {"Prefix in dir name", "Kconfig/no_match", ""},
0046 
0047     {"*.qml", "/bla/foo.qml", "QML"},
0048     {"*.frag", "flat.frag", "GLSL"},
0049     {"*.md", "highPriority.md", "Markdown"},
0050     {"*.octave", "lowPriority.octave", "Octave"},
0051     {"*.hats", "sameLastLetterPattern.hats", "ATS"},
0052 
0053     {"*.c", "test.c", "C"},
0054     {"*.fs", "test.fs", "FSharp"},
0055     {"*.m", "/bla/foo.m", "Objective-C"},
0056 
0057     {"Makefile", "Makefile", "Makefile"},
0058     {"Path to Makefile", "/some/path/to/Makefile", "Makefile"},
0059     {"Makefile.*", "Makefile.am", "Makefile"},
0060 
0061     {"not-Makefile.dic", "not-Makefile.dic", "Hunspell Dictionary File"},
0062     {"*qmakefile.cpp", "test_qmakefile.cpp", "C++"},
0063     {"*_makefile.mm", "bench_makefile.mm", "Objective-C++"},
0064 
0065     {"xorg.conf", "/etc/literal-pattern/xorg.conf", "x.org Configuration"},
0066     {".profile", "2-literal-patterns/.profile", "Bash"},
0067 
0068     {"Config.*", "Config.beginning", "Kconfig"},
0069     {"usr.libexec.*", "usr.libexec.", "AppArmor Security Profile"},
0070     {"Jam*", "Jam-beginning-no-dot", "Jam"},
0071     {"usr.li-*.ch", "usr.li-many-partial-prefix-matches.ch", "xHarbour"},
0072     {"QRPG*.*", "QRPG1u4[+.unusual", "ILERPG"},
0073 
0074     {"*patch", "no-dot-before-ending~patch", "Diff"},
0075     {"*.cmake.in", "two-dots-after-asterisk.cmake.in", "CMake"},
0076     {"*.html.mst", "two-dots-priority!=0.html.mst", "Mustache/Handlebars (HTML)"},
0077 
0078     {"*.desktop.cmake", "2_suffixes.desktop.cmake", ".desktop"},
0079     {"*.per.err", "2_suffixes-but-one-a-better-match.per.err", "4GL"},
0080     {"*.xml.eex", "2_suffixes-one-lang.xml.eex", "Elixir"},
0081     {"fishd.*.fish", "fishd.prefix,suffix=one-lang.fish", "Fish"},
0082 
0083     {"usr.bin.*.ftl", "usr.bin.heterogenousPatternMatch.ftl", "AppArmor Security Profile"},
0084     {"Doxyfile.*.pro", "Doxyfile.heterogenous.Pattern-Match.pro", "QMake"},
0085     {"Kconfig*.ml", "KconfigHeterogenous_pattern_match.ml", "Objective Caml"},
0086     {"snap-confine.*.html.rac", "snap-confine.2.-higher-priority.html.rac", "Mustache/Handlebars (HTML)"},
0087     {"file_contexts_*.fq.gz", "file_contexts_prefix-higher-priority.fq.gz", "SELinux File Contexts"},
0088     {"QRPG*.ninja", "QRPG.ninja", "Ninja"},
0089     {"qrpg*.tt", "qrpgTwoUnusualPatterns.tt", "TT2"},
0090     {"qrpg*.cl", "qrpg$heterogenous~pattern&match.cl", "OpenCL"},
0091     {".gitignore*.tt*.textile", ".gitignoreHeterogenous3.tt.textile", "Textile"},
0092 };
0093 
0094 constexpr FileTypeDataRow fileTypesForMimeTypeNames[] = {
0095     {"empty", "", ""},
0096 
0097     {"Nonexistent MIME type", "text/nonexistent-mt", ""},
0098     {"No match", "application/x-bzip-compressed-tar", ""},
0099 
0100     {"High priority", "text/rust", "Rust"},
0101     {"Negative priority", "text/octave", "Octave"},
0102 
0103     {"Multiple types match", "text/x-chdr", "C++"},
0104 };
0105 
0106 template<std::size_t size>
0107 void addFileTypeDataRows(const FileTypeDataRow (&array)[size])
0108 {
0109     for (const auto &row : array) {
0110         QTest::newRow(row.dataTag) << row.inputString() << row.fileTypeName();
0111     }
0112 }
0113 } // unnamed namespace
0114 
0115 KateModeManagerTestBase::KateModeManagerTestBase()
0116     : m_modeManager{KTextEditor::EditorPrivate::self()->modeManager()}
0117 {
0118 }
0119 
0120 void KateModeManagerTestBase::wildcardsFindTestData()
0121 {
0122     QTest::addColumn<QString>("fileName");
0123     QTest::addColumn<QString>("fileTypeName");
0124     addFileTypeDataRows(fileTypesForFileNames);
0125 }
0126 
0127 void KateModeManagerTestBase::mimeTypesFindTestData()
0128 {
0129     QTest::addColumn<QString>("mimeTypeName");
0130     QTest::addColumn<QString>("fileTypeName");
0131     addFileTypeDataRows(fileTypesForMimeTypeNames);
0132 }
0133 
0134 #include "moc_katemodemanager_test_base.cpp"