kmacroexpandertest.cpp 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. /*
  2. This file is part of the KDE libraries
  3. SPDX-FileCopyrightText: 2003, 2008 Oswald Buddenhagen <ossi@kde.org>
  4. SPDX-FileCopyrightText: 2005 Thomas Braxton <brax108@cox.net>
  5. SPDX-License-Identifier: LGPL-2.0-only
  6. */
  7. #include <QTest>
  8. #include <kmacroexpander.h>
  9. #include <QHash>
  10. #include <QObject>
  11. class KMacroExpanderTest : public QObject
  12. {
  13. Q_OBJECT
  14. private Q_SLOTS:
  15. void expandMacros();
  16. void expandMacrosShellQuote();
  17. void expandMacrosShellQuoteParens();
  18. void expandMacrosSubClass();
  19. };
  20. class MyCExpander : public KCharMacroExpander
  21. {
  22. QString exp;
  23. public:
  24. MyCExpander()
  25. : KCharMacroExpander()
  26. , exp("expanded")
  27. {
  28. }
  29. protected:
  30. bool expandMacro(QChar ch, QStringList &ret) override
  31. {
  32. if (ch == 'm') {
  33. ret = QStringList(exp);
  34. return true;
  35. }
  36. return false;
  37. }
  38. };
  39. class MyWExpander : public KWordMacroExpander
  40. {
  41. QString exp;
  42. public:
  43. MyWExpander()
  44. : KWordMacroExpander()
  45. , exp("expanded")
  46. {
  47. }
  48. protected:
  49. bool expandMacro(const QString &str, QStringList &ret) override
  50. {
  51. if (str == QLatin1String("macro")) {
  52. ret = QStringList(exp);
  53. return true;
  54. }
  55. return false;
  56. }
  57. };
  58. void KMacroExpanderTest::expandMacros()
  59. {
  60. QHash<QChar, QStringList> map;
  61. QStringList list;
  62. QString s;
  63. list << QString("Restaurant \"Chew It\"");
  64. map.insert('n', list);
  65. list.clear();
  66. list << QString("element1") << QString("'element2'");
  67. map.insert('l', list);
  68. s = "%% text %l text %n";
  69. QCOMPARE(KMacroExpander::expandMacros(s, map), QLatin1String("% text element1 'element2' text Restaurant \"Chew It\""));
  70. s = "text \"%l %n\" text";
  71. QCOMPARE(KMacroExpander::expandMacros(s, map), QLatin1String("text \"element1 'element2' Restaurant \"Chew It\"\" text"));
  72. QHash<QChar, QString> map2;
  73. map2.insert('a', "%n");
  74. map2.insert('f', "filename.txt");
  75. map2.insert('u', "https://www.kde.org/index.html");
  76. map2.insert('n', "Restaurant \"Chew It\"");
  77. s = "Title: %a - %f - %u - %n - %%";
  78. QCOMPARE(KMacroExpander::expandMacros(s, map2), QLatin1String("Title: %n - filename.txt - https://www.kde.org/index.html - Restaurant \"Chew It\" - %"));
  79. QHash<QString, QString> smap;
  80. smap.insert("foo", "%n");
  81. smap.insert("file", "filename.txt");
  82. smap.insert("url", "https://www.kde.org/index.html");
  83. smap.insert("name", "Restaurant \"Chew It\"");
  84. s = "Title: %foo - %file - %url - %name - %";
  85. QCOMPARE(KMacroExpander::expandMacros(s, smap), QLatin1String("Title: %n - filename.txt - https://www.kde.org/index.html - Restaurant \"Chew It\" - %"));
  86. s = "%foo - %file - %url - %name";
  87. QCOMPARE(KMacroExpander::expandMacros(s, smap), QLatin1String("%n - filename.txt - https://www.kde.org/index.html - Restaurant \"Chew It\""));
  88. s = "Title: %{foo} - %{file} - %{url} - %{name} - %";
  89. QCOMPARE(KMacroExpander::expandMacros(s, smap), QLatin1String("Title: %n - filename.txt - https://www.kde.org/index.html - Restaurant \"Chew It\" - %"));
  90. s = "%{foo} - %{file} - %{url} - %{name}";
  91. QCOMPARE(KMacroExpander::expandMacros(s, smap), QLatin1String("%n - filename.txt - https://www.kde.org/index.html - Restaurant \"Chew It\""));
  92. s = "Title: %foo-%file-%url-%name-%";
  93. QCOMPARE(KMacroExpander::expandMacros(s, smap), QLatin1String("Title: %n-filename.txt-https://www.kde.org/index.html-Restaurant \"Chew It\"-%"));
  94. s = "Title: %{file} %{url";
  95. QCOMPARE(KMacroExpander::expandMacros(s, smap), QLatin1String("Title: filename.txt %{url"));
  96. s = " * Copyright (C) 2008 %{AUTHOR}";
  97. smap.clear();
  98. QCOMPARE(KMacroExpander::expandMacros(s, smap), QLatin1String(" * Copyright (C) 2008 %{AUTHOR}"));
  99. }
  100. void KMacroExpanderTest::expandMacrosShellQuote()
  101. {
  102. QHash<QChar, QStringList> map;
  103. QStringList list;
  104. QString s;
  105. list << QString("Restaurant \"Chew It\"");
  106. map.insert('n', list);
  107. list.clear();
  108. list << QString("element1") << QString("'element2'") << QString("\"element3\"");
  109. map.insert('l', list);
  110. #ifdef Q_OS_WIN
  111. s = "text %l %n text";
  112. QCOMPARE(KMacroExpander::expandMacrosShellQuote(s, map),
  113. QLatin1String("text element1 'element2' \\^\"element3\\^\" \"Restaurant \"\\^\"\"Chew It\"\\^\" text"));
  114. s = "text \"%l %n\" text";
  115. QCOMPARE(KMacroExpander::expandMacrosShellQuote(s, map),
  116. QLatin1String("text \"element1 'element2' \"\\^\"\"element3\"\\^\"\" Restaurant \"\\^\"\"Chew It\"\\^\"\"\" text"));
  117. #else
  118. s = "text %l %n text";
  119. QCOMPARE(KMacroExpander::expandMacrosShellQuote(s, map), QLatin1String("text element1 ''\\''element2'\\''' '\"element3\"' 'Restaurant \"Chew It\"' text"));
  120. s = "text \"%l %n\" text";
  121. QCOMPARE(KMacroExpander::expandMacrosShellQuote(s, map), QLatin1String("text \"element1 'element2' \\\"element3\\\" Restaurant \\\"Chew It\\\"\" text"));
  122. #endif
  123. QHash<QChar, QString> map2;
  124. map2.insert('a', "%n");
  125. map2.insert('f', "filename.txt");
  126. map2.insert('u', "https://www.kde.org/index.html");
  127. map2.insert('n', "Restaurant \"Chew It\"");
  128. #ifdef Q_OS_WIN
  129. s = "Title: %a - %f - %u - %n - %% - %VARIABLE% foo";
  130. QCOMPARE(
  131. KMacroExpander::expandMacrosShellQuote(s, map2),
  132. QLatin1String(
  133. "Title: %PERCENT_SIGN%n - filename.txt - https://www.kde.org/index.html - \"Restaurant \"\\^\"\"Chew It\"\\^\" - %PERCENT_SIGN% - %VARIABLE% foo"));
  134. s = "kedit --caption %n %f";
  135. map2.insert('n', "Restaurant 'Chew It'");
  136. QCOMPARE(KMacroExpander::expandMacrosShellQuote(s, map2), QLatin1String("kedit --caption \"Restaurant 'Chew It'\" filename.txt"));
  137. s = "kedit --caption \"%n\" %f";
  138. QCOMPARE(KMacroExpander::expandMacrosShellQuote(s, map2), QLatin1String("kedit --caption \"Restaurant 'Chew It'\" filename.txt"));
  139. map2.insert('n', "Restaurant \"Chew It\"");
  140. QCOMPARE(KMacroExpander::expandMacrosShellQuote(s, map2), QLatin1String("kedit --caption \"Restaurant \"\\^\"\"Chew It\"\\^\"\"\" filename.txt"));
  141. map2.insert('n', "Restaurant %HOME%");
  142. QCOMPARE(KMacroExpander::expandMacrosShellQuote(s, map2), QLatin1String("kedit --caption \"Restaurant %PERCENT_SIGN%HOME%PERCENT_SIGN%\" filename.txt"));
  143. s = "kedit c:\\%f";
  144. QCOMPARE(KMacroExpander::expandMacrosShellQuote(s, map2), QLatin1String("kedit c:\\filename.txt"));
  145. s = "kedit \"c:\\%f\"";
  146. QCOMPARE(KMacroExpander::expandMacrosShellQuote(s, map2), QLatin1String("kedit \"c:\\filename.txt\""));
  147. map2.insert('f', "\"filename.txt\"");
  148. QCOMPARE(KMacroExpander::expandMacrosShellQuote(s, map2), QLatin1String("kedit \"c:\\\\\"\\^\"\"filename.txt\"\\^\"\"\""));
  149. map2.insert('f', "path\\");
  150. QCOMPARE(KMacroExpander::expandMacrosShellQuote(s, map2), QLatin1String("kedit \"c:\\path\\\\\"\"\""));
  151. #else
  152. s = "Title: %a - %f - %u - %n - %%";
  153. QCOMPARE(KMacroExpander::expandMacrosShellQuote(s, map2),
  154. QLatin1String("Title: %n - filename.txt - https://www.kde.org/index.html - 'Restaurant \"Chew It\"' - %"));
  155. s = "kedit --caption %n %f";
  156. map2.insert('n', "Restaurant 'Chew It'");
  157. QCOMPARE(KMacroExpander::expandMacrosShellQuote(s, map2), QLatin1String("kedit --caption 'Restaurant '\\''Chew It'\\''' filename.txt"));
  158. s = "kedit --caption \"%n\" %f";
  159. QCOMPARE(KMacroExpander::expandMacrosShellQuote(s, map2), QLatin1String("kedit --caption \"Restaurant 'Chew It'\" filename.txt"));
  160. map2.insert('n', "Restaurant \"Chew It\"");
  161. QCOMPARE(KMacroExpander::expandMacrosShellQuote(s, map2), QLatin1String("kedit --caption \"Restaurant \\\"Chew It\\\"\" filename.txt"));
  162. map2.insert('n', "Restaurant $HOME");
  163. QCOMPARE(KMacroExpander::expandMacrosShellQuote(s, map2), QLatin1String("kedit --caption \"Restaurant \\$HOME\" filename.txt"));
  164. map2.insert('n', "Restaurant `echo hello`");
  165. QCOMPARE(KMacroExpander::expandMacrosShellQuote(s, map2), QLatin1String("kedit --caption \"Restaurant \\`echo hello\\`\" filename.txt"));
  166. s = "kedit --caption \"`echo %n`\" %f";
  167. QCOMPARE(KMacroExpander::expandMacrosShellQuote(s, map2), QLatin1String("kedit --caption \"$( echo 'Restaurant `echo hello`')\" filename.txt"));
  168. #endif
  169. }
  170. class DummyMacroExpander : public KMacroExpanderBase
  171. {
  172. public:
  173. DummyMacroExpander()
  174. : KMacroExpanderBase(QChar(0x4567))
  175. {
  176. }
  177. protected:
  178. int expandPlainMacro(const QString &, int, QStringList &) override
  179. {
  180. return 0;
  181. }
  182. int expandEscapedMacro(const QString &, int, QStringList &) override
  183. {
  184. return 0;
  185. }
  186. };
  187. void KMacroExpanderTest::expandMacrosShellQuoteParens()
  188. {
  189. QString s;
  190. s = "( echo \"just testing (parens)\" ) ) after";
  191. int pos = 0;
  192. DummyMacroExpander kmx;
  193. QVERIFY(kmx.expandMacrosShellQuote(s, pos));
  194. QCOMPARE(s.mid(pos), QLatin1String(") after"));
  195. QVERIFY(!kmx.expandMacrosShellQuote(s));
  196. }
  197. void KMacroExpanderTest::expandMacrosSubClass()
  198. {
  199. QString s;
  200. MyCExpander mx1;
  201. s = "subst %m but not %n equ %%";
  202. mx1.expandMacros(s);
  203. QCOMPARE(s, QLatin1String("subst expanded but not %n equ %"));
  204. MyWExpander mx2;
  205. s = "subst %macro but not %not equ %%";
  206. mx2.expandMacros(s);
  207. QCOMPARE(s, QLatin1String("subst expanded but not %not equ %"));
  208. }
  209. QTEST_MAIN(KMacroExpanderTest)
  210. #include "kmacroexpandertest.moc"