summaryrefslogtreecommitdiffstats
path: root/kdecore/tests/kmacroexpandertest.cpp
blob: 6c92ba935c41d43e2827106492463469a68f5a7a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#include <kmacroexpander.h>

#include <kapplication.h>
#include <kcmdlineargs.h>
#include <kdebug.h>

#include <stdlib.h>

bool check(QString txt, QString s, QString a, QString b)
{
  if (a.isEmpty())
     a = QString::null;
  if (b.isEmpty())
     b = QString::null;
  if (a == b)
    kdDebug() << txt << " (" << s << ") : '" << a << "' - ok" << endl;
  else {
    kdDebug() << txt << " (" << s << ") : got '" << a << "' but expected '" << b << "' - KO!" << endl;
    exit(1);
  }
  return true;
}

class MyCExpander : public KCharMacroExpander {
public:
  MyCExpander() : KCharMacroExpander() {}
protected:
  bool expandMacro(QChar chr, QStringList &ret)
  {
    if (chr == 'm') {
      ret = QString("expanded");
      return true;
    }
    return false;
  }
};

class MyWExpander : public KWordMacroExpander {
public:
  MyWExpander() : KWordMacroExpander() {}
protected:
  bool expandMacro(const QString &str, QStringList &ret)
  {
    if (str == "macro") {
      ret = QString("expanded");
      return true;
    }
    return false;
  }
};

int main(int argc, char *argv[])
{
  KCmdLineArgs::init(argc, argv, ":", "", "", "");
  KApplication app(false,false);
  QString s, s2;

  QMap<QChar,QStringList> map1;
  map1.insert('n', "Restaurant \"Chew It\"");
  QStringList li;
  li << "element1" << "'element2'";
  map1.insert('l', li);

  s = "text %l %n text";
  check( "KMacroExpander::expandMacros", s, KMacroExpander::expandMacros(s, map1), "text element1 'element2' Restaurant \"Chew It\" text");
  check( "KMacroExpander::expandMacrosShellQuote", s, KMacroExpander::expandMacrosShellQuote(s, map1), "text 'element1' ''\\''element2'\\''' 'Restaurant \"Chew It\"' text");
  s = "text \"%l %n\" text";
  check( "KMacroExpander::expandMacrosShellQuote", s, KMacroExpander::expandMacrosShellQuote(s, map1), "text \"element1 'element2' Restaurant \\\"Chew It\\\"\" text");

  QMap<QChar,QString> map;
  map.insert('a', "%n");
  map.insert('f', "filename.txt");
  map.insert('u', "http://www.kde.org/index.html");
  map.insert('n', "Restaurant \"Chew It\"");

  s = "Title: %a - %f - %u - %n - %%";
  check( "KMacroExpander::expandMacros", s, KMacroExpander::expandMacros(s, map), "Title: %n - filename.txt - http://www.kde.org/index.html - Restaurant \"Chew It\" - %");

  s = "kedit --caption %n %f";
  check( "KMacroExpander::expandMacrosShellQuote", s, KMacroExpander::expandMacrosShellQuote(s, map), "kedit --caption 'Restaurant \"Chew It\"' 'filename.txt'");

  map.replace('n', "Restaurant 'Chew It'");
  s = "kedit --caption %n %f";
  check( "KMacroExpander::expandMacrosShellQuote", s, KMacroExpander::expandMacrosShellQuote(s, map), "kedit --caption 'Restaurant '\\''Chew It'\\''' 'filename.txt'");

  s = "kedit --caption \"%n\" %f";
  check( "KMacroExpander::expandMacrosShellQuote", s, KMacroExpander::expandMacrosShellQuote(s, map), "kedit --caption \"Restaurant 'Chew It'\" 'filename.txt'");

  map.replace('n', "Restaurant \"Chew It\"");
  s = "kedit --caption \"%n\" %f";
  check( "KMacroExpander::expandMacrosShellQuote", s, KMacroExpander::expandMacrosShellQuote(s, map), "kedit --caption \"Restaurant \\\"Chew It\\\"\" 'filename.txt'");

  map.replace('n', "Restaurant $HOME");
  s = "kedit --caption \"%n\" %f";
  check( "KMacroExpander::expandMacrosShellQuote", s, KMacroExpander::expandMacrosShellQuote(s, map), "kedit --caption \"Restaurant \\$HOME\" 'filename.txt'");

  map.replace('n', "Restaurant `echo hello`");
  s = "kedit --caption \"%n\" %f";
  check( "KMacroExpander::expandMacrosShellQuote", s, KMacroExpander::expandMacrosShellQuote(s, map), "kedit --caption \"Restaurant \\`echo hello\\`\" 'filename.txt'");

  map.replace('n', "Restaurant `echo hello`");
  s = "kedit --caption \"`echo %n`\" %f";
  check( "KMacroExpander::expandMacrosShellQuote", s, KMacroExpander::expandMacrosShellQuote(s, map), "kedit --caption \"$( echo 'Restaurant `echo hello`')\" 'filename.txt'");

  QMap<QString,QString> smap;
  smap.insert("foo", "%n");
  smap.insert("file", "filename.txt");
  smap.insert("url", "http://www.kde.org/index.html");
  smap.insert("name", "Restaurant \"Chew It\"");

  s = "Title: %foo - %file - %url - %name - %";
  check( "KMacroExpander::expandMacros", s, KMacroExpander::expandMacros(s, smap), "Title: %n - filename.txt - http://www.kde.org/index.html - Restaurant \"Chew It\" - %");

  s = "Title: %{foo} - %{file} - %{url} - %{name} - %";
  check( "KMacroExpander::expandMacros", s, KMacroExpander::expandMacros(s, smap), "Title: %n - filename.txt - http://www.kde.org/index.html - Restaurant \"Chew It\" - %");

  s = "Title: %foo-%file-%url-%name-%";
  check( "KMacroExpander::expandMacros", s, KMacroExpander::expandMacros(s, smap), "Title: %n-filename.txt-http://www.kde.org/index.html-Restaurant \"Chew It\"-%");

  s = "Title: %{file} %{url";
  check( "KMacroExpander::expandMacros", s, KMacroExpander::expandMacros(s, smap), "Title: filename.txt %{url");

  MyCExpander mx1;
  s = "subst %m but not %n equ %%";
  s2 = s;
  mx1.expandMacros(s2);
  check( "MyCExpander::expandMacros", s, s2, "subst expanded but not %n equ %");

  MyWExpander mx2;
  s = "subst %macro but not %not equ %%";
  s2 = s;
  mx2.expandMacros(s2);
  check( "MyWExpander::expandMacros", s, s2, "subst expanded but not %not equ %");

  kdDebug() << endl << "Test OK!" << endl;
}