You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
138 lines
5.2 KiB
138 lines
5.2 KiB
15 years ago
|
#include <kmacroexpander.h>
|
||
|
|
||
|
#include <kapplication.h>
|
||
|
#include <kcmdlineargs.h>
|
||
|
#include <kdebug.h>
|
||
|
|
||
|
#include <stdlib.h>
|
||
|
|
||
15 years ago
|
bool check(TQString txt, TQString s, TQString a, TQString b)
|
||
15 years ago
|
{
|
||
|
if (a.isEmpty())
|
||
15 years ago
|
a = TQString::null;
|
||
15 years ago
|
if (b.isEmpty())
|
||
15 years ago
|
b = TQString::null;
|
||
15 years ago
|
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:
|
||
15 years ago
|
bool expandMacro(TQChar chr, TQStringList &ret)
|
||
15 years ago
|
{
|
||
|
if (chr == 'm') {
|
||
15 years ago
|
ret = TQString("expanded");
|
||
15 years ago
|
return true;
|
||
|
}
|
||
|
return false;
|
||
|
}
|
||
|
};
|
||
|
|
||
|
class MyWExpander : public KWordMacroExpander {
|
||
|
public:
|
||
|
MyWExpander() : KWordMacroExpander() {}
|
||
|
protected:
|
||
15 years ago
|
bool expandMacro(const TQString &str, TQStringList &ret)
|
||
15 years ago
|
{
|
||
|
if (str == "macro") {
|
||
15 years ago
|
ret = TQString("expanded");
|
||
15 years ago
|
return true;
|
||
|
}
|
||
|
return false;
|
||
|
}
|
||
|
};
|
||
|
|
||
|
int main(int argc, char *argv[])
|
||
|
{
|
||
|
KCmdLineArgs::init(argc, argv, ":", "", "", "");
|
||
|
KApplication app(false,false);
|
||
15 years ago
|
TQString s, s2;
|
||
15 years ago
|
|
||
15 years ago
|
TQMap<TQChar,TQStringList> map1;
|
||
15 years ago
|
map1.insert('n', "Restaurant \"Chew It\"");
|
||
15 years ago
|
TQStringList li;
|
||
15 years ago
|
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");
|
||
|
|
||
15 years ago
|
TQMap<TQChar,TQString> map;
|
||
15 years ago
|
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'");
|
||
|
|
||
14 years ago
|
map.replace('n', "Restaurant 'Chew It'");
|
||
15 years ago
|
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'");
|
||
|
|
||
14 years ago
|
map.replace('n', "Restaurant \"Chew It\"");
|
||
15 years ago
|
s = "kedit --caption \"%n\" %f";
|
||
|
check( "KMacroExpander::expandMacrosShellQuote", s, KMacroExpander::expandMacrosShellQuote(s, map), "kedit --caption \"Restaurant \\\"Chew It\\\"\" 'filename.txt'");
|
||
|
|
||
14 years ago
|
map.replace('n', "Restaurant $HOME");
|
||
15 years ago
|
s = "kedit --caption \"%n\" %f";
|
||
|
check( "KMacroExpander::expandMacrosShellQuote", s, KMacroExpander::expandMacrosShellQuote(s, map), "kedit --caption \"Restaurant \\$HOME\" 'filename.txt'");
|
||
|
|
||
14 years ago
|
map.replace('n', "Restaurant `echo hello`");
|
||
15 years ago
|
s = "kedit --caption \"%n\" %f";
|
||
|
check( "KMacroExpander::expandMacrosShellQuote", s, KMacroExpander::expandMacrosShellQuote(s, map), "kedit --caption \"Restaurant \\`echo hello\\`\" 'filename.txt'");
|
||
|
|
||
14 years ago
|
map.replace('n', "Restaurant `echo hello`");
|
||
15 years ago
|
s = "kedit --caption \"`echo %n`\" %f";
|
||
|
check( "KMacroExpander::expandMacrosShellQuote", s, KMacroExpander::expandMacrosShellQuote(s, map), "kedit --caption \"$( echo 'Restaurant `echo hello`')\" 'filename.txt'");
|
||
|
|
||
15 years ago
|
TQMap<TQString,TQString> smap;
|
||
15 years ago
|
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;
|
||
|
}
|
||
|
|