File indexing completed on 2024-04-28 11:35:07

0001 /*
0002     This file is part of the KDE libraries
0003     SPDX-FileCopyrightText: 2003, 2008 Oswald Buddenhagen <ossi@kde.org>
0004     SPDX-FileCopyrightText: 2005 Thomas Braxton <brax108@cox.net>
0005 
0006     SPDX-License-Identifier: LGPL-2.0-only
0007 */
0008 
0009 #include <QTest>
0010 #include <kmacroexpander.h>
0011 
0012 #include <QHash>
0013 #include <QObject>
0014 
0015 class KMacroExpanderTest : public QObject
0016 {
0017     Q_OBJECT
0018 
0019 private Q_SLOTS:
0020     void expandMacros();
0021     void expandMacrosShellQuote();
0022     void expandMacrosShellQuoteParens();
0023     void expandMacrosSubClass();
0024 };
0025 
0026 class MyCExpander : public KCharMacroExpander
0027 {
0028     QString exp;
0029 
0030 public:
0031     MyCExpander()
0032         : KCharMacroExpander()
0033         , exp("expanded")
0034     {
0035     }
0036 
0037 protected:
0038     bool expandMacro(QChar ch, QStringList &ret) override
0039     {
0040         if (ch == 'm') {
0041             ret = QStringList(exp);
0042             return true;
0043         }
0044         return false;
0045     }
0046 };
0047 
0048 class MyWExpander : public KWordMacroExpander
0049 {
0050     QString exp;
0051 
0052 public:
0053     MyWExpander()
0054         : KWordMacroExpander()
0055         , exp("expanded")
0056     {
0057     }
0058 
0059 protected:
0060     bool expandMacro(const QString &str, QStringList &ret) override
0061     {
0062         if (str == QLatin1String("macro")) {
0063             ret = QStringList(exp);
0064             return true;
0065         }
0066         return false;
0067     }
0068 };
0069 
0070 void KMacroExpanderTest::expandMacros()
0071 {
0072     QHash<QChar, QStringList> map;
0073     QStringList list;
0074     QString s;
0075 
0076     list << QString("Restaurant \"Chew It\"");
0077     map.insert('n', list);
0078     list.clear();
0079     list << QString("element1") << QString("'element2'");
0080     map.insert('l', list);
0081 
0082     s = "%% text %l text %n";
0083     QCOMPARE(KMacroExpander::expandMacros(s, map), QLatin1String("% text element1 'element2' text Restaurant \"Chew It\""));
0084     s = "text \"%l %n\" text";
0085     QCOMPARE(KMacroExpander::expandMacros(s, map), QLatin1String("text \"element1 'element2' Restaurant \"Chew It\"\" text"));
0086 
0087     QHash<QChar, QString> map2;
0088     map2.insert('a', "%n");
0089     map2.insert('f', "filename.txt");
0090     map2.insert('u', "https://www.kde.org/index.html");
0091     map2.insert('n', "Restaurant \"Chew It\"");
0092     s = "Title: %a - %f - %u - %n - %%";
0093     QCOMPARE(KMacroExpander::expandMacros(s, map2), QLatin1String("Title: %n - filename.txt - https://www.kde.org/index.html - Restaurant \"Chew It\" - %"));
0094 
0095     QHash<QString, QString> smap;
0096     smap.insert("foo", "%n");
0097     smap.insert("file", "filename.txt");
0098     smap.insert("url", "https://www.kde.org/index.html");
0099     smap.insert("name", "Restaurant \"Chew It\"");
0100 
0101     s = "Title: %foo - %file - %url - %name - %";
0102     QCOMPARE(KMacroExpander::expandMacros(s, smap), QLatin1String("Title: %n - filename.txt - https://www.kde.org/index.html - Restaurant \"Chew It\" - %"));
0103     s = "%foo - %file - %url - %name";
0104     QCOMPARE(KMacroExpander::expandMacros(s, smap), QLatin1String("%n - filename.txt - https://www.kde.org/index.html - Restaurant \"Chew It\""));
0105 
0106     s = "Title: %{foo} - %{file} - %{url} - %{name} - %";
0107     QCOMPARE(KMacroExpander::expandMacros(s, smap), QLatin1String("Title: %n - filename.txt - https://www.kde.org/index.html - Restaurant \"Chew It\" - %"));
0108     s = "%{foo} - %{file} - %{url} - %{name}";
0109     QCOMPARE(KMacroExpander::expandMacros(s, smap), QLatin1String("%n - filename.txt - https://www.kde.org/index.html - Restaurant \"Chew It\""));
0110 
0111     s = "Title: %foo-%file-%url-%name-%";
0112     QCOMPARE(KMacroExpander::expandMacros(s, smap), QLatin1String("Title: %n-filename.txt-https://www.kde.org/index.html-Restaurant \"Chew It\"-%"));
0113 
0114     s = "Title: %{file} %{url";
0115     QCOMPARE(KMacroExpander::expandMacros(s, smap), QLatin1String("Title: filename.txt %{url"));
0116 
0117     s = " * Copyright (C) 2008 %{AUTHOR}";
0118     smap.clear();
0119     QCOMPARE(KMacroExpander::expandMacros(s, smap), QLatin1String(" * Copyright (C) 2008 %{AUTHOR}"));
0120 }
0121 
0122 void KMacroExpanderTest::expandMacrosShellQuote()
0123 {
0124     QHash<QChar, QStringList> map;
0125     QStringList list;
0126     QString s;
0127 
0128     list << QString("Restaurant \"Chew It\"");
0129     map.insert('n', list);
0130     list.clear();
0131     list << QString("element1") << QString("'element2'") << QString("\"element3\"");
0132     map.insert('l', list);
0133 
0134 #ifdef Q_OS_WIN
0135     s = "text %l %n text";
0136     QCOMPARE(KMacroExpander::expandMacrosShellQuote(s, map),
0137              QLatin1String("text element1 'element2' \\^\"element3\\^\" \"Restaurant \"\\^\"\"Chew It\"\\^\" text"));
0138 
0139     s = "text \"%l %n\" text";
0140     QCOMPARE(KMacroExpander::expandMacrosShellQuote(s, map),
0141              QLatin1String("text \"element1 'element2' \"\\^\"\"element3\"\\^\"\" Restaurant \"\\^\"\"Chew It\"\\^\"\"\" text"));
0142 #else
0143     s = "text %l %n text";
0144     QCOMPARE(KMacroExpander::expandMacrosShellQuote(s, map), QLatin1String("text element1 ''\\''element2'\\''' '\"element3\"' 'Restaurant \"Chew It\"' text"));
0145 
0146     s = "text \"%l %n\" text";
0147     QCOMPARE(KMacroExpander::expandMacrosShellQuote(s, map), QLatin1String("text \"element1 'element2' \\\"element3\\\" Restaurant \\\"Chew It\\\"\" text"));
0148 #endif
0149 
0150     QHash<QChar, QString> map2;
0151     map2.insert('a', "%n");
0152     map2.insert('f', "filename.txt");
0153     map2.insert('u', "https://www.kde.org/index.html");
0154     map2.insert('n', "Restaurant \"Chew It\"");
0155 
0156 #ifdef Q_OS_WIN
0157     s = "Title: %a - %f - %u - %n - %% - %VARIABLE% foo";
0158     QCOMPARE(
0159         KMacroExpander::expandMacrosShellQuote(s, map2),
0160         QLatin1String(
0161             "Title: %PERCENT_SIGN%n - filename.txt - https://www.kde.org/index.html - \"Restaurant \"\\^\"\"Chew It\"\\^\" - %PERCENT_SIGN% - %VARIABLE% foo"));
0162 
0163     s = "kedit --caption %n %f";
0164     map2.insert('n', "Restaurant 'Chew It'");
0165     QCOMPARE(KMacroExpander::expandMacrosShellQuote(s, map2), QLatin1String("kedit --caption \"Restaurant 'Chew It'\" filename.txt"));
0166 
0167     s = "kedit --caption \"%n\" %f";
0168     QCOMPARE(KMacroExpander::expandMacrosShellQuote(s, map2), QLatin1String("kedit --caption \"Restaurant 'Chew It'\" filename.txt"));
0169 
0170     map2.insert('n', "Restaurant \"Chew It\"");
0171     QCOMPARE(KMacroExpander::expandMacrosShellQuote(s, map2), QLatin1String("kedit --caption \"Restaurant \"\\^\"\"Chew It\"\\^\"\"\" filename.txt"));
0172 
0173     map2.insert('n', "Restaurant %HOME%");
0174     QCOMPARE(KMacroExpander::expandMacrosShellQuote(s, map2), QLatin1String("kedit --caption \"Restaurant %PERCENT_SIGN%HOME%PERCENT_SIGN%\" filename.txt"));
0175 
0176     s = "kedit c:\\%f";
0177     QCOMPARE(KMacroExpander::expandMacrosShellQuote(s, map2), QLatin1String("kedit c:\\filename.txt"));
0178 
0179     s = "kedit \"c:\\%f\"";
0180     QCOMPARE(KMacroExpander::expandMacrosShellQuote(s, map2), QLatin1String("kedit \"c:\\filename.txt\""));
0181 
0182     map2.insert('f', "\"filename.txt\"");
0183     QCOMPARE(KMacroExpander::expandMacrosShellQuote(s, map2), QLatin1String("kedit \"c:\\\\\"\\^\"\"filename.txt\"\\^\"\"\""));
0184 
0185     map2.insert('f', "path\\");
0186     QCOMPARE(KMacroExpander::expandMacrosShellQuote(s, map2), QLatin1String("kedit \"c:\\path\\\\\"\"\""));
0187 #else
0188     s = "Title: %a - %f - %u - %n - %%";
0189     QCOMPARE(KMacroExpander::expandMacrosShellQuote(s, map2),
0190              QLatin1String("Title: %n - filename.txt - https://www.kde.org/index.html - 'Restaurant \"Chew It\"' - %"));
0191 
0192     s = "kedit --caption %n %f";
0193     map2.insert('n', "Restaurant 'Chew It'");
0194     QCOMPARE(KMacroExpander::expandMacrosShellQuote(s, map2), QLatin1String("kedit --caption 'Restaurant '\\''Chew It'\\''' filename.txt"));
0195 
0196     s = "kedit --caption \"%n\" %f";
0197     QCOMPARE(KMacroExpander::expandMacrosShellQuote(s, map2), QLatin1String("kedit --caption \"Restaurant 'Chew It'\" filename.txt"));
0198 
0199     map2.insert('n', "Restaurant \"Chew It\"");
0200     QCOMPARE(KMacroExpander::expandMacrosShellQuote(s, map2), QLatin1String("kedit --caption \"Restaurant \\\"Chew It\\\"\" filename.txt"));
0201 
0202     map2.insert('n', "Restaurant $HOME");
0203     QCOMPARE(KMacroExpander::expandMacrosShellQuote(s, map2), QLatin1String("kedit --caption \"Restaurant \\$HOME\" filename.txt"));
0204 
0205     map2.insert('n', "Restaurant `echo hello`");
0206     QCOMPARE(KMacroExpander::expandMacrosShellQuote(s, map2), QLatin1String("kedit --caption \"Restaurant \\`echo hello\\`\" filename.txt"));
0207 
0208     s = "kedit --caption \"`echo %n`\" %f";
0209     QCOMPARE(KMacroExpander::expandMacrosShellQuote(s, map2), QLatin1String("kedit --caption \"$( echo 'Restaurant `echo hello`')\" filename.txt"));
0210 #endif
0211 }
0212 
0213 class DummyMacroExpander : public KMacroExpanderBase
0214 {
0215 public:
0216     DummyMacroExpander()
0217         : KMacroExpanderBase(QChar(0x4567))
0218     {
0219     }
0220 
0221 protected:
0222     int expandPlainMacro(const QString &, int, QStringList &) override
0223     {
0224         return 0;
0225     }
0226     int expandEscapedMacro(const QString &, int, QStringList &) override
0227     {
0228         return 0;
0229     }
0230 };
0231 
0232 void KMacroExpanderTest::expandMacrosShellQuoteParens()
0233 {
0234     QString s;
0235 
0236     s = "( echo \"just testing (parens)\" ) ) after";
0237     int pos = 0;
0238     DummyMacroExpander kmx;
0239     QVERIFY(kmx.expandMacrosShellQuote(s, pos));
0240     QCOMPARE(s.mid(pos), QLatin1String(") after"));
0241     QVERIFY(!kmx.expandMacrosShellQuote(s));
0242 }
0243 
0244 void KMacroExpanderTest::expandMacrosSubClass()
0245 {
0246     QString s;
0247 
0248     MyCExpander mx1;
0249     s = "subst %m but not %n equ %%";
0250     mx1.expandMacros(s);
0251     QCOMPARE(s, QLatin1String("subst expanded but not %n equ %"));
0252 
0253     MyWExpander mx2;
0254     s = "subst %macro but not %not equ %%";
0255     mx2.expandMacros(s);
0256     QCOMPARE(s, QLatin1String("subst expanded but not %not equ %"));
0257 }
0258 
0259 QTEST_MAIN(KMacroExpanderTest)
0260 
0261 #include "kmacroexpandertest.moc"