summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorRhys Weatherley <rhys.weatherley@nokia.com>2010-12-06 08:47:49 +1000
committerRhys Weatherley <rhys.weatherley@nokia.com>2010-12-06 08:47:49 +1000
commitf32b158a249bd1135a6a58c9443095101c2210ec (patch)
treefe31727b5be302f12d69b19918fd039cabe010bb /src
parentbf945d8868531395cfc7d044c89f96c6dbb18480 (diff)
QObject wrapper for QTestResult
Diffstat (limited to 'src')
-rw-r--r--src/quicktestlib/qdeclarativetestresult.cpp489
-rw-r--r--src/quicktestlib/qdeclarativetestresult_p.h153
-rw-r--r--src/quicktestlib/quicktestlib.pro6
3 files changed, 646 insertions, 2 deletions
diff --git a/src/quicktestlib/qdeclarativetestresult.cpp b/src/quicktestlib/qdeclarativetestresult.cpp
new file mode 100644
index 0000000..40d2d01
--- /dev/null
+++ b/src/quicktestlib/qdeclarativetestresult.cpp
@@ -0,0 +1,489 @@
+/****************************************************************************
+**
+** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
+** All rights reserved.
+** Contact: Nokia Corporation (qt-info@nokia.com)
+**
+** This file is part of the test suite of the Qt Toolkit.
+**
+** $QT_BEGIN_LICENSE:LGPL$
+** No Commercial Usage
+** This file contains pre-release code and may not be distributed.
+** You may use this file in accordance with the terms and conditions
+** contained in the Technology Preview License Agreement accompanying
+** this package.
+**
+** GNU Lesser General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU Lesser
+** General Public License version 2.1 as published by the Free Software
+** Foundation and appearing in the file LICENSE.LGPL included in the
+** packaging of this file. Please review the following information to
+** ensure the GNU Lesser General Public License version 2.1 requirements
+** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
+**
+** In addition, as a special exception, Nokia gives you certain additional
+** rights. These rights are described in the Nokia Qt LGPL Exception
+** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
+**
+** If you have questions regarding the use of this file, please contact
+** Nokia at qt-info@nokia.com.
+**
+**
+**
+**
+**
+**
+**
+**
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+#include "qdeclarativetestresult_p.h"
+#include "qtestcase.h"
+#include "qtestresult_p.h"
+#include "qtesttable_p.h"
+#include "qtestlog_p.h"
+#include <QtCore/qset.h>
+#include <QtCore/qmap.h>
+#include <QtCore/qbytearray.h>
+#include <QtCore/qcoreapplication.h>
+
+QT_BEGIN_NAMESPACE
+
+class QDeclarativeTestResultPrivate
+{
+public:
+ QDeclarativeTestResultPrivate()
+ : table(0)
+ {
+ }
+ ~QDeclarativeTestResultPrivate()
+ {
+ delete table;
+ }
+
+ QByteArray intern(const QString &str);
+ void updateTestObjectName();
+
+ QString programName;
+ QString testCaseName;
+ QString functionName;
+ QSet<QByteArray> internedStrings;
+ QMap<QString, int> globalTags;
+ QTestTable *table;
+};
+
+QByteArray QDeclarativeTestResultPrivate::intern(const QString &str)
+{
+ QByteArray bstr = str.toUtf8();
+ return *(internedStrings.insert(bstr));
+}
+
+void QDeclarativeTestResultPrivate::updateTestObjectName()
+{
+ // In plain logging mode we use the TestCase name as the
+ // class name so that multiple TestCase elements will report
+ // results with "testCase::function". In XML logging mode,
+ // we use the program name as the class name and report test
+ // functions as "testCase__function".
+ if (QTestLog::logMode() == QTestLog::Plain) {
+ if (testCaseName.isEmpty()) {
+ if (programName.isEmpty()) {
+ QTestResult::setCurrentTestObject(0);
+ } else {
+ QTestResult::setCurrentTestObject
+ (intern(programName).constData());
+ }
+ } else if (QTestLog::logMode() == QTestLog::Plain) {
+ QTestResult::setCurrentTestObject
+ (intern(testCaseName).constData());
+ }
+ } else if (programName.isEmpty()) {
+ QTestResult::setCurrentTestObject(0);
+ } else {
+ QTestResult::setCurrentTestObject
+ (intern(programName).constData());
+ }
+}
+
+QDeclarativeTestResult::QDeclarativeTestResult(QObject *parent)
+ : QObject(parent), d_ptr(new QDeclarativeTestResultPrivate)
+{
+}
+
+QDeclarativeTestResult::~QDeclarativeTestResult()
+{
+}
+
+/*!
+ \qmlproperty string TestResult::programName
+
+ This property defines the name of the test program that
+ is running the test cases. If this string is set to empty,
+ then all TestCase elements have been executed and the program
+ is about to exit.
+
+ \sa testCaseName, functionName
+*/
+QString QDeclarativeTestResult::programName() const
+{
+ Q_D(const QDeclarativeTestResult);
+ return d->programName;
+}
+
+void QDeclarativeTestResult::setProgramName(const QString &name)
+{
+ Q_D(QDeclarativeTestResult);
+ d->programName = name;
+ d->updateTestObjectName();
+ emit programNameChanged();
+}
+
+/*!
+ \qmlproperty string TestResult::testCaseName
+
+ This property defines the name of current TestCase element
+ that is running test cases. If this string is empty, then
+ programName will be used instead.
+
+ \sa programName, functionName
+*/
+QString QDeclarativeTestResult::testCaseName() const
+{
+ Q_D(const QDeclarativeTestResult);
+ return d->testCaseName;
+}
+
+void QDeclarativeTestResult::setTestCaseName(const QString &name)
+{
+ Q_D(QDeclarativeTestResult);
+ d->testCaseName = name;
+ d->updateTestObjectName();
+ emit testCaseNameChanged();
+}
+
+/*!
+ \qmlproperty string TestResult::functionName
+
+ This property defines the name of current test function
+ within a TestCase element that is running. If this string is
+ empty, then no function is currently running.
+
+ \sa programName, testCaseName
+*/
+QString QDeclarativeTestResult::functionName() const
+{
+ Q_D(const QDeclarativeTestResult);
+ return d->functionName;
+}
+
+void QDeclarativeTestResult::setFunctionName(const QString &name)
+{
+ Q_D(QDeclarativeTestResult);
+ if (!name.isEmpty()) {
+ // In plain logging mode, we use the function name directly.
+ // In XML logging mode, we use "testCase__functionName" as the
+ // programName is acting as the class name.
+ if (QTestLog::logMode() == QTestLog::Plain ||
+ d->testCaseName.isEmpty() ||
+ d->testCaseName == d->programName) {
+ QTestResult::setCurrentTestFunction
+ (d->intern(name).constData());
+ } else {
+ QString fullName = d->testCaseName + QLatin1String("__") + name;
+ QTestResult::setCurrentTestFunction
+ (d->intern(fullName).constData());
+ }
+ } else {
+ QTestResult::setCurrentTestFunction(0);
+ }
+ emit functionNameChanged();
+}
+
+QDeclarativeTestResult::FunctionType QDeclarativeTestResult::functionType() const
+{
+ return FunctionType(QTestResult::currentTestLocation());
+}
+
+void QDeclarativeTestResult::setFunctionType(FunctionType type)
+{
+ QTestResult::setCurrentTestLocation(QTestResult::TestLocation(type));
+ emit functionTypeChanged();
+}
+
+/*!
+ \qmlproperty string TestResult::dataTag
+
+ This property defines the tag for the current row in a
+ data-driven test, or an empty string if not a data-driven test.
+
+ \sa globalDataTag
+*/
+QString QDeclarativeTestResult::dataTag() const
+{
+ const char *tag = QTestResult::currentDataTag();
+ if (tag)
+ return QString::fromUtf8(tag);
+ else
+ return QString();
+}
+
+void QDeclarativeTestResult::setDataTag(const QString &tag)
+{
+ if (!tag.isEmpty()) {
+ QTestData *data = &(QTest::newRow(tag.toUtf8().constData()));
+ QTestResult::setCurrentTestData(data);
+ emit dataTagChanged();
+ } else {
+ QTestResult::setCurrentTestData(0);
+ }
+}
+
+/*!
+ \qmlproperty string TestResult::globalDataTag
+
+ This property defines the tag for the current row in the
+ global data table, or the empty string if the global data
+ table is not in use.
+
+ \sa dataTag, addGlobalDataTag()
+*/
+QString QDeclarativeTestResult::globalDataTag() const
+{
+ const char *tag = QTestResult::currentGlobalDataTag();
+ if (tag)
+ return QString::fromUtf8(tag);
+ else
+ return QString();
+}
+
+void QDeclarativeTestResult::setGlobalDataTag(const QString &tag)
+{
+ Q_D(QDeclarativeTestResult);
+ if (!tag.isEmpty()) {
+ int index = d->globalTags.value(tag, -1);
+ Q_ASSERT(index >= 0);
+ QTestResult::setCurrentGlobalTestData
+ (QTestTable::globalTestTable()->testData(index));
+ } else {
+ QTestResult::setCurrentGlobalTestData(0);
+ }
+ emit globalDataTagChanged();
+}
+
+/*!
+ \qmlproperty bool TestResult::failed
+
+ This property returns true if the current test function has
+ failed; false otherwise. The fail state is reset when
+ functionName is changed or finishTestFunction() is called.
+
+ \sa skipped, dataFailed
+*/
+bool QDeclarativeTestResult::isFailed() const
+{
+ return QTestResult::testFailed();
+}
+
+/*!
+ \qmlproperty bool TestResult::dataFailed
+
+ This property returns true if the current data function has
+ failed; false otherwise. The fail state is reset when
+ functionName is changed or finishTestFunction() is called.
+
+ \sa failed
+*/
+bool QDeclarativeTestResult::isDataFailed() const
+{
+ return QTestResult::currentTestFailed();
+}
+
+/*!
+ \qmlproperty bool TestResult::skipped
+
+ This property returns true if the current test function was
+ marked as skipped; false otherwise.
+
+ \sa failed
+*/
+bool QDeclarativeTestResult::isSkipped() const
+{
+ return QTestResult::skipCurrentTest();
+}
+
+void QDeclarativeTestResult::setSkipped(bool skip)
+{
+ QTestResult::setSkipCurrentTest(skip);
+ emit skippedChanged();
+}
+
+/*!
+ \qmlproperty int TestResult::passCount
+
+ This property returns the number of tests that have passed.
+
+ \sa failCount, skipCount
+*/
+int QDeclarativeTestResult::passCount() const
+{
+ return QTestResult::passCount();
+}
+
+/*!
+ \qmlproperty int TestResult::failCount
+
+ This property returns the number of tests that have failed.
+
+ \sa passCount, skipCount
+*/
+int QDeclarativeTestResult::failCount() const
+{
+ return QTestResult::failCount();
+}
+
+/*!
+ \qmlproperty int TestResult::skipCount
+
+ This property returns the number of tests that have been skipped.
+
+ \sa passCount, failCount
+*/
+int QDeclarativeTestResult::skipCount() const
+{
+ return QTestResult::skipCount();
+}
+
+/*!
+ \qmlmethod TestResult::reset()
+
+ Resets all pass/fail/skip counters and prepare for testing.
+*/
+void QDeclarativeTestResult::reset()
+{
+ QTestResult::reset();
+}
+
+/*!
+ \qmlmethod TestResult::startLogging()
+
+ Starts logging to the test output stream and writes the
+ test header for programName.
+
+ \sa stopLogging()
+*/
+void QDeclarativeTestResult::startLogging()
+{
+ // The program name is used for logging headers and footers.
+ Q_D(QDeclarativeTestResult);
+ const char *saved = QTestResult::currentTestObjectName();
+ QTestResult::setCurrentTestObject(d->intern(d->programName).constData());
+ QTestLog::startLogging();
+ QTestResult::setCurrentTestObject(saved);
+}
+
+/*!
+ \qmlmethod TestResult::stopLogging()
+
+ Writes the test footer for programName to the test
+ output stream and then stops logging.
+
+ \sa startLogging()
+*/
+void QDeclarativeTestResult::stopLogging()
+{
+ Q_D(QDeclarativeTestResult);
+ const char *saved = QTestResult::currentTestObjectName();
+ QTestResult::setCurrentTestObject(d->intern(d->programName).constData());
+ QTestLog::stopLogging();
+ QTestResult::setCurrentTestObject(saved);
+}
+
+void QDeclarativeTestResult::initGlobalTestTable()
+{
+ Q_D(QDeclarativeTestResult);
+ d->globalTags.clear();
+ QTestTable::clearGlobalTestTable();
+ QTestTable::globalTestTable();
+ Q_ASSERT(QTestTable::currentTestTable() == QTestTable::globalTestTable());
+}
+
+void QDeclarativeTestResult::clearGlobalTestTable()
+{
+ QTestTable::clearGlobalTestTable();
+}
+
+void QDeclarativeTestResult::addGlobalDataTag(const QString &tag)
+{
+ Q_D(QDeclarativeTestResult);
+ Q_ASSERT(!tag.isEmpty());
+ if (!d->globalTags.contains(tag))
+ d->globalTags.insert(tag, d->globalTags.count());
+ QTest::newRow(tag.toUtf8().constData());
+}
+
+void QDeclarativeTestResult::initTestTable()
+{
+ Q_D(QDeclarativeTestResult);
+ delete d->table;
+ d->table = new QTestTable;
+}
+
+void QDeclarativeTestResult::clearTestTable()
+{
+ Q_D(QDeclarativeTestResult);
+ delete d->table;
+ d->table = 0;
+}
+
+void QDeclarativeTestResult::finishTestFunction()
+{
+ QTestResult::finishedCurrentTestFunction();
+}
+
+void QDeclarativeTestResult::fail(const QString &message)
+{
+ QTestResult::addFailure(message.toLatin1().constData(), "", 0);
+}
+
+bool QDeclarativeTestResult::verify(bool success, const QString &message)
+{
+ if (message.isEmpty()) {
+ return QTestResult::verify(success, "verify()", "", "", 0);
+ } else {
+ return QTestResult::verify
+ (success, message.toLatin1().constData(), "", "", 0);
+ }
+}
+
+bool QDeclarativeTestResult::compare
+ (bool success, const QString &message,
+ const QString &val1, const QString &val2)
+{
+ return QTestResult::compare
+ (success, message.toLocal8Bit().constData(),
+ QTest::toString(val1.toLatin1().constData()),
+ QTest::toString(val2.toLatin1().constData()),
+ "", "", "", 0);
+}
+
+void QDeclarativeTestResult::skipSingle(const QString &message)
+{
+ QTestResult::addSkip(message.toLatin1().constData(),
+ QTest::SkipSingle, "", 0);
+}
+
+void QDeclarativeTestResult::skipAll(const QString &message)
+{
+ QTestResult::addSkip(message.toLatin1().constData(),
+ QTest::SkipAll, "", 0);
+ QTestResult::setSkipCurrentTest(true);
+}
+
+void QDeclarativeTestResult::warn(const QString &message)
+{
+ QTestLog::warn(message.toLatin1().constData());
+}
+
+QT_END_NAMESPACE
diff --git a/src/quicktestlib/qdeclarativetestresult_p.h b/src/quicktestlib/qdeclarativetestresult_p.h
new file mode 100644
index 0000000..2cc0822
--- /dev/null
+++ b/src/quicktestlib/qdeclarativetestresult_p.h
@@ -0,0 +1,153 @@
+/****************************************************************************
+**
+** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
+** All rights reserved.
+** Contact: Nokia Corporation (qt-info@nokia.com)
+**
+** This file is part of the test suite of the Qt Toolkit.
+**
+** $QT_BEGIN_LICENSE:LGPL$
+** No Commercial Usage
+** This file contains pre-release code and may not be distributed.
+** You may use this file in accordance with the terms and conditions
+** contained in the Technology Preview License Agreement accompanying
+** this package.
+**
+** GNU Lesser General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU Lesser
+** General Public License version 2.1 as published by the Free Software
+** Foundation and appearing in the file LICENSE.LGPL included in the
+** packaging of this file. Please review the following information to
+** ensure the GNU Lesser General Public License version 2.1 requirements
+** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
+**
+** In addition, as a special exception, Nokia gives you certain additional
+** rights. These rights are described in the Nokia Qt LGPL Exception
+** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
+**
+** If you have questions regarding the use of this file, please contact
+** Nokia at qt-info@nokia.com.
+**
+**
+**
+**
+**
+**
+**
+**
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+#ifndef QDECLARATIVETESTRESULT_P_H
+#define QDECLARATIVETESTRESULT_P_H
+
+#include "quicktestglobal.h"
+#include <QtCore/qobject.h>
+#include <QtCore/qstring.h>
+#include <QtCore/qscopedpointer.h>
+
+QT_BEGIN_NAMESPACE
+
+class QDeclarativeTestResultPrivate;
+
+class Q_TEST_QUICK_EXPORT QDeclarativeTestResult : public QObject
+{
+ Q_OBJECT
+ Q_ENUMS(FunctionType)
+ Q_PROPERTY(QString programName READ programName WRITE setProgramName NOTIFY programNameChanged)
+ Q_PROPERTY(QString testCaseName READ testCaseName WRITE setTestCaseName NOTIFY testCaseNameChanged)
+ Q_PROPERTY(QString functionName READ functionName WRITE setFunctionName NOTIFY functionNameChanged)
+ Q_PROPERTY(FunctionType functionType READ functionType WRITE setFunctionType NOTIFY functionTypeChanged)
+ Q_PROPERTY(QString dataTag READ dataTag WRITE setDataTag NOTIFY dataTagChanged)
+ Q_PROPERTY(QString globalDataTag READ globalDataTag WRITE setGlobalDataTag NOTIFY globalDataTagChanged)
+ Q_PROPERTY(bool failed READ isFailed)
+ Q_PROPERTY(bool dataFailed READ isDataFailed)
+ Q_PROPERTY(bool skipped READ isSkipped WRITE setSkipped NOTIFY skippedChanged)
+ Q_PROPERTY(int passCount READ passCount)
+ Q_PROPERTY(int failCount READ failCount)
+ Q_PROPERTY(int skipCount READ skipCount)
+public:
+ QDeclarativeTestResult(QObject *parent = 0);
+ ~QDeclarativeTestResult();
+
+ // Values must match QTestResult::TestLocation.
+ enum FunctionType
+ {
+ NoWhere = 0,
+ DataFunc = 1,
+ InitFunc = 2,
+ Func = 3,
+ CleanupFunc = 4
+ };
+
+ QString programName() const;
+ void setProgramName(const QString &name);
+
+ QString testCaseName() const;
+ void setTestCaseName(const QString &name);
+
+ QString functionName() const;
+ void setFunctionName(const QString &name);
+
+ FunctionType functionType() const;
+ void setFunctionType(FunctionType type);
+
+ QString dataTag() const;
+ void setDataTag(const QString &tag);
+
+ QString globalDataTag() const;
+ void setGlobalDataTag(const QString &tag);
+
+ bool isFailed() const;
+ bool isDataFailed() const;
+
+ bool isSkipped() const;
+ void setSkipped(bool skip);
+
+ int passCount() const;
+ int failCount() const;
+ int skipCount() const;
+
+public Q_SLOTS:
+ void reset();
+
+ void startLogging();
+ void stopLogging();
+
+ void initGlobalTestTable();
+ void clearGlobalTestTable();
+ void addGlobalDataTag(const QString &tag);
+
+ void initTestTable();
+ void clearTestTable();
+
+ void finishTestFunction();
+
+ void fail(const QString &message);
+ bool verify(bool success, const QString &message);
+ bool compare(bool success, const QString &message,
+ const QString &val1, const QString &val2);
+ void skipSingle(const QString &message);
+ void skipAll(const QString &message);
+ void warn(const QString &message);
+
+Q_SIGNALS:
+ void programNameChanged();
+ void testCaseNameChanged();
+ void functionNameChanged();
+ void functionTypeChanged();
+ void dataTagChanged();
+ void globalDataTagChanged();
+ void skippedChanged();
+
+private:
+ QScopedPointer<QDeclarativeTestResultPrivate> d_ptr;
+
+ Q_DECLARE_PRIVATE(QDeclarativeTestResult)
+ Q_DISABLE_COPY(QDeclarativeTestResult)
+};
+
+QT_END_NAMESPACE
+
+#endif
diff --git a/src/quicktestlib/quicktestlib.pro b/src/quicktestlib/quicktestlib.pro
index 5e30d7a..e57d34a 100644
--- a/src/quicktestlib/quicktestlib.pro
+++ b/src/quicktestlib/quicktestlib.pro
@@ -26,12 +26,14 @@ symbian {
SOURCES += \
qdeclarativetest.cpp \
- qdeclarativetestreport.cpp
+ qdeclarativetestreport.cpp \
+ qdeclarativetestresult.cpp
HEADERS += \
quicktestglobal.h \
qdeclarativetest.h
PRIVATE_HEADERS += \
- qdeclarativetestreport_p.h
+ qdeclarativetestreport_p.h \
+ qdeclarativetestresult_p.h
PUBLIC_HEADERS += $$HEADERS
HEADERS += $$PRIVATE_HEADERS