00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include "testobject.h"
00021 #include "testwindow.h"
00022
00023
00024 #include "../core/action.h"
00025 #include "../core/interpreter.h"
00026 #include "../core/manager.h"
00027
00028
00029 #include <QString>
00030 #include <QFile>
00031 #include <QMetaObject>
00032 #include <QMetaMethod>
00033
00034
00035 #include <kdebug.h>
00036 #include <kinstance.h>
00037 #include <kapplication.h>
00038 #include <kcmdlineargs.h>
00039 #include <kaboutdata.h>
00040 #include <ksharedptr.h>
00041
00042
00043 #include <string>
00044 #include <iostream>
00045
00046 #define ERROR_OK 0
00047 #define ERROR_HELP -1
00048 #define ERROR_NOSUCHFILE -2
00049 #define ERROR_OPENFAILED -3
00050 #define ERROR_NOINTERPRETER -4
00051 #define ERROR_EXCEPTION -6
00052
00053 KApplication *app = 0;
00054
00055 static KCmdLineOptions options[] =
00056 {
00057 { "gui", "Start the GUI; otherwise the command line application is used.", 0 },
00058 { "+file", "Scriptfile", 0 },
00059
00060
00061
00062 { 0, 0, 0 }
00063 };
00064
00065 QString getInterpreterName(const QString& scriptfile)
00066 {
00067 Kross::InterpreterInfo* interpreterinfo = Kross::Manager::self().interpreterInfo( Kross::Manager::self().interpreternameForFile(scriptfile) );
00068 return interpreterinfo ? interpreterinfo->interpreterName() : "python";
00069 }
00070
00071 int readFile(const QString& scriptfile, QString& content)
00072 {
00073 QFile f(QFile::encodeName(scriptfile));
00074 if(! f.exists()) {
00075 std::cerr << "No such scriptfile: " << scriptfile.toLatin1().data() << std::endl;
00076 return ERROR_NOSUCHFILE;
00077 }
00078 if(! f.open(QIODevice::ReadOnly)) {
00079 std::cerr << "Failed to open scriptfile: " << scriptfile.toLatin1().data() << std::endl;
00080 return ERROR_OPENFAILED;
00081 }
00082 content = f.readAll();
00083 f.close();
00084 return ERROR_OK;
00085 }
00086
00087 static TestObject* testobj1 = 0;
00088 static TestObject* testobj2 = 0;
00089
00090 void finishTestEnvironment()
00091 {
00092 delete testobj1; testobj1 = 0;
00093 delete testobj2; testobj2 = 0;
00094 }
00095
00096 void initTestEnvironment()
00097 {
00098
00099 testobj1 = new TestObject("TestObject1");
00100 testobj2 = new TestObject("TestObject2");
00101
00102
00103 Kross::Manager::self().addObject( testobj1 );
00104 Kross::Manager::self().addObject( testobj2 );
00105 }
00106
00107 int runScriptFile(const QString& scriptfile)
00108 {
00109
00110 QString scriptcode;
00111 int result = readFile(scriptfile, scriptcode);
00112 if(result != ERROR_OK)
00113 return result;
00114
00115
00116 QString interpretername = getInterpreterName(scriptfile);
00117
00118
00119 TestObject* testobj3 = new TestObject("TestObject3");
00120 TestObject* testobj4 = new TestObject("TestObject4");
00121
00122
00123 Kross::Action* action = new Kross::Action( scriptfile );
00124 action->setInterpreter( interpretername );
00125 action->setCode( scriptcode );
00126
00127
00128 action->addObject( testobj3 );
00129 action->addObject( testobj4 );
00130
00131
00132 std::cout << "Execute scriptfile " << scriptfile.toLatin1().data() << " now" << std::endl;
00133 action->trigger();
00134 std::cout << "Execution of scriptfile " << scriptfile.toLatin1().data() << " done" << std::endl;
00135
00136
00137
00138
00139
00140
00141
00142
00143
00144
00145
00146 delete action;
00147 delete testobj3;
00148 delete testobj4;
00149
00150 return ERROR_OK;
00151 }
00152
00153 int main(int argc, char **argv)
00154 {
00155 int result = 0;
00156
00157 KAboutData about("krosstest",
00158 "KrossTest",
00159 "0.1",
00160 "KDE application to test the Kross framework.",
00161 KAboutData::License_LGPL,
00162 "(C) 2005 Sebastian Sauer",
00163 "Test the Kross framework!",
00164 "http://kross.dipe.org",
00165 "kross@dipe.org");
00166 about.addAuthor("Sebastian Sauer", "Author", "mail@dipe.org");
00167
00168 KCmdLineArgs::init(argc, argv, &about);
00169 KCmdLineArgs::addCmdLineOptions(options);
00170
00171 KCmdLineArgs *args = KCmdLineArgs::parsedArgs();
00172
00173 QStringList scriptfiles;
00174 for(int i = 0; i < args->count(); i++)
00175 scriptfiles.append( QFile::decodeName(args->arg(i)) );
00176
00177
00178 if(scriptfiles.count() < 1)
00179 scriptfiles.append("testcase.py");
00180
00181 if(scriptfiles.count() < 1) {
00182 std::cerr << "No scriptfile to execute defined. See --help" << std::endl;
00183 return ERROR_NOSUCHFILE;
00184 }
00185
00186 if( args->isSet("gui") ) {
00187 app = new KApplication();
00188
00189 QString interpretername, scriptcode;
00190 if(scriptfiles.count() > 0) {
00191 result = readFile( scriptfiles[0], scriptcode );
00192 if(result == ERROR_OK)
00193 interpretername = getInterpreterName( scriptfiles[0] );
00194 }
00195
00196 if(result == ERROR_OK) {
00197 initTestEnvironment();
00198
00199 TestWindow *mainWin = new TestWindow(interpretername, scriptcode);
00200 app->setMainWidget(mainWin);
00201 mainWin->show();
00202 args->clear();
00203 result = app->exec();
00204 }
00205 }
00206 else {
00207 app = new KApplication(true);
00208 initTestEnvironment();
00209
00210 foreach(QString file, scriptfiles) {
00211 result = runScriptFile(file);
00212 if(result != ERROR_OK)
00213 break;
00214 }
00215 }
00216
00217 finishTestEnvironment();
00218 delete app;
00219
00220 kDebug() << "DONE!!!" << endl;
00221 return result;
00222 }