00001 #include "testbench.h"
00002
00003 #include "tools/global.h"
00004
00005 #include <QTextStream>
00006
00007 #ifdef Q_OS_WIN32
00008 #define FILTER "test_*.dll"
00009 #else
00010 #define FILTER "libtest_*.so"
00011 #endif
00012
00013 template<typename T>
00014 QTextStream& operator<<(QTextStream& stream, QList<T> list)
00015 {
00016 if (list.isEmpty())
00017 return stream;
00018
00019 stream << "(" << list.first();
00020
00021 for (int i = 1; i < list.size(); i++)
00022 stream << ", " << list[i];
00023
00024 return stream << ")";
00025 }
00026
00027 int main(int argc, char* argv[])
00028 {
00029 QCoreApplication app(argc, argv);
00030
00031 QTextStream console(stdout);
00032
00033 QMap<QString, TestInterface*> testsMap;
00034
00035 QDir pluginsDir = PLUGINS_PATH;
00036 pluginsDir.setNameFilters(QStringList() << FILTER);
00037
00038 Q_FOREACH (QString fileName, pluginsDir.entryList(QDir::Files))
00039 {
00040
00041 QPluginLoader pluginLoader(pluginsDir.absoluteFilePath(fileName));
00042
00043 if (pluginLoader.load())
00044 {
00045 TestInterface* test = qobject_cast<TestInterface*>(pluginLoader.instance());
00046
00047 if (test != 0)
00048 {
00049 QString name = test->name();
00050
00051
00052 if (testsMap.contains(name))
00053 {
00054 console << "Tests: multiple plugins with name "
00055 << name << ", overwriting existing" << endl;
00056 }
00057
00058 testsMap[name] = test;
00059 }
00060 else
00061 {
00062 console << "Tests: plugin in " << fileName
00063 << " is not of type TestInterface";
00064 }
00065 }
00066 else
00067 console << pluginLoader.errorString() << endl;
00068 }
00069
00070
00071 QStringList args(argv[0]);
00072 QList<TestInterface*> tests;
00073
00074 if (argc > 1)
00075 {
00076 if (QString("-list") == argv[1])
00077 {
00078 console << "Available tests:" << endl;
00079
00080 Q_FOREACH (QString test, testsMap.keys())
00081 console << " " << test;
00082
00083 return 0;
00084 }
00085 else
00086 {
00087 int i;
00088 for (i = 1; i < argc && testsMap.keys().contains(argv[i]); i++)
00089 {
00090
00091 tests << testsMap[argv[i]];
00092 }
00093
00094 for (; i < argc; i++)
00095 args << argv[i];
00096 }
00097 }
00098
00099 if (tests.isEmpty())
00100 tests = testsMap.values();
00101
00102 int retVal = 0;
00103 QStringList passedTests, failedTests;
00104
00105 Q_FOREACH (TestInterface* test, tests)
00106 {
00107 console << "Tests: executing test " << test->name() << " with ";
00108
00109 if (args.size() > 1)
00110 console << "args " << args.mid(1);
00111 else
00112 console << "no args";
00113
00114 console << endl;
00115
00116
00117 #if QT_VERSION >= 0x040400
00118 int ret = QTest::qExec(test, args);
00119 #else
00120 char** charArgs = new char*[args.count()];
00121
00122 for (int i = 0; i < args.count(); i++)
00123 charArgs[i] = args[i].toAscii().data();
00124
00125 int ret = QTest::qExec(test, args.count(), charArgs);
00126
00127 delete[] charArgs;
00128 #endif
00129
00130 if (ret != 0)
00131 {
00132 retVal = ret;
00133 failedTests << test->name();
00134 }
00135 else
00136 passedTests << test->name();
00137 }
00138
00139 console << "*********** Tests: summary ***********" << endl;
00140 console << tests.size() << " tests executed" << endl;
00141 console << passedTests.size() << " tests passed " << passedTests << endl;
00142 console << failedTests.size() << " tests failed " << failedTests << endl;
00143 console << "********* Tests: end summary *********" << endl;
00144
00145 return retVal;
00146 }