From b68bae1132b5dc5c8f55435c72cd48e24aa52ec9 Mon Sep 17 00:00:00 2001 From: Jo Asplin Date: Tue, 1 Nov 2011 12:19:39 +0100 Subject: Added -datatags option to QTestLib (Note: This feature is ported from Qt 4.8. See the following commits: 01575deafb7d26ca2431374e92c6d71de96547c7 4866d1ba8afbab61e102942d1ea93b81fea053d6 ) Passing the -datatags option to a QTestLib program prints the available data tags to standard output. For completeness, the test case name is also printed at the start of each output line. (Although the file name is supposed to match the lower-case version of the test case name, this is currently not true in all cases (particularly not under tests/benchmarks/). Even if there was a script to enforce this convention, the -datatags option provides this information in a reliable way.) Data tags for each test function (f() in this case) are printed in four different ways depending on the presence of local and global data tags: Case 1: No tags: tst_MyTestCasetst_MyTestCase f Case 2: Local tags only: tst_MyTestCase f local tag 1 tst_MyTestCase f local tag 2 ... Case 3: Global tags only: tst_MyTestCase f __global__ global tag 1 tst_MyTestCase f __global__ global tag 2 ... Case 4: Local and global tags: tst_MyTestCase f local tag 1 __global__ global tag 1 tst_MyTestCase f local tag 2 __global__ global tag 1 ... tst_MyTestCase f local tag 1 __global__ global tag 2 tst_MyTestCase f local tag 2 __global__ global tag 2 ... ... Note that the string __global__ is assumed to be highly unlikely to occur in a data tag (if it does, an ambiguity results). Change-Id: Ib51aa0c3c32ad52e52ce519729292cf8f0ec5d50 Reviewed-by: Jason McDonald Reviewed-by: Sergio Ahumada --- src/testlib/qtestcase.cpp | 72 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) (limited to 'src/testlib/qtestcase.cpp') diff --git a/src/testlib/qtestcase.cpp b/src/testlib/qtestcase.cpp index 713004da4d..4e0205ccbc 100644 --- a/src/testlib/qtestcase.cpp +++ b/src/testlib/qtestcase.cpp @@ -1015,6 +1015,71 @@ static void qPrintTestSlots(FILE *stream) } } +static void qPrintDataTags(FILE *stream) +{ + // Avoid invoking the actual test functions, and also avoid printing irrelevant output: + QTestLog::setPrintAvailableTagsMode(); + + // Get global data tags: + QTestTable::globalTestTable(); + invokeMethod(QTest::currentTestObject, "initTestCase_data()"); + const QTestTable *gTable = QTestTable::globalTestTable(); + + const QMetaObject *currTestMetaObj = QTest::currentTestObject->metaObject(); + + // Process test functions: + for (int i = 0; i < currTestMetaObj->methodCount(); ++i) { + QMetaMethod tf = currTestMetaObj->method(i); + + if (isValidSlot(tf)) { + + // Retrieve local tags: + QStringList localTags; + QTestTable table; + char *slot = qstrdup(tf.signature()); + slot[strlen(slot) - 2] = '\0'; + QByteArray member; + member.resize(qstrlen(slot) + qstrlen("_data()") + 1); + QTest::qt_snprintf(member.data(), member.size(), "%s_data()", slot); + invokeMethod(QTest::currentTestObject, member.constData()); + for (int j = 0; j < table.dataCount(); ++j) + localTags << QLatin1String(table.testData(j)->dataTag()); + + // Print all tag combinations: + if (gTable->dataCount() == 0) { + if (localTags.count() == 0) { + // No tags at all, so just print the test function: + fprintf(stream, "%s %s\n", currTestMetaObj->className(), slot); + } else { + // Only local tags, so print each of them: + for (int k = 0; k < localTags.size(); ++k) + fprintf( + stream, "%s %s %s\n", + currTestMetaObj->className(), slot, localTags.at(k).toLatin1().data()); + } + } else { + for (int j = 0; j < gTable->dataCount(); ++j) { + if (localTags.count() == 0) { + // Only global tags, so print the current one: + fprintf( + stream, "%s %s __global__ %s\n", + currTestMetaObj->className(), slot, gTable->testData(j)->dataTag()); + } else { + // Local and global tags, so print each of the local ones and + // the current global one: + for (int k = 0; k < localTags.size(); ++k) + fprintf( + stream, "%s %s %s __global__ %s\n", currTestMetaObj->className(), slot, + localTags.at(k).toLatin1().data(), gTable->testData(j)->dataTag()); + } + } + } + + delete[] slot; + } + } +} + static int qToInt(char *str) { char *pEnd; @@ -1061,6 +1126,8 @@ Q_TESTLIB_EXPORT void qtest_qParseArgs(int argc, char *argv[], bool qml) "\n" " Testing options:\n" " -functions : Returns a list of current testfunctions\n" + " -datatags : Returns a list of current data tags.\n" + " A global data tag is preceded by ' __global__ '.\n" " -eventdelay ms : Set default delay for mouse and keyboard simulation to ms milliseconds\n" " -keydelay ms : Set default delay for keyboard simulation to ms milliseconds\n" " -mousedelay ms : Set default delay for mouse simulation to ms milliseconds\n" @@ -1111,6 +1178,11 @@ Q_TESTLIB_EXPORT void qtest_qParseArgs(int argc, char *argv[], bool qml) qPrintTestSlots(stdout); exit(0); } + } else if (strcmp(argv[i], "-datatags") == 0) { + if (!qml) { + qPrintDataTags(stdout); + exit(0); + } } else if (strcmp(argv[i], "-txt") == 0) { logFormat = QTestLog::Plain; } else if (strcmp(argv[i], "-xunitxml") == 0) { -- cgit v1.2.3