File indexing completed on 2024-03-24 03:56:26
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(QStringLiteral("expanded")) 0034 { 0035 } 0036 0037 protected: 0038 bool expandMacro(QChar ch, QStringList &ret) override 0039 { 0040 if (ch == QLatin1Char('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(QStringLiteral("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 << QStringLiteral("Restaurant \"Chew It\""); 0077 map.insert(QLatin1Char('n'), list); 0078 list.clear(); 0079 list << QStringLiteral("element1") << QStringLiteral("'element2'"); 0080 map.insert(QLatin1Char('l'), list); 0081 0082 s = QStringLiteral("%% text %l text %n"); 0083 QCOMPARE(KMacroExpander::expandMacros(s, map), QLatin1String("% text element1 'element2' text Restaurant \"Chew It\"")); 0084 s = QStringLiteral("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(QLatin1Char('a'), QStringLiteral("%n")); 0089 map2.insert(QLatin1Char('f'), QStringLiteral("filename.txt")); 0090 map2.insert(QLatin1Char('u'), QStringLiteral("https://www.kde.org/index.html")); 0091 map2.insert(QLatin1Char('n'), QStringLiteral("Restaurant \"Chew It\"")); 0092 s = QStringLiteral("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(QStringLiteral("foo"), QStringLiteral("%n")); 0097 smap.insert(QStringLiteral("file"), QStringLiteral("filename.txt")); 0098 smap.insert(QStringLiteral("url"), QStringLiteral("https://www.kde.org/index.html")); 0099 smap.insert(QStringLiteral("name"), QStringLiteral("Restaurant \"Chew It\"")); 0100 0101 s = QStringLiteral("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 = QStringLiteral("%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 = QStringLiteral("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 = QStringLiteral("%{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 = QStringLiteral("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 = QStringLiteral("Title: %{file} %{url"); 0115 QCOMPARE(KMacroExpander::expandMacros(s, smap), QLatin1String("Title: filename.txt %{url")); 0116 0117 s = QStringLiteral(" * 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 << QStringLiteral("Restaurant \"Chew It\""); 0129 map.insert(QLatin1Char('n'), list); 0130 list.clear(); 0131 list << QStringLiteral("element1") << QStringLiteral("'element2'") << QStringLiteral("\"element3\""); 0132 map.insert(QLatin1Char('l'), list); 0133 0134 #ifdef Q_OS_WIN 0135 s = QStringLiteral("text %l %n text"); 0136 QCOMPARE(KMacroExpander::expandMacrosShellQuote(s, map), 0137 QLatin1String("text element1 'element2' \\^\"element3\\^\" \"Restaurant \"\\^\"\"Chew It\"\\^\" text")); 0138 0139 s = QStringLiteral("text \"%l %n\" text"); 0140 QCOMPARE(KMacroExpander::expandMacrosShellQuote(s, map), 0141 QLatin1String("text \"element1 'element2' \"\\^\"\"element3\"\\^\"\" Restaurant \"\\^\"\"Chew It\"\\^\"\"\" text")); 0142 #else 0143 s = QStringLiteral("text %l %n text"); 0144 QCOMPARE(KMacroExpander::expandMacrosShellQuote(s, map), QLatin1String("text element1 ''\\''element2'\\''' '\"element3\"' 'Restaurant \"Chew It\"' text")); 0145 0146 s = QStringLiteral("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(QLatin1Char('a'), QStringLiteral("%n")); 0152 map2.insert(QLatin1Char('f'), QStringLiteral("filename.txt")); 0153 map2.insert(QLatin1Char('u'), QStringLiteral("https://www.kde.org/index.html")); 0154 map2.insert(QLatin1Char('n'), QStringLiteral("Restaurant \"Chew It\"")); 0155 0156 #ifdef Q_OS_WIN 0157 s = QStringLiteral("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 = QStringLiteral("kedit --caption %n %f"); 0164 map2.insert(QLatin1Char('n'), QStringLiteral("Restaurant 'Chew It'")); 0165 QCOMPARE(KMacroExpander::expandMacrosShellQuote(s, map2), QLatin1String("kedit --caption \"Restaurant 'Chew It'\" filename.txt")); 0166 0167 s = QStringLiteral("kedit --caption \"%n\" %f"); 0168 QCOMPARE(KMacroExpander::expandMacrosShellQuote(s, map2), QLatin1String("kedit --caption \"Restaurant 'Chew It'\" filename.txt")); 0169 0170 map2.insert(QLatin1Char('n'), QStringLiteral("Restaurant \"Chew It\"")); 0171 QCOMPARE(KMacroExpander::expandMacrosShellQuote(s, map2), QLatin1String("kedit --caption \"Restaurant \"\\^\"\"Chew It\"\\^\"\"\" filename.txt")); 0172 0173 map2.insert(QLatin1Char('n'), QStringLiteral("Restaurant %HOME%")); 0174 QCOMPARE(KMacroExpander::expandMacrosShellQuote(s, map2), QLatin1String("kedit --caption \"Restaurant %PERCENT_SIGN%HOME%PERCENT_SIGN%\" filename.txt")); 0175 0176 s = QStringLiteral("kedit c:\\%f"); 0177 QCOMPARE(KMacroExpander::expandMacrosShellQuote(s, map2), QLatin1String("kedit c:\\filename.txt")); 0178 0179 s = QStringLiteral("kedit \"c:\\%f\""); 0180 QCOMPARE(KMacroExpander::expandMacrosShellQuote(s, map2), QLatin1String("kedit \"c:\\filename.txt\"")); 0181 0182 map2.insert(QLatin1Char('f'), QStringLiteral("\"filename.txt\"")); 0183 QCOMPARE(KMacroExpander::expandMacrosShellQuote(s, map2), QLatin1String("kedit \"c:\\\\\"\\^\"\"filename.txt\"\\^\"\"\"")); 0184 0185 map2.insert(QLatin1Char('f'), QStringLiteral("path\\")); 0186 QCOMPARE(KMacroExpander::expandMacrosShellQuote(s, map2), QLatin1String("kedit \"c:\\path\\\\\"\"\"")); 0187 #else 0188 s = QStringLiteral("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 = QStringLiteral("kedit --caption %n %f"); 0193 map2.insert(QLatin1Char('n'), QStringLiteral("Restaurant 'Chew It'")); 0194 QCOMPARE(KMacroExpander::expandMacrosShellQuote(s, map2), QLatin1String("kedit --caption 'Restaurant '\\''Chew It'\\''' filename.txt")); 0195 0196 s = QStringLiteral("kedit --caption \"%n\" %f"); 0197 QCOMPARE(KMacroExpander::expandMacrosShellQuote(s, map2), QLatin1String("kedit --caption \"Restaurant 'Chew It'\" filename.txt")); 0198 0199 map2.insert(QLatin1Char('n'), QStringLiteral("Restaurant \"Chew It\"")); 0200 QCOMPARE(KMacroExpander::expandMacrosShellQuote(s, map2), QLatin1String("kedit --caption \"Restaurant \\\"Chew It\\\"\" filename.txt")); 0201 0202 map2.insert(QLatin1Char('n'), QStringLiteral("Restaurant $HOME")); 0203 QCOMPARE(KMacroExpander::expandMacrosShellQuote(s, map2), QLatin1String("kedit --caption \"Restaurant \\$HOME\" filename.txt")); 0204 0205 map2.insert(QLatin1Char('n'), QStringLiteral("Restaurant `echo hello`")); 0206 QCOMPARE(KMacroExpander::expandMacrosShellQuote(s, map2), QLatin1String("kedit --caption \"Restaurant \\`echo hello\\`\" filename.txt")); 0207 0208 s = QStringLiteral("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 = QStringLiteral("( 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 = QStringLiteral("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 = QStringLiteral("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"